Navigating Forms

G

Gary

I have a project with three forms. Form1, Form2, and
Form3. On Form1 I have the following:

Dim Form2 as new Form2
Dim Form3 as new Form3

Let's say once I get to Form3, I want to return to
Form2. If I put a Form2.Show on a button, and Dimension
Form2 as new Form2, I get an error because Form2 has
already been defined on Form1. Is there an easy way to
navigate between forms without using MDI forms? I am a
newbie and was wondering how to do this.

Thanks,

Gary
 
K

Klaus Löffelmann

Gary,

An instance of a form is always nothing else but an instance of any other
object - you only Dim it once! Meaning: If you want to use it again, simply
show it without Dim.

Klaus
 
G

Guest

Klaus,

Can you give me an example? I did try and remove the Dim
statement from Form2 and Form2 but when I ran the
program, I got build errors.

On Form2 in a button I entered the following code:

Form1.show

I then got the blue Squiggly line under the code letting
me know that it's an error. On Form1 I had Dim Form2 as
new Form2, but it seems that this isn't global through
the project. Can I Dim the forms in a module? I tried
this before posting but got an error.

Can you show exact statements of how to do this? Any
help would be appreciated. Remember, I am just beginning
the VB .NET and need all the help I can get.

Thanks,

Gary
 
K

Klaus Löffelmann

In a nutshell:

If you have designed a form, it's not directly usable. Yoe see, in .NET
everything is an object, and that applies for forms, too. Before you can use
any object (that is, as long they are not value types like Integer, String,
DateTime, etc.) you have to instance it, thereby calling its constructor
with *new*.

So lets say you designed a form named frmTest. Before instancing it you have
to define an object varibale, like this:

Dim instanceOfFrmTest as frmTest.

This variable is just a placeholder for a future reference to the actual
form data. It's not instanced yet. To actually instance it, you code:

instaceOfFrmTest = New frmTest.

From now on you can use the form as often as you want to.

You show it modally with:

instaceOfFrmTest.ShowDialog()

Modal means, that you give the whole program control to the form. The
program logic sort of stops at the ShowDialog, and waits till the form
closes - for example due to some user interaction. Once the forms hides,
closes or disposes itself, the program continues at that position.

If you want to run both instances of the forms simultaneously (one instance
is already running, that would be the main form the program started with),
you call it with

instanceOfFrmTest.Show()

In that case, you don't give away program control at the time exclusively to
the form.

By the way: To shorten things up, you can do the defining and instacing of
an object in one line:

Dim instaceOfFrmTest as New frmTest.

But for every object you use, you do that only once.

About the modifiers: If you define a variable in a procedure (a sub, a
property or a function), the variable is only valid in that procedure. It's
not valid outside. If you want its scope to be in the whole form class, you
define it right after the class definition.

However, I suggest you read about objects and form, provided by the VS-Help.
It'll let you develop programs quicker and more reliable, once you
understood the basic concepts behind it.

Hope this helps,

Klaus
 
G

Gary

Klaus,

Thanks for the explanation, it helps alot. I did create
a new instance of Form2 and Form3 within Form1. I used
the following code:

Dim Form2 as new Form2
Dim Form3 as new Form3

This code is placed just after the Public Class Form1. I
think this is the correct place for it. If not, please
correct me.

I can click on a button to bring up Form2 and Form3 from
Form1. No problem. Maybe my issue is how to make the
instance of Form1, Form2, and Form3 global so I can
access them from any form. If I click on a button on
Form1 to activate and show Form3 for example, it works.
When Form3 is active then I click on a button to go to
Form2, that is where the problem is.

On Form2 I have a button: Button1_Click and it has

Form1.ShowDialog

This gives an error.


How do I make Form1 and Form3 available to Form2? I
tried your suggestion of
calling Form1.showdialog() from Form2 but I get an
unhandled exception exception of
type "System.NullReferenceException". I still don't
understand.

From Form1, calling the other forms works like a charm.
I understand that. Maybe I can't do what I want to do,
or there is a better way, like using MDI forms :)

Thanks loads,

Gary
 
K

Klaus Löffelmann

Gary,

don't forget, that every object's instace is only accessible within the
class you defined it in. So if you called the instance of form2, you have to
define the object varibale for form3 in form2 for being able to call it from
there.
Although, there is a possiblity to define all three forms in a way so you
can access them in every other class' instance of your application. But,
actually, you don't want to do it. Object-oriented programming is all about
encapsulating program code and data. So, try to avoid application-wide
variables at any cost.

Klaus
 
G

Guest

It because in form 1 you are dimming up instances of forms 2 and 3


dim frm2 as form2

but when you call just Form1.ShowDialog

there is not instance associated with the request and so you get the null ref
exception. What "instance" to show?

What you may want is
dim frm1 as form1
frm1.showdialog

in forms 2 and 3

hth
Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top