Best way to use a form as a GUI, but code it elsewhere - vb.net

I'm wanting to use visual studio to create a form with buttons, but then have it coded elsewhere, so that I can use the events (ie; button clicks) to perform specific actions in this application.
Is it possible/practical to do this? If not/if so, what do I need to look up and learn about from here to be able to implement it? My knowledge base in programming is limitted; I've just been starting to get familiar with Classes.
(I'm working in Autodesk Inventor, and am trying to create a prompt window with buttons to control the output of another program I have. In order to save on the amount of calls/interfacing, I was hoping to just create an uncoded button form, but code it within my program/macro I have within Inventor - its to be a form with six buttons that end up rotating some graphics within the program at a certain stop point, and then the program resumes when the form is closed via the "x")
I have seen posts such as the one below, but that doesn't seem to have the capability to receive user input: How to create a custom MessageBox?
Currently I am here, which works for showing the toolbox. Can someone show me how I would handle the events please?
AddReference "C:\Users\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\bin\Release\SectionSymToolBox.dll"
Imports System.Windows.Forms
Public Class SectionSymRule
'Public dlg As New System.Windows.Forms.For
Public Shared ToolBox As New SectionSymToolBox.SectionSymToolBox
Dim WithEvents EClass As New EventClass
Sub Main()
ToolBox.Show()
End Sub
End Class
Public Class SectionSymToolBox
Private Sub Main()
End Sub
Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Swap Symbols
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Flip Symbol
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Flip Text
End Sub
Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'<
End Sub
Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'>
End Sub
End Class

You can always use Partial Class to split your code into multiple files.
So you create your Form (say Form1) the normal way. Then put the code in another class file with class declared as Partial
For example,
Partial Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(TextBox1.Text)
End Sub
End Class
The other way is to inherit your Form. The inherited form will then have everything of whatever is on the form, with whatever you want to add.
Public Class Form1Code
Inherits Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(TextBox1.Text)
End Sub
End Class

Related

Trouble switching between projects/forms in vb.net

I am working on a project in Visual Studio Community 2019 using VB.Net. I am developing a user interface wherein users start on one form (a sort of index, call it form 1) and select from their the next form they want to go to (say form 2). This part is working fine. The trouble is that after users finish in form 2, they should be directed back to form 1 to continue from here, and I can't get this to happen. I need somehow to make the main menu a public shared form so that the other projects can access it but I can’t figure out how to do that.
Here are the relevant lines from form 1:
Public Class frmMainMenu
Public Shared inst As frmMainMenu
Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
inst = Me
Timer1.Start()
Me.Show()
End Sub
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles
btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
formNew.Show()
Timer1.Stop()
Me.Hide()
End Sub
And here are the relevant lines from form 2. I want lines 23 and 24 to return the user to the main menu, but it doesn't work.
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
Me.Show
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles
btnMainMenu.Click
inst.frmMainMenu.Show()
inst.frmMainMenu.Timer1.Start()
Me.Close()
End Sub
I appreciate any help you can offer!
From my comments above:
When you create your instance of your second form called "formNew",
use the AddHandler command to wire up its FormClosed event to a method
in the main form. Then in the second form you can just call Me.Close()
and the method in the main form will fire where you can call
Me.Show()..
In main menu:
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
Timer1.Stop()
Me.Hide()
formNew.Show()
End Sub
Private Sub formNew_FormClosed(sender As Object, e As FormClosedEventArgs)
Me.Show()
Timer1.Start()
End Sub
End Class
In frmGeologyMenu:
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblTime.Text = 10
End Sub
Private Sub BtnMainMenu_Click(sender As Object, e As EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
End Class
In VB.NET, your startup form is always the default instance of its type. That means that you can simply use the type name to refer to your startup form anywhere in your app. If you use Me.Hide() in your startup form then frmMainMenu.Show() can be used anywhere to redisplay it. This would not be the case in C# as default form instances are a VB-specific feature. See here for more info.

How to handle an button click even from a .dll

Could someone please guide me as to how I would get this actually working? Currently it gives me errors about WithEvents - although simplified it shows the form - but I have no clue what that actually means. It's an toolbox I'm making just to allow the user to better interact with some of my other code.
All I need is the form visuals/guts to be custom, but then the code to be done within my application (which doesn't have visual editing capabilities).
Is this a case where I need to use interfacing/partial classes/inheritance, or can this easily be accomplished with just some minor tweaking to what I have?
(Form created in visual studio form designer and then changed to a class library. Application code written in Autodesk Inventor "rule" environment)
Thanks!
Application code:
AddReference "C:\Users\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\bin\Release\SectionSymToolBox.dll"
Imports System.Windows.Forms
Public Class SectionSymRule
'Public dlg As New System.Windows.Forms.For
Public Shared ToolBox As New SectionSymToolBox.SectionSymToolBox
Dim WithEvents EClass As New EventClass
Sub Main()
ToolBox.Show()
End Sub
End Class
Form code:
Public Class SectionSymToolBox
Private Sub Main()
End Sub
Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Swap Symbols
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Flip Symbol
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Flip Text
End Sub
Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'<
End Sub
Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'>
End Sub
End Class
Is this what you are looking for?
AddHandler ToolBox.Button1.Click, AddressOf HandlerMethodHere
Where HandlerMethod is a method that has the same signature as Button1.Click so for example:
Private Sub HandleButton1Click(sender As Object, e As EventArgs)
'Code here
End Sub
Obviously, replace Button1 with the name of the button.

Editing button properties from a module

I am just starting out with Visual basic .Net.
I can't seem to figure what's the scope of button properties like button.text. Can they be used outside the button_click event sub? And if so, how?
How can I modify button properties from a module in real time when a certain condition is met?
I'd surely appreciate some guidance and an example, if possible. Thanks.
Just as quick sample, I don't suggest doing something like this
I have 2 forms open, Form2 and Form3. Each form has a button on it.
I also have a Module, called MyModule
Public Class Form2
Public Sub ChangeButtonText(ByVal s As String)
Button1.Text = s
End Sub
End Class
.
Public Module MyModule
Sub ChangeForm2Btn()
Form2.ChangeButtonText("LOL")
End Sub
End Module
From my Form3 I click the button and call the module function to change Form2 button's text
Public Class Form3
Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
MyModule.ChangeForm2Btn()
End Sub
End Class
You could pass a reference to the button to a sub in the module, and then call that sub from the form.
i.e.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChangeButtonText(Me.Button1, "Changed")
End Sub
End Class
Module modButton
Public Sub ChangeButtonText(ByRef Button As Button, ByVal Text As String)
Button.Text = Text
End Sub
End Module

How can I have a button to minimize the form and another to exit without writing the same code in every form?

I made a borderless form so I created my own exit and minimize buttons. It works, but I wanted to know if there's a way to avoid having to write what each button does in each form. I've tried to create a module but I never learnt how to use that, and I don't know how to make it work, as I can't use "me.Close" and those things... anyone can help me?
You can do it with Inheritance
Inheriting a winForm In Vb.Net
Public Class CustomerEditForm
Inherits VBInheritanceSample.DataEntryBaseForm
Inheriting a winForm In C#
public class CustomerEditForm :
CSInheritanceSample.DataEntryBaseForm
Now, for a complete example similar to your case, please visit this tutorial called: Understanding Visual Inheritance in .NET.
In this tutorial you will learn to create a winForm with buttons and events. After that, it shows you how to inherit to another winForms.
Well, this is just one way, I think people can tell you another good idea.
You need to change the me to sender."so if you call it on form2 , its Me.Close for form2"
In form1 you add the code for exit and minimized in a public sub.
Public Sub MinimizeForm(sender As Object)
sender.WindowState = FormWindowState.Minimized
End Sub
Public Sub ExitForm(sender As Object)
sender.Close()
End Sub
From form1 you call it like this :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.MinimizeForm(Me)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.ExitForm(Me)
End Sub
Then from form2 and all the other forms you call it like this :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.MinimizeForm(Me)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Form1.ExitForm(Me)
End Sub

Switching between different forms

I have some forms in my application and need to switch between them. I dont want to open another form over other form, in short I want the user to get a feel that he is working on one form only. Right now I am doing something like this but this is not what I want
Public Class Form1
Dim fo As Form2
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.TopLevel = False
Me.Panel1.Controls.Add(Form2)
Form2.Show()
End Sub
End Class
I also need to send data to other form like list of some class.
Look into UserControls(Composite Controls). They will allow you create a custom layout with events and properties and add it to your panel. I have used this to swap in/out edit pages for my applications.
Here is a very simplistic example:( 1 Form, 2 Panels, 2 UserControls)
Form1
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Private Sub UserControl11_SendText(value As String) Handles UserControl11.SendText
UserControl12.SetText(value)
End Sub
Private Sub UserControl12_SendText(value As String) Handles UserControl12.SendText
UserControl11.SetText(value)
End Sub
End Class
UserControl
Public Class UserControl1
Public Event SendText(ByVal Text As String)
Public Sub SetText(value As String)
Label1.Text = value
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
RaiseEvent SendText(TextBox1.Text)
End Sub
End Class
Have you considered using a TabControl? This will let you create different screens, and you (or the user) can switch between them easily.