|
-
Mar 28th, 2024, 02:44 PM
#1
Thread Starter
Lively Member
loading forms
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.
-
Mar 28th, 2024, 03:18 PM
#2
Re: loading forms
Just create it and then show it,
Code:
Dim frm As New Form2
frm.show
-
Mar 28th, 2024, 03:31 PM
#3
Re: loading forms
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):
Code:
Using frm = New Form2()
frm.ShowDialog()
End Using
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:
Dim frm = New Form2()
AddHandler frm.FormClosed, Sub(sender, e)
DirectCast(sender, Form).Dispose()
End Sub
frm.Show()
Last edited by dday9; Mar 28th, 2024 at 03:36 PM.
-
Mar 28th, 2024, 03:41 PM
#4
Thread Starter
Lively Member
Re: loading forms
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.
-
Mar 28th, 2024, 03:46 PM
#5
Thread Starter
Lively Member
Re: loading forms
i figured it out. i was loading the wrong form. thank you so much for your help!
-
Mar 28th, 2024, 03:49 PM
#6
Re: loading forms
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.
Code:
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 190942
Attachment 190943
-
Mar 28th, 2024, 03:49 PM
#7
Re: loading forms
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.
My usual boring signature: Nothing
 
-
Mar 28th, 2024, 03:56 PM
#8
Re: loading forms
Cross posted, glad you got it resolved.
-
Apr 1st, 2024, 10:22 PM
#9
Re: loading forms
 Originally Posted by dday9
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:
Dim frm = New Form2()
AddHandler frm.FormClosed, Sub(sender, e)
DirectCast(sender, Form).Dispose()
End Sub
frm.Show()
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.
-
Apr 2nd, 2024, 08:40 AM
#10
Re: loading forms
 Originally Posted by jmcilhinney
... When you display a form by calling Show though, closing it actually does dispose it ...
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?
-
Apr 2nd, 2024, 08:48 AM
#11
Re: loading forms
It's actually (kind of) noted on ShowDialog
 Originally Posted by MSDN
Because a form displayed as a dialog box is hidden instead of closed, you must call the Dispose method of the form when the form is no longer needed by your application.
Looks like there's more info about it in the Close method.
When a form is closed, all resources created within the object are closed and the form is disposed
-tg
-
Apr 2nd, 2024, 10:30 AM
#12
Re: loading forms
 Originally Posted by techgnome
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();
-
Apr 2nd, 2024, 03:14 PM
#13
Re: loading forms
I don't want to derail this member's thread, so I'm going to open a new one.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|