hi guys. i have a quick question. I have a form with a command button that when i click i want to load a new form. it was easy in vb6 but i can't figure it out in vb.net
I'd love some help with this one guys.
Printable View
hi guys. i have a quick question. I have a form with a command button that when i click i want to load a new form. it was easy in vb6 but i can't figure it out in vb.net
I'd love some help with this one guys.
Just create it and then show it,
Code:Dim frm As New Form2
frm.show
wes4dbt is correct, but you should be aware that the Form class implements IDisposable and should be disposed after you access it. Here is documentation on the subject: https://learn.microsoft.com/en-us/do...em-idisposable
If you wanted to display it as a modal then wrap the form creation in a using statement (documentation) and call the ShowDialog method (documentation):
If you wanted to display it without being a modal, then you can dispose of it manually after the form closes by calling the Dispose method (documentation). You can do this in the Form's FormClosed event (documentation):Code:Using frm = New Form2()
frm.ShowDialog()
End Using
Code:Dim frm = New Form2()
AddHandler frm.FormClosed, Sub(sender, e)
DirectCast(sender, Form).Dispose()
End Sub
frm.Show()
Using frm = New Form2()
frm.ShowDialog()
End Using
just keeps bringing up the original form.
Let me explain, i have a form with a dropdown.box with form2 as an item
i need to Load form2 when a certain item is selected.
i figured it out. i was loading the wrong form. thank you so much for your help!
I don't understand. If Form1 holds the dropdown, when the item is selected it should create a new instance of Form2 and show it.
Attachment 190942Code:Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim selectedItem = ComboBox1.Text
If (selectedItem.Equals("Form2", StringComparison.OrdinalIgnoreCase)) Then
Using frm = New Form2()
frm.ShowDialog()
End Using
End If
End Sub
End Class
Attachment 190943
Then that code would be located in the SelectedIndexChanged event for the dropdown. I'm not sure what you meant by it just bringing up the original form. The code shown will create an instance of Form2 and display it. How is that the original form? I expect that there is a miscommunication here.
Cross posted, glad you got it resolved.
That is unnecessary. When a form is displayed by calling ShowDialog, closing it does not dispose it, so you actually do have to dispose it explicitly or via a using block. When you display a form by calling Show though, closing it actually does dispose it. It may well have been done that way specifically because having to write code to dispose forms that might even outlive their caller would be cumbersome. Less so now with the advent of lambda expressions but back in the .NET Framework 1.0 days, definitely so.
I was not aware of that. It doesn't look like that is documented anywhere in the Form's Show method (here), do you know where I can find some more information on that?
It's actually (kind of) noted on ShowDialog
Looks like there's more info about it in the Close method.Quote:
Originally Posted by MSDN
-tgQuote:
When a form is closed, all resources created within the object are closed and the form is disposed
I found this in the Reference Source. Didn't seem to matter if it was Show or ShowDialog.
// MSDN: When a form is closed, all resources created within the object are closed and the form is disposed.
// For MDI child: MdiChildren collection gets updated (VSWhidbey# 368642 & 93550)
Dispose();
I don't want to derail this member's thread, so I'm going to open a new one.