how to close current form and open the next?

jalo

Freshman
Joined
Aug 9, 2005
Messages
28
kind of a beginner question - how do i get Form1 to close itself after the user presses a "NEXT" button, and Form2 to open up. I would also like to send a parameter to Form2...

i would also appreciate advice on better ways of implementing the following:
in Form1 i ask the user to enter some info and then i do a query to the db to get the relevant matches. there could be multiple matches. i display those in a listbox. the user selects the one right match he wants and then clicks the "NEXT" button which is supposed to close Form1 and bring up Form2, while sending the info for the user's choice to Form2. Thus i was thinking of using listbox.selectedItem() to get the user choice and send it as a parameter to Form2... any better ideas of implementing the whole thing?
 
To close the form, you can use Me.Close or Me.Dispose. That would work unless the form you are trying to close is the Startup form. If you close the startup form, it will exit your application. If you are trying to close your startup form, you could hide the main form, but I'm not sure if thats good programming practice. And about sending data to another form, You could just pass whatever data you want in through the constructor. I don't see why that would be a problem. I've done it before on several occasions. You could even take the data you want passed and save it into a Class or Module. However, most would frown upon the use of Modules. Good luck.

-=Simcoder=-
 
thanks!

so i guess from what you are saying that i cannot close the startup form and the following screens would just go on top of it?

i am also not able to figure out what syntax i would use to bring up Form2... i tried something like
Code:
Dim nxtScreen As Form2 = New Form2
or
Dim nxtScreen As Form2 = Form2.ActiveForm

but it doesn't work................... pls help...
 
last question - how would i pass a parameter to Form2?
(in java there would be parenthesis for the constructor, but no parens here...)
 
You pass them in the same way you do with Java or any other language. Here is a link that should be quite helpful.
Constructors

Basically you need to go into the Windows Generated Code and you should see something ilke that looks like this

Visual Basic:
  Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

You will copy and paste that code and then edit the part that says

Visual Basic:
Public Sub New()

And you will edit it with the parameters you want to pass in like this:

Visual Basic:
Public Sub New(ByVal Str As String)

You also need to add an initialization statement so the Constructor when done should look like this:

Visual Basic:
   Public Sub New(ByVal str As String)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Message = str
    End Sub

Note:The variable message was a global variable I declared at the top of my form

When you call your form from another form, you would pass in a string value using code something like this

Visual Basic:
Dim nxtScreen As New Form2("Hello") 
nxtScreen.ShowDialog()

I hope this code and the link proves to be useful. Sorry if you don't understand or if its confusing, just let me know. I'm the type of person who learns better by step by step example rather than reading some long tutorial and trying to go from there. Let me know.

-=Simcoder=-
 
this is absoluely helpful -thanks! looks exactly like java - i guess i was confused by the lack of parenthesis for the call to the default constructor.
 
Back
Top