Easy But Stupid Question

  • Thread starter Thread starter Trev
  • Start date Start date
T

Trev

Hey,

I just want to know (i know i should know this) how can i
show up another form?

I have 2 forms and the first one has a button on it that
when i click the button i want the second form to show up
(and the first form disappear), and i also want another
button on the second form to return to the first form.

When i use the following code

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnNorthwind.Click
DataForm1.Show()
End Sub

I get this error: Reference to a non-shared member
requires an object reference.

Can someone please help me.

Thanks
 
Hi Trev,

It <is> an easy question - once you know - but it's far from stupid. This
issue (Forms versus Form classes) is a major stumbling block for many
programmers coming from VB6 to VB.NET.

The following is taken from an answer to someone a while back. It only
answers part of your question, but hopefully it will shed a bit of light on
the matter.

Forms are no longer just there. They have to be managed. What's worse is
that I'm not quite telling the truth. The .NET designers, in an effort to be
helpful, have hidden some of the code that, if it had to be done manually,
would make things clearer.

In C#, if you want a form, you have to code its declaration and its
instantiation and then show it. All C# programs start with their "Sub Main".
This creates the startup form and off you go. Being explicit means less
confusion.

In VB, if you don't use Sub Main as your starting point, the compiler
effectively creates it for you - behind the scenes. And thus the confusion
starts! Here you have an obviously visible form which has appeared out of
nowhere. There is no variable with which to access it from outside the form.
There is little (to a newcomer from VB6) to say that this Form1 is <not> the
form that you can see, but is the class (template) for the form.

Create a project with a single form, Form1 and this is what you get
(simplified)

Class Form1
Sub Main
Dim f As New Form1
Application.Run (f)
End Sub

Sub New
Sub Form1_Load
End Class

But you never get to see the Sub Main as this is done for you and hidden.
In VB6, Form1 would be the class <and> the instance. In VB.NET, Form1 is the
class <only> while f, the hidden variable, is the instance.

Let's put Sub Main into a module:

Module LetsGo
Public f As New Form1
Sub Main
Application.Run (f)
End Sub
End Module

Class Form1
Sub New
Sub Form1_Load
End Class

Now you have a global variable, f, which is the actual form. You can
access this variable from anywhere and use it to Show and Hide, etc.

Regards,
Fergus
 
* "Trev said:
I just want to know (i know i should know this) how can i
show up another form?

I have 2 forms and the first one has a button on it that
when i click the button i want the second form to show up
(and the first form disappear), and i also want another
button on the second form to return to the first form.

When i use the following code

Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnNorthwind.Click
DataForm1.Show()
End Sub

I get this error: Reference to a non-shared member
requires an object reference.

You will have to use 'Call (New DataForm1()).Show())'.

But this won't solve your problem.

<http://www.google.de/groups?selm=u#[email protected]>
 
Hi Trev,
In addition to the others,
I just want to know (i know i should know this) how can i
show up another form?

I have 2 forms and the first one has a button on it that
when i click the button i want the second form to show up
(and the first form disappear), and i also want another
button on the second form to return to the first form.
This kind of form looks like a dialog form. Therefore is a special method

\\\
dim frm as new form2
frm.showdialog(me)
theTextTromTorm2=frm.aPublicvariableInForm2
frm.dispose 'one of the exceptions to use dispose
///

I hope this helps a little bit?

Cor
 
Back
Top