Showing another form in VB - vb.net

I want to make a program with many forms in VB, I need to make a button that opens another form.
form2.open()

Do not use the default instance of the form(as has been shown in the other answers), instead
Dim foo As New Form2
foo.Show()
'or
'foo.ShowDialog()
If you want to show multiple instances of the same form this
Form2.Show()
Form2.Show()
will not work.
This will
Dim foo As New Form2
foo.Show()
Dim bar As New Form2
bar.Show()

Simply the code will be
form2.show()

You can open a form inside yours as a MDI child, or you can "open" your new form in a new window with this methods:
form2.Show()
form2.ShowDialog()
Read here and here about them.

As SysDragon already mentioned,
.Show()
.ShowDialog(owner)
are the methods you can use.
If the calling Form should be frozen until the new opened one is closed again, then you should use ShowDialog.

Related

vb.net Closing multiple form and open another form

I have 2 forms open, my current form is in dialog box. Now I want to close that 2 forms and open another form.
Dim frmStg2 As New frmStageTwo
Dim frmStg1 As New frmStageOne
frmStg2.Show()
Me.Close()
frmStg1.Close()
But this code seems not working the frmStageOne is still open and showing frmStageTwo but I can't view that form.
This is what happen
You are trying to close a form instance that has not yet been opened.
When you use the New keyword you are declaring a new instance of an object (a Form in this case) which has it's own version of the base class's properties and fields.
In order to close an already existing instance of the form you can loop through the Application.OpenForms property.
For Each frm As Form In Application.OpenForms
If frm.GetType() Is GetType(frmStageOne) Then
frm.Close()
End If
Next
You could actually make it a method:
Public Sub CloseFormsOfType(ByVal TargetForm As Form)
For Each frm As Form In Application.OpenForms
If frm.GetType() Is TargetForm.GetType() Then
frm.Close()
End If
Next
End Sub
...and just call:
CloseFormsOfType(frmStageOne)

Why does my new form move behind the opener?

I'm not going to post a bunch of code here since I do not think it is a code issue.
Here is a link to my original question where I have shown the code if you are interested. Code
Just as a test I created a blank form window (Form1.vb) and no code gets passed to it and no code runs when it opens. If I do Form1.Show() from a MenuStrip Control or a Button Control, the window opens and stays on top. Now if I do Form1.Show() from a TreeView Control, the window opens and goes behind the window with the TreeView Control.
So my question is, what is different about the TreeView opening a form vs a button or other control?
I am using the basic VB TreeView Controll, and the new form is being called in AfterSelect method for the TreeView.
The AfterSelect works if you use your keyboard navigation to select a node, but it doesn't work when you use the mouse because the mouse capture is forcing the parent form to remain in focus. You would have to run your code after the AfterSelect event:
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) _
Handles TreeView1.AfterSelect
Me.BeginInvoke(New Action(Sub()
Dim f2 As New Form2
f2.Show(Me)
End Sub))
End Sub
Use the Form.Show(parentForm) option, this will always put the new form on top of the old one.
have you tried Form1.ShowDialog() ? or if you don't want to show it as a dialog you should use:
Form1.Show()
Form1.BringToFront()

Click on button to open a new form?

I'm trying to create a text-based game where the user selects buttons and that will lead to other parts of the game, right now I need the program to close the old Form and the new form must be placed in where the old one was.
You can try something like this :
Dim form2 As New Form2
'set start position to manual
form2.StartPosition = FormStartPosition.Manual
'set form2 location the same as current form
form2.DesktopLocation = Me.DesktopLocation
form2.Show()
Me.Close()
try this...
me.close()
Dim SecondForm As New frmSecond
SecondForm.Show()

Working With multiple instance of a windows form in vb.net

I have three forms (form1, form2, form3) in a windows application using vb.net
form1 has a button (button1). Onclick of button1, I want to open form2 in such a way that it can also open multiple times. I achieved this with the code below:
Dim myForm As New Form2
myForm.Show()
Now form2 has a button (button2), and a label (label1). Onclick of button2, I want to open a single instance of form3 a dialog, so I have the code below:
form3.showdialog()
form3 has a textbox (textbox1).
My problem is that I want when I fill textbox1, I want the value to appear in label1 of form2 that opened the form3, I tried the code below, but it did not work:
form2.label1.Text = textbox1.Text
I need to update form2 (the last active one) once form3 has been closed
Can anybody help me out please?
When you go to show the Form3 as a dialog, you should be able to do:
Dim f3 As New Form3
f3.ShowDialog()
Me.label1.Text = f3.textbox1.Text 'Copy the value out of the dialog

How to reload windows form without closing it using VB.NET?

Can you please tell me how I can reload a windows form without closing it using VB.NET?
Put all your initialization code into a method and not the constructor or the Form.Load event and just call that method. This can also include the designer-generated InitializeComponent() method which sets up all controls on the form. You should remove all controls on the form as your first action in that method, though.
I know that it is late, but useful
Me.Controls.Clear() 'removes all the controls on the form
InitializeComponent() 'load all the controls again
Form1_Load(e, e) 'Load everything in your form, load event again
You cannot do that.
Why do you want to reload a form? Do you want to clear all input controls or something like that? The simplest solution might be to just do the clearing by hand.
Alternatively, you can put all your controls into a user control container. Then just instantiate that user control on your form. If you want to reload your form content, you now just need to remove and re-instantiate the user control.
Application.Restart()
Shuts down the application and immediately opens a new instance.
try using Me.Refresh() it worked with me
Me.Controls.Clear() 'removes all the controls on the form
InitializeComponent() 'load all the controls again
main_Load(e, e)
MsgBox("Thank you for sending report", vbInformation, "")
Refresh()
I know. I'm late to the party. But, maybe it works for who need it.
If you shows a form from other form with .ShowDialog the code after this call will be executed when the form that has been showed is closed. Example:
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Dim form2 As New Form2
form2.ShowDialog
UpdateThisForm()
End Sub
Then it's not necessary update the form from the other form (Form2).
Execute the following line:
Form_Load(sender, e)