Solved

Declaring Variables Load and Unload Forms in VB.net

Posted on 2004-09-15
15
622 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:GarethWham
  • 8
  • 6
15 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 12066215
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
0
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12068359
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
0
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12068586
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.
0
 

Author Comment

by:GarethWham
ID: 12074601
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?
0
 

Author Comment

by:GarethWham
ID: 12074627
Incidently, I know how to add a module and that the declerations are to be entered there
0
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12075421
"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
0
 

Author Comment

by:GarethWham
ID: 12082989
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
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12083403
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
0
 

Author Comment

by:GarethWham
ID: 12083672
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
0
 
LVL 5

Accepted Solution

by:
WesleySaysHi earned 225 total points
ID: 12083823
Well, you have to work a little more. In the click event you have now the Dim statement:
            Dim fpubs As New Form2()
I told you to place it there in order for you to see some immediate result, but it shouldn't be there!!!
You must erase this line of code from the method and place it at class level.
Go back to the button's click event and complete the following steps:
   1. Double click the button on "green1" to enter the click event handler;
   2. Remove the Dim statement out of the method and place it at class level;
   3. Make your click event look like this, and I will explain you in the following post what the code means:

      Private fpubs As Form2

      Private Sub Button1_Click(ByVal Sender As Object, ByVal e as EventArgs) Handles Button1.Click
         If IsNothing(fpubs) = False Then
            If fpubs.IsDisposed = False Then
            'do nothing
            Else
                fpubs = New Form2()
                fpubs.Show()
            End If
         Else 'The form is nothing
            fpubs = New Form2()
            fpubs.Show()
         End If
      End Sub
0
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12083995
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
0
 

Author Comment

by:GarethWham
ID: 12084197
Ok,

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

0
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12084617
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
0
 
LVL 5

Expert Comment

by:WesleySaysHi
ID: 12091640
You still have problems?????
Just wondering....

Wesley
0
 

Author Comment

by:GarethWham
ID: 12100329
Thanks for all your help its working fine now.

Regards,

Gareth
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordsWithoutList  challenge 24 75
matchUp  challenge 9 71
How to split this in C++ 4 77
creating threads in delphi 1 54
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now