Loading another form on button click

Cracken

Member
Joined
Sep 30, 2008
Messages
15
Location
England UK
Programming Experience
Beginner
Hi

Could anyone advise me how to call another form in the same application on a button click event.

I do not want to create a new form using
Dim frmNew As Form = New Form()
as it's already in the solution explorer.

If I type in the application name in the event handler I can see the form but I cant get it to fire, the closest code I could find was :

AlbosTemplateManager.frmLogin.ActiveForm.Show()

I get the error :
"Additional information: Object reference not set to an instance of an object."

Any advise would be very helpful.

Thankyou in advance.
 
It may well be in the solution explorer but to use it in your application you must create an instance of it. Which means creating a 'new' instance.

VB.NET:
Dim someForm as New formInSolutionExplorer
 
Thankyou

Thanks, that worked, here's the code if anyone would like it.

Dim frmNew As New frmLogin
frmNew.Show()

Also, can controls on the original form be accessed from the new form ?

Regards

Mark
 
If you create a new instance of a Form all the controls on it will be 'new' also. Each Form has it's own ControlCollection, even 'New' instances of the same type.
 
Hi

Thanks for the quick replys, I'm new to this and I a bit confused.
If I create an instance of the original form on the new form I can access the control how ?
 
Hi

Sorry, Should explain this a little better.

I have a text box on my main form "txtQuery" with the ReadOnly set to True
On a button click event to change the ReadOnly property it loads another form "frmNew" aka frmLogin from the solution explorer.

On frmNew (frmLogin) there is a text box and a button, when certian text is entered into this text box you click and another button, when the button is clicked on the new form I want change the ReadOnly property for txtQuery on the main form to True.

Heres the code I have so far:

Main Form

Private Sub frmTemplateManager_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtQuery.ReadOnly = True
btnQuery.Visible = False
txtTemplateID.Visible = False
RefreshGrid()
End Sub

Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

Dim frmNew As New frmLogin
frmNew.Show()
End Sub

New Form

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
Dim txtQuery As TextBox
Dim frmNew As frmTemplateManager
frmNew.ControlCollection(txtQuery)
txtQuery.ReadOnly = False
End Sub


Hope this explains it.

Thanks

Mark
 
All you need is to pass a pointer to that textbox in your second form. You can do this by passing it in in the second forms constructor.

VB.NET:
Public Sub New(byRef Tb as TextBox)

// Assign the tB variable to the reference of the readonly textbox we pass in
Me. tB = Tb
End Sub

Have a private member variable in your class

VB.NET:
Private tB as TextBox;

Then you can manipluate the Textbox as you wish by referencing the tB member.

VB.NET:
tB.Readonly = False


You could also accomplish this with Properties, Global variables and probably a hundred or so other ways. The .Net way would prabably be thorugh Properties.. I prefer the pass an instance of the object so I have complete control over what happens to it.
 
Thnakyou vis781, that worked.

I'm still a newbie at coding so it took a while to work it out.
Here is the code for any other newbie reading this thread.

On the first form pass the object referance you want the other form to see:

VB.NET:
 Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        Dim frmNew As New frmLogin([COLOR="Red"]txtQuery[/COLOR])
        frmNew.Show()
    End Sub

On the second form in 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 Sub

    [COLOR="Red"]Public Sub New(ByRef Tb As TextBox)
        Me.new()
        Me.textboxQuery = Tb
    End Sub[/COLOR]

Add the private member just under the last bit of code:

[COLOR="red"]Private textboxQuery As TextBox[/COLOR]

And the final bit:

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

        [COLOR="red"]If txtLogin.Text = "Admin" Then
            textboxQuery.ReadOnly = False
            Me.Close()
        End If
    End Sub[/COLOR]
Thanks again, very helpful and very quick response, I'll be back !

Mark
 
Last edited by a moderator:
Back
Top