Reapz Posted December 17, 2002 Posted December 17, 2002 A button on my Form1 opens Form2. Simple as that. In VB6 I was using.... Form2.Show(1) to show the form modally and the cancel button on Form2 would have the code... Unload Me This doing the same thing as clicking on the close icon. Form2 would appear, its Load event having been executed and then it would close when the appropriate button was clicked. If I wanted to open it again then it would do so exactly the same. Now in VB.NET I can't use Form2.Show(1) apparently I have to use... Form2.ShowDialog() ...if I want the form to open modally. So here we get to the first problem. If I use ShowDialog instead of Show then the Load event of the form doesn't execute. So I use Form2.Show() and put up with the fact the form isn't modal. The second problem is that Unload Me no longer exists and according to another thread here the equivalent is... Me.Close() This however causes problems when I click the button to execute the code Form2.Show() code again. It gives me the error 'Cannot access a disposed object named "Form2".' I can make the Cancel button Me.Hide() instead but if the user clicks the close icon then this error is gonna occur. Oh my god my head's about to explode. All I'm after is for Form2 to open modally AND execute its Load event AND NOT get that damned error when I try an open it again later. (Something I could do in VB6 in 2 lines containing 4 words, 1 integer, 1 pair of brackets and a full stop!) And if at all possible could someone tell me why Microsoft would take something as easy to use as VB6 and turn it into the complete and utter pig that is VB.NET? Grrrr.... :mad: Quote I'm getting the hang of this now... No really I am!
*Experts* Volte Posted December 17, 2002 *Experts* Posted December 17, 2002 Try this:'In Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frm As New Form2() frm.ShowDialog() End Sub 'In Form2 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MessageBox.Show("Wheeeeee!!! I am Form2_Load's code!") End Sub Quote
Reapz Posted December 17, 2002 Author Posted December 17, 2002 (edited) Ok the bit in Form1 is the same as that already and I commented out everything else in my Form2_Load and added your line. The Load event still didn't execute with ShowDialog! :( I forgot to mention the disposed error doesn't occur with ShowDialog. Edited December 17, 2002 by Reapz Quote I'm getting the hang of this now... No really I am!
*Gurus* divil Posted December 17, 2002 *Gurus* Posted December 17, 2002 Why use the Load event at all? Why not put initialization code in the form's constructor after the InitializeComponent call? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Reapz Posted December 17, 2002 Author Posted December 17, 2002 Pardon? Quote I'm getting the hang of this now... No really I am!
Reapz Posted December 17, 2002 Author Posted December 17, 2002 Oh I see. Yeah I'll give that a try. Cheers! :) Quote I'm getting the hang of this now... No really I am!
*Experts* Volte Posted December 17, 2002 *Experts* Posted December 17, 2002 (edited) Well, initialization code would fire on the Dim frm As New Form2() line, instead of on the showing of the form. In this case, that wouldn't really matter because they are right beside each other, but in some cases, it could throw some things off. But to try it, expand the 'Windows forms designer generated code' region, and look for the constructor: Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End SubAnd then try adding your Form2_Load coad underneath of that second comment. Edited December 17, 2002 by Volte Quote
*Gurus* Derek Stone Posted December 17, 2002 *Gurus* Posted December 17, 2002 (edited) Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add your code here <-<-<-<- End Sub Edited December 18, 2002 by Derek Stone Quote Posting Guidelines
Reapz Posted December 18, 2002 Author Posted December 18, 2002 Cheers guys! I found that and it now works perfectly. I get to use ShowDialog so the form is modal and I don't get that annoying error. Cool! :) Quote I'm getting the hang of this now... No really I am!
*Experts* Volte Posted December 18, 2002 *Experts* Posted December 18, 2002 Just remember that the constuctor and Load event are not the same. Load fires before the form is shown for the first time, and the constructor is fired when the form is initialized (Dim frm As New Form2). Quote
*Experts* Nerseus Posted December 18, 2002 *Experts* Posted December 18, 2002 And there should be NO reason why the _Load event doesn't fire when showing a form modally. It works perfectly fine for me... -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.