How to call a form from another from's button click event?

ssfftt

Well-known member
Joined
Oct 27, 2005
Messages
163
Programming Experience
1-3
[RESOLVED] How to call a form from another from's button click event?

hi every one, I am new to here and new to VB.NEThere is an easy question:I have frmLogin and frmRegister. on frmLogin i have a button called btnReg.What can i do to display frmRegister and hide frmLogin by clicking on btnReg?plz help.Best Regards!
 
Last edited:
Re:

First declare an object for the form to display in button_click

VB.NET:
Dim form2 as new form2()

then show form2

VB.NET:
form2.show()

hide form1

VB.NET:
me.hide()

Regards:eek:
 
so....if I wanted to call same screen in another button click, i have to put "DIM form2 as new form2() " again?
 
yes, or you could Private frm2 As New Form2 at the top of the form's code window

then all you would need is frm2.Show in the button's click event

also note that if you do it this way, in Form2's Closing event be sure to put:
e.Cancel = True
Me.Hide
 
thx JuggaloBrotha. I am learning VB.NET by trying to build a thick client application. So what is the best way to do it? have Dim in every event or have it at the top? what are the trade-offs?
 
a variable (even form's are variables) can only be used in the immediate area that it's declared

meaning that if you declare a variable at the top of the form then everything in the form can use it

but if you declare a variable in a sub (like a button's click event) then it can only be used there, if you need to use it elsewhere in that form (say a different button, or perhaps a menu item) then you should declare it at the top so all the procedures can use it
 
In Module:
VB.NET:
Public myForm2 as Form2


In Form1:
VB.NET:
If myForm2 Is Nothing OrElse myForm2.IsDisposed = True Then
 myForm2 = New Form2
 myForm2.Show()
 myForm2.Activate()
Else
 myForm2.Show()
 myForm2.Activate()
End If
 
ssfftt said:
thx JuggaloBrotha. I am learning VB.NET by trying to build a thick client application. So what is the best way to do it? have Dim in every event or have it at the top? what are the trade-offs?



basically it depends on the usage of the variable, like Juggalo said. Ideally u should declare those variables at the top (global for project) only if u really need to access their values in various sections of the project. But if the variable is local to a specific sub or event then its better to declare & use it locally. The trade-off is that having too many global variables will use more system resources, whilst local variables are immediately destroyed by garbage collection once they go out of scope thereby freeing up valuable resources that may be needed by other parts of ur program.
 
Back
Top