Passing things from Form1 to Form2 and back

Hi everyone !

Imagine this :

I have 2 Forms: Form1 and Form2

Form1 is the startupform and holds a button, Button1 , to show Form 2.

Code:

Dim F2AsNew Form2

PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

F2.Show()

EndSub

On Button1_Click an new Form2 is shown.

Form2 holds a button wich should change the text of Form1:

Code:

Dim F1As Form1

PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click

F1.Text = "hello"

EndSub

This gives the result : Object reference not set to an instance of an object.

I know i'm doing somthing wrong, but i've just switched from VB6 to .net, and it's all new to me now :-(

Can someone please tell me how i should do this ?

Kind regards,

Guy

[3044 byte] By [GuyVerachtert] at [2007-12-28]
# 1

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

F2.F1 = Me

F2.Show()

End Sub

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Hi DMan1,

No offence, but just F2.F1=me doesn't do the trick ...

Kind regards,

Guy

GuyVerachtert at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

In your code you:

Dim F1 As Form1

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

F1.Text = "hello"

End Sub

Why not

Form1.Button1.Text = "Hello"

F1 nee Form1 does not have a .Text member but the button which is the target does and is on Form1.

You may then have to do an update on Form1:

Form1.Update or Form1.Button1.Update

To force the update as form1 is not the foreground.

Rabtok at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

dear Rabtok,

the problem is that i do not have acces to Form1 ... , not the .Text member

kind regards,

Guy

GuyVerachtert at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5
GuyVerachtert wrote:

Hi DMan1,

No offence, but just F2.F1=me doesn't do the trick ...

Kind regards,

Guy

No offense taken...that is becuase of your scope declaration of F1...

Change it from Dim F1 as Form1 to

Public F1 as Form1

I tested that one and it works just fine...

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6
Rabtok wrote:

Form1 does not have a .Text member

Forms do have a text member ...it is the title bar text

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7

take a look at this:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=835382&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=777222&SiteID=1

you should also avoid using default instances as they are bad practice and will also cause some unpredictable results at times. you should also make use of using public properties on the class you are going to call so it will do the job for you rather than say making a member/method/control public and giving just anyone more than required access

ahmedilyas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8
I am not a pro and do not have VB in this lab; I am in school. But i think you can use the My namespace. like My.Application.Form1....Text = My.Application.Form2....Text
IceAngel89 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9

Hi DMan1,

It works ! Thanks alot !

Kind regards,

Guy

GuyVerachtert at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic Language...