Switching between different forms - vb.net

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.

Related

Switching forms in vb.net

I am a volunteer for the National Park Service trying to convert an interactive display originally created 20 years ago in a language called ToolBook into Visual Basic. The program consists of several projects under a single solution. The starting project, called "MainMenu", can be thought of as a library, with buttons that bring up “books.” The project called Geology is an example “book” and GeologyMenu can be thought of as the index of a book. The buttons on GeologyMenu connect to “chapters” that explain and show examples of geologic processes in the park. The “chapters” are within the project “Geology” and work fine within the project. All forms used in the program have timers that allow the program to re-set itself to MainMenu when not in use.
In a previous post, with the help of Idle Mind (thank you again), the following code for works fine for going from MainMenu to GeologyMenu and in the reverse direction as long as no button is pushed on GeologyMenu. However, if I go to a “chapter” I can no longer get back to the MainMenu from the GeologyMenu. Here is the relevant code:
MainMenu
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
TimerMain.Stop()
formNew.Show()
Me.Hide()
End Sub
Private Sub formNew_FormClosed(Sender As Object, e As FormClosedEventArgs)
lblTime.Text = 8
TimerMain.Start()
Me.Show()
End Sub
GeologyMenu
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblTime.Text = 6
TimerGeologyMenu.Enabled = True
Me.Show()
End Sub
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
TimerGeologyMenu.Stop()
frmErosionP01.Show()
Me.Hide()
End Sub
The code below for takes the viewer to the Erosion “chapter”
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
TimerGeologyMenu.Stop()
frmErosionP01.Show()
Me.Hide()
End Sub
Erosion “Chapter” . This is the code for the button on every form in Erosion that takes the program back to GeologyMenu
Public Class frmErosionP02
Private Sub BtnGeologyMenu_Click(sender As Object, e As EventArgs) Handles btnGeologyMenu.Click
My.Computer.Audio.Stop()
frmGeologyMenu.lblTime.Text = 10
frmGeologyMenu.TimerGeologyMenu.Enabled = True
frmGeologyMenu.Show()
Me.Close()
End Sub
The code for forms within Erosion takes me back to GeologyMenu, but then MainMenu won’t show when I close GeologyMenu and I don’t understand why or how to fix it. Thank you in advance for your help!
Simply, pass the previous menu/Form to the new one in a parameterized constructor and keep it in a class variable, then handle the Form.Closed event of the new menu to show the previous one.
Example for the relevant code:
Public Class frmMainMenu
Inherits Form
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New frmGeologyMenu(Me)
Me.Hide()
formNew.Show()
End Sub
End Class
Public Class frmGeologyMenu
Inherits Form
Private PreviousMenu As Form
Private Sub New()
InitializeComponent()
'...
End Sub
Sub New(prevMenu As Form)
Me.New()
PreviousMenu = prevMenu
AddHandler FormClosed,
Sub(s, e)
PreviousMenu.Show()
End Sub
End Sub
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
Dim frmErosion As New frmErosionP02(Me)
Me.Hide()
frmErosion.Show()
End Sub
End Class
Public Class frmErosionP02
Inherits Form
Private PreviousMenu As Form
Private Sub New()
InitializeComponent()
'...
End Sub
Public Sub New(prevMenu As Form)
Me.New()
PreviousMenu = prevMenu
AddHandler FormClosed,
Sub(s, e)
PreviousMenu.Show()
End Sub
End Sub
End Class

Best way to use a form as a GUI, but code it elsewhere

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

Creating form name and object name as parameters of a function

