Link to home
Start Free TrialLog in
Avatar of GarethWham
GarethWham

asked on

Declaring Variables Load and Unload Forms in VB.net

I have a number of forms which I need to navigate in my Vb.net project. I am not sure how to declare the variables to set this up. Nor I am sure how to load and unload forms once they have been declared.

I think this should be fairly straight forward. I think it may be a case of declaring the forms globally, then a click command to show one form and hide another,

Thanks
Avatar of Mike McCracken
Mike McCracken

I am not familiar with VB.Net but in VB forms are loaded via

    formName.Show

Unloaded by

    Unload formName

Variables can be declared on a form as global to the form or local to a module on the form
Variables can also be declared in modules that are sepate from the forms.  These can be global to the project or local to the module.

mlmcc
mlmcc guessed it right. The 'formName.Show' is used in VB as well as in VB.net
It would be interesting to know how long Microsoft will continue to support VB6 syntax in .NET.

VB is now fully object oriented and therefore you have to declare a variable of that type,

like this:

Dim frm As Form1
frm = New Form1
frm.Show()

or like this:

Dim frm As New Form1
frm.Show()

This is an example of a VB.Net project with form1 and form 2 and a button on form1 to launch
form2. Here's the event handler for the Button1Click event:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim form2 As New Form2()

form2.Show()

End Sub


The best step by step description about using multiple forms in VB.NET is here:
http://www.vbcity.com/forums/faq.asp?fid=15&cat=Forms#TID23583


Regards,
Wesley
Forgot to mention:

After clicking the link I gave you, you have to wait a while till the page is redirected from the general questions of the forum to the page about Forms in VB.net
The page is big and might take some time to load on dial up connections. Read all and don't miss the zip files that appear as attachements in text, because they contain source codes you can download and use.
Avatar of GarethWham

ASKER

Thanks for your comments.

I am still not sure how to write the code to declare variables as global any ideas?
I have two forms one called green1 and one called fpub. Basically on fpub I have a command button which when clicked will open green1.

Can you help?
Incidently, I know how to add a module and that the declerations are to be entered there
"I have two forms one called green1 and one called fpub. Basically on fpub I have a command button which when clicked will open green1."

This means that you have the situation I described in my previous post. Hope it was helpful.

Now about variables and declaring variables!

Local variables:
The variables that you declare inside a function, procedure (you name it) will be local, which means they will be used only inside the code written for that part of your program and they can't be accessed by the rest of the program.  With other words these variables may only be read or modified while code is executing within the particular procedure.

Member variables:
If you "incidently know how to add a module..." then you probably know what a class is and what methods are. Assuming that you know this, I will tell you that there are variables that can be used from within any method of the same class. These variables are called member variables. Although they are "global" for some part of the program, they can't be used by the whole program, therefore they are not called global variables.

Global variables:
Global or public variables may be referenced from any class, method, procedure or function within the same project.  You must be careful when declaring these variables. Usually only a few variables are declared as global, most are declared as member variables.

There are some conventions, when declaring variables. It is very important not to mess up variables, because debugging a code at a later time is sometimes more difficult then re-writing it.

Global or public variables use the prefix "g", member variables use the prefix "m" and local variables don't use a prefix. For example:
Public gintLoop As Integer  ' Public variable
Private mstrName As String  ' Member variable
Dim boolOpen As Boolean ' Local variable

In VB.NET (unlike other languages) you have to add a module to your project and declare your global variables there.

Here is an example of declaring global variables. If you need to reset them somewhere in the program, then it is a good ideea to initialize them in a routine. Doing this, you will be able to reset them all just by calling the routine.

'declarations

public gName1 as String
public gName2 as String
..............
public gNameI as Integer
public gNameII as Integer
.............


public sub InitGlobalvariables()
     gName1 = ""
     gName2 = ""
........................
     gNameI = 0
     gNameII = 0
.......................
End Sub

When you need to reset your global variables, call the routine like this:

Call InitGlobalvariables()

I don't think there is more to tell about this subject, is it?

Regards,
Wesley
Apologies,

I am a

I have added a module then in the module put:

public gGreen1 as String
public gfPubs as String

On the form that represents Green1 i need a button that closes it and opens the form that represents fPubs.

Am I going in the right direction. Sorry to ask for further help but I am struggling at the moment.

Thanks
So, you declared 2 variables as strings in global scope: gGreen1 and gfPubs
You will be able to use them throughout the whole program!

Coming back to the forms now. You say:
"I have two forms one called green1 and one called fpub",
then you say:
"On the form that represents Green1 i need a button that closes it and opens the form that represents fPubs."
Only you know how you really named the forms! Anyway, I will use the names: "green1" and "fpubs" for your forms. This is what you have to do if you want that a button on "green" opens "fpub":

   1. On your form named "green1" put a button;
   2. Double click the button to get the click event handler;
   3. Put these 2 lines of code in the click event handler of the button:
         Dim fpubs As New Form2()
         fpubs.Show()
After you put the 2 lines of code in it, the click event handler should look like this:
      Private Sub Button1_Click(ByVal Sender As Object, ByVal e as EventArgs) Handles Button1.Click
         Dim fpubs As New Form2()
         fpubs.Show()
      End Sub
   4. Run your project, click the button a few times without closing the new form that pops up

Place a comment here after you complete these steps successfully and I will tell you how to continue. Ok?

Wesley
That works fine thanks, it cretaes a new instance of fPubs form, thanks.

I now need to hide the green form, if possible at the same time

Gareth
ASKER CERTIFIED SOLUTION
Avatar of WesleySaysHi
WesleySaysHi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is what the code does:
     1. checks if "fpubs" is Nothing, because if it is Nothing and you try to "Show()" it, you will get an error;
     2. checks if "fpubs" is Disposed. If "fpubs" is something (which means: Not Nothing), but disposed, calling the show method will cause an error;
     3. If "fpubs" is Not Nothing, and is also Not Disposed, then "fpubs" must be open, which means your code must 'do nothing'.
Else - the form is Disposed or Nothing and you must call the show method.

Run your code again, click the button several times and tell me what happens.

I'm still here, but not for long,

Wesley
Ok,

That displays fPubs form. How do I hide the green form?

Ok, let's go back to the old code (the short one!). When using the 'short code' you saw that each time when you were pressing the button on "green1" a new "fpubs" appeard. The 'long code' prevents this.

Let's hide now "green1" after "fpub" opens. In this particular case you can use the 'short code', because once "fpubs" opens, "green1" closes and the user can't press the button on "green1" again.

Here is what you have to do:

Go back to the first example and make the click event of the button on "green1" look like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim fpubs as New Form2()
    fpubs.Show()
    Me.Hide()
End Sub

Don't make a habit out of using this. It's bad programming. Use the Me.Hide() in the 'long code'.

Wesley
You still have problems?????
Just wondering....

Wesley
Thanks for all your help its working fine now.

Regards,

Gareth