954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Button to open form with variable

Title probably makes no sense :)

I have a single picturebox button that I will be using to open new forms. Is there a way to set a variable and then have the button open the form name included in the variable

I really dont know where to start as every variable I set can't use the .show.

I'm guessing I have to set the variable to the current object but I'm lost.

Merovingian
Light Poster
40 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

You need to create a variable that will store a reference (if that's the right terminology, I'm not a VB pro) to the form you want to show. The variable should be of type Windows.Forms.Form .

In the following example, Form2 would be the name of the form you want your picturebox to open:

' This represents whatever variable you use to select a form.
    ' Obviously it would normally be set based on some other factor.
    Dim whichForm As Integer = 1

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

        'This variable is what you will use to reference and show the form
        Dim formName As Windows.Forms.Form

        ' I used a Select Case to choose
        Select Case whichForm
            Case 1
                formName = Form2
        End Select

        ' Make sure to check that this is actually set -- I left it out.
        formName.Show()

    End Sub

Hope that helps!
- Z

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

you could create an overloaded New function in your form just like

Public Sub New (byval frmName as string)
'what ever you wanna do with the name
End Sub


and you call that form by doing

Dim newFrm as New Form2(myName)
newForm.Show
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

@EvolutionFallen works great! thanks man.

Actually really simple when you get down to it, I was making it much more complex than it needed to be.

@GeekByChoice, I'll check out your suggestion too.

Thanks again!

Merovingian
Light Poster
40 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

then please mark the thread as solved ;)

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

Glad to help =)

EvolutionFallen
Junior Poster
198 posts since Aug 2009
Reputation Points: 40
Solved Threads: 31
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You