I'm trying to pass form name and object as parameters of my public sub because I have 2 forms and they are almost identical to one another.
For example I have richtextbox1 in Form1 and I also have richtextbox1 in Form2, I would like to create a public sub something like this.
Public Sub Sub1(ByVal frm_name As form, Byval obj_name As object)
frm_name.obj_name.Lines.Length 'Get the length of lines of that richtextbox
End Sub
Then when I want to call it
'Form 1 Codes
Public Class Form1
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(me, richtextbox1)
End Sub
End Class
'Form 2 Codes
Public Class Form2
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(me, richtextbox1)
End Sub
End Class
It is not working the way I want it, is there anything I can do for it to work?
Think about it. You are saying that you're passing the names of things but you're not. You're not passing the name of a form; you're passing the form itself. You're not passing the name of an object; you're passing the object itself. What is the actual point of this? It's to get the number of lines in a RichTextBox, right? So, write a method that takes a RichTextBox as an argument and then call it passing a RichTextBox.
Public Sub Sub1(rtb As RichTextBox)
Dim lineCount = rtb.Lines.Length
'...
End Sub
'Form 1 Codes
Public Class Form1
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(richtextbox1)
End Sub
End Class
'Form 2 Codes
Public Class Form2
Private Sub Tmr1_Tick(sender As Object, e As EventArgs) Handles Tmr1.Tick
Sub1(richtextbox1)
End Sub
End Class

Changing Controls of a form inside a panel

I am facing a weird problem
I have 3 forms: MainForm, Form1, Form2
MainForm has 1 Panel: Panel1
Form1 has 1 Label: NameLbl and Button: ChangeBtn
Form2 has 1 textbox: NameTxt and Button: SaveBtn
I used the following code to open form1 inside Panel1 in mainform
Panel1.Controls.Clear()
Dim FormInPanel As New Form1()
FormInPanel.TopLevel = False
Panel1.Controls.Add(FormInPanel)
FormInPanel.Show()
On ChangeBtn.Click Form2 opens as showdialog
I want NameLbl.text to change to NameLbl.text when SaveBtn is clicked But normal code doesnt work.
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
Form1.NameLbl.text=NameTxt.text
End Sub
What Should I do? Any suggestions? Given that i need to open the forms in panels for certain reasons.
Please keep in mind that this is just an example. I have multiple controls in Form1 which i want to change on form2.SaveBtn.click
I have also tried this but it does nothing
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If c.Name="NameLbl" Then
c.Text = NameTxt.Text
End If
Next
End Sub
Please Somebody tell me how it do it!
Form2 doesn't seem to have any connection back to Form1 or MainForm. You'd need to raise an event from Form2 which MainForm handles or another class handles and can pass to MainForm
Edit:
Sorry, I've just seen how you're calling Form2. There are lots of ways to get a value back from Form2 after calling ShowDialog(). One is to create a property called Result and check Result if ShowDialog() == DialogResult.OK. Something like the following.
Public Class Form2
Inherits System.Windows.Forms.Form
Public Property Result() As String
Get
Return m_Result
End Get
Set
m_Result = Value
End Set
End Property
Private m_Result As String
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Public ChangeBtn As Button
Public NameLbl As Label
Public Sub New()
Me.ChangeBtn = New Button()
AddHandler Me.ChangeBtn.Click, AddressOf ChangeBtn_Click
Me.NameLbl = New Label()
End Sub
Private Sub ChangeBtn_Click(sender As Object, e As EventArgs)
Dim form As New Form2()
Dim dr = New form.ShowDialog()
If dr = DialogResult.OK Then
Me.NameLbl.Text = form.Result
End If
End Sub
End Class
I'd like to add that if you plan on growing this application much larger you'll run into issues with maintenance. Look into some patterns for dealing with UI logic like MVC, MVP, MVVM if you're interested.
you can try this code:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
panel1.Controls(0).NameLbl.text=NameTxt.text '"0" is the index of forminpanel in panel1,maybe it need to change.
End Sub
Form1 is contained in Panel1, so you can not access it via
Form1.
Only MainForm is visible from Form2:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If TypeOf c Is TextBox Then
c.Text = NameTxt.Text
End If
Next
End Sub
Try this:
For Each form1 As Form1 In MainForm.OwnedForms.OfType(Of Form1)
Form1.NameLbl.text = NameTxt.text
Next
I've faced same issue and I've fixed with this
Dim f As FormInPanel
f = Form.Panel1.Controls(0)
f.transection = True
f.NameLbl.text=NameTxt.text

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