Question How do I code for multiple forms?

Nouveaish

Member
Joined
Dec 3, 2012
Messages
10
Programming Experience
Beginner
I'm having trouble with getting multiple forms to pop up in the order I'd like. I have a welcome form, and then three buttons for a user to choose. Based on what button the user clicks on, the corresponding window form should open. But what is happening during debugging is the welcome form gets pushed to open second, and then all 3 of the other forms open consecutively. I don't want that to happen! I want assigned forms to correspond with the buttons, but I can't figure out how to code it :culpability:

I'm a beginner and while I'm able to follow along using my book, this is a project I'm doing from my own ideas so figuring out the "behind the scenes" is taking a little more time. I did write down the actions I want everything to take so I have some sort of pseudocode.
 
Here's what I'd like to have happen:

On the welcome form, there are three buttons. When the user clicks the first one, only one window form should open and ONLY that one. When the user clicks the second, the corresponding form should open and ONLY that one. Same with the 3rd form. The welcome form is mainly there to present a screen with the three button choices.

I have this and it's not working out so great:

Public Class frmWelcome

Private Sub frmWelcome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim FirstForm As New frmWelcome
End Sub

Private Sub radPurchaseSeeds_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radPurchaseSeeds.CheckedChanged
Dim SecondForm As New frmPurchaseSeeds
frmPurchaseSeeds.ShowDialog()
End Sub

Private Sub radGardenTools_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radGardenTools.CheckedChanged
Dim ThirdForm As New frmTools
frmTools.ShowDialog()
End Sub

Private Sub radSeedlings_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radSeedlings.CheckedChanged
Dim Fourthform As New frmSeedlings
frmSeedlings.ShowDialog()
End Sub
End Class
 
First things first, a RadioButton and a Button are two different things. If you use incorrect terminology then you're only going to confuse matters. In this scenario, it seems to me that Buttons would be a more appropriate choice.

Also, there's quite a bit of code there that serves no purpose. That whole Load event handler is useless. All it does is create an instance of the frmWelcome class that you never use. For that method to be executed you must have already displayed an instance of that class so why do you need another? The other three methods also create form instances that you never use.

What I would suggest is that you start by getting rid of those RadioButtons and replace them with Buttons. Then get rid of your Load event handler. Handle the Click event of each Button and, in each, call ShowDialog on the appropriate form's default instance, i.e. on the class name itself.
 
Okay, I so I got rid of the radio buttons and the load event handler. I was using them because my teacher wanted to see button and radio button usage, but for this the radio buttons might not be so appropriate.
 
If you want to use RadioButtons and Buttons then what you could do is use the RadioButtons to select which form to open but only open that form when the Button is clicked. You use a single Button to open a different form depending on which RadioButton is checked. That's how RadioButtons usually get used, i.e. the user makes a choice and then that choice gets used later on. You don't usually perform an action when a RadioButton is checked, other than perhaps something like enabling or disabling other controls based on that selection. If you want to initiate some process then that's what a Button is used for. The RadioButton would be used to configure that process but not to start it.
 
What are the consequences of working with Forms' base classes or instances? I've found some material that states that VB.Net Forms should always be instances of the base class ("Private MyForm1 as Form1 (...) MyForm1 = New Form1"), instead of VB6. But I noticed that calling Forms' base classes actually worked as well, so far at least. Thanks!
 
What are the consequences of working with Forms' base classes or instances? I've found some material that states that VB.Net Forms should always be instances of the base class ("Private MyForm1 as Form1 (...) MyForm1 = New Form1"), instead of VB6. But I noticed that calling Forms' base classes actually worked as well, so far at least. Thanks!

I don't really know what you mean. Any form is an instance of its own type, as is any object. If you mean using a variable of a less derived type to access it then generally you'd only do that if you needed to support more than one type derived from that base type, e.g. a form's MdiParent property is type Form because it can be any type of form.
 
I don't really know what you mean. Any form is an instance of its own type, as is any object. If you mean using a variable of a less derived type to access it then generally you'd only do that if you needed to support more than one type derived from that base type, e.g. a form's MdiParent property is type Form because it can be any type of form.

I think I didn't make myself clear, sorry... What I've read is that if you need to show a second form from the main application form, you should use:

Dim oForm As FormName
oForm = New FormName()
oForm.Show()
oForm = Nothing

(this code is from this site: Opening, Closing, and Hiding Forms with Visual Basic .NET - For Dummies)

But since my first attempts of coding in VB.NET, it always worked if I simply used:
FormName.Show()

or
FormName.ShowDialog()


The issue behind my curiosity here is that in the application I'm developing, there are resources in the main form (routines, data access and web navigation) that need to be used by secondary forms, and I was having problems - which I woudn't be able to accurately describe - when trying to open these secondary forms modally with .ShowDialog,

But I've got a nice flow when I declared variables for each one of them, using "Private WithEvents" in the main form. So when I need to open one of them, I create a new instance of it, show the form, hide the main form, which is not closed, just stays invisible offering its resources to the secondary forms, and remain waiting for the .FormClosed event of these secondary forms, and then it reappears with "Me.Show".

That said, I wished to know what is implied on each one of these approaches. Because if each Form is a class, it was unclear to me why its methods like control's events handlers (instance methods, not shared methods) worked without instancing the class in any part of the visible code. I assume IDE designer makes a lot of work into the hidden part of the code, but I couldn't say I've understood it well.

Thank you very much.
 
FormName.Show()
it was unclear to me why its methods like control's events handlers (instance methods, not shared methods) worked without instancing the class in any part of the visible code
That is using the default instance of that form. When you use the New keyword you are creating a new instance, and you also need to manage the reference to that if you need to access it elsewhere.
there are resources in the main form (routines, data access and web navigation) that need to be used by secondary forms
Maybe they don't belong there, or if they do, maybe you should pass them to where you need them rather than the other way around.
remain waiting for the .FormClosed event
If you're using ShowDialog that is not necessary, it is closed when that call returns.
 
When using multiple forms, is it appropriate or useful to have an exit button on each form to go back to the original? for instance in my program, I would like the user to choose from opening 3 forms, but when they are done with the form they should head back to the welcome form in order to end the program. Or does it make more sense to have a user click the close button to close a window?
 
When using multiple forms, is it appropriate or useful to have an exit button on each form to go back to the original? for instance in my program, I would like the user to choose from opening 3 forms, but when they are done with the form they should head back to the welcome form in order to end the program. Or does it make more sense to have a user click the close button to close a window?

I would say that that depends on the form. In my own projects, I would use buttons on dialogue boxes only, while a more self-contained window would rely on its own inbuilt Close button. There may be a bit of a grey area between the two but think about how Windows itself works and what types of windows have extra buttons with OK or Cancel or whatever and which ones you have to close using the title bar Close button.
 
Back
Top