469,912 Members | 1,842 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,912 developers. It's quick & easy.

Pass Data between 2 forms

Yeah, I know this question was asked by someone elselike 2 weeks ago. But I
need some additional help. I have a program I'm developing, and multiple
different forms will be opened. For now though, I just have two forms. One is
the main window, and the other sets some preferences like names, and email
addresses.

When someone opens this window, and enters values on this page, I'd like it
to set the values of public variables on the main page.

I'm assuming this can be done, but how?

On the main page, I set a public form2 as 2ndForm = new secondForm

I can access the 2nd form properties from the main page, but not visa-versa.

?????????

Thanks
Jul 21 '05 #1
12 1602
Are you talking about a Windows or Web application?

With WebForm applications, we now use a 1 page paradigm for submitting and
processing form data. With Windows Forms applications, you just need to
declare the variables to Public scope and they will be accessible from the
other form instance.
Did you catch that, the Public variables should be declared on the *first*
form, then they will be available from the second form.

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
Yeah, I know this question was asked by someone elselike 2 weeks ago. But
I
need some additional help. I have a program I'm developing, and multiple
different forms will be opened. For now though, I just have two forms. One
is
the main window, and the other sets some preferences like names, and email
addresses.

When someone opens this window, and enters values on this page, I'd like
it
to set the values of public variables on the main page.

I'm assuming this can be done, but how?

On the main page, I set a public form2 as 2ndForm = new secondForm

I can access the 2nd form properties from the main page, but not
visa-versa.

?????????

Thanks

Jul 21 '05 #2
Yeah. I'm talking about a windows form.
The web form thing I got down.

I did declare them public on the first form... I thought all I needed to do
was to type something like:

(from form 2)
form1.person1.name = "betty"

however, in Visual Studio whenever I try to put something in like that it
underlines it saying "Reference to a non-shared member requires an object
reference."

so... my assumption was incorrect.

I tried creating a reference to it:

public firstForm as Form1

That makes the underlining go away, but during runtime, it throws an
exception:
"Object reference not set to an instance of an object."

so I don't know.
Maybe I'm just not understanding?
"Scott M." wrote:
Are you talking about a Windows or Web application?

With WebForm applications, we now use a 1 page paradigm for submitting and
processing form data. With Windows Forms applications, you just need to
declare the variables to Public scope and they will be accessible from the
other form instance.
Did you catch that, the Public variables should be declared on the *first*
form, then they will be available from the second form.

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
Yeah, I know this question was asked by someone elselike 2 weeks ago. But
I
need some additional help. I have a program I'm developing, and multiple
different forms will be opened. For now though, I just have two forms. One
is
the main window, and the other sets some preferences like names, and email
addresses.

When someone opens this window, and enters values on this page, I'd like
it
to set the values of public variables on the main page.

I'm assuming this can be done, but how?

On the main page, I set a public form2 as 2ndForm = new secondForm

I can access the 2nd form properties from the main page, but not
visa-versa.

?????????

Thanks


Jul 21 '05 #3
Did you actually assign the form1 variable in the second form to point to
form1? I'm pretty certain that you didn't considering the error you're
getting. Anyway, if the fields are indeed public in form1, then it should
be as simple as doing something like this inside form2.cs

// add a field to hold a reference to the main form
private Form1 _mainFrm;

// adjust the constructor to accept the main form's reference
public Form2(Form1 frm)
{
InitializeComponent();

_mainFrm = frm;
_mainFrm.person1.name = "betty";
}
....

Then inside Form1, create the second form using the constructor we just
made:

Form2 f = new Form2(this);
f.ShowDialog();

As a side note, I highly recommend using delegates/events to notify the main
form when something has changed instead of the above.

ShaneB

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
Yeah. I'm talking about a windows form.
The web form thing I got down.

I did declare them public on the first form... I thought all I needed to
do
was to type something like:

(from form 2)
form1.person1.name = "betty"

however, in Visual Studio whenever I try to put something in like that it
underlines it saying "Reference to a non-shared member requires an object
reference."

so... my assumption was incorrect.

I tried creating a reference to it:

public firstForm as Form1

That makes the underlining go away, but during runtime, it throws an
exception:
"Object reference not set to an instance of an object."

so I don't know.
Maybe I'm just not understanding?

Jul 21 '05 #4
You need to have an instance of Form1 before you can use any of its public
members in Form2.

This (form1.person1.name = "betty") won't work unless you have an instance
of Form1 still in accessible memory. And, this (public firstForm as Form1)
does declare an object variable of the right type (so no underline), but
doesn't give you an instance of the object to work with because you didn't
include the keyword "new" in the statement.

It actually is simple and you are very close. Your problem is that you
don't have an instance of Form1 in memory when you are using Form2. Show us
all of your code.
"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
Yeah. I'm talking about a windows form.
The web form thing I got down.

I did declare them public on the first form... I thought all I needed to
do
was to type something like:

(from form 2)
form1.person1.name = "betty"

however, in Visual Studio whenever I try to put something in like that it
underlines it saying "Reference to a non-shared member requires an object
reference."

so... my assumption was incorrect.

I tried creating a reference to it:

public firstForm as Form1

That makes the underlining go away, but during runtime, it throws an
exception:
"Object reference not set to an instance of an object."

so I don't know.
Maybe I'm just not understanding?
"Scott M." wrote:
Are you talking about a Windows or Web application?

With WebForm applications, we now use a 1 page paradigm for submitting
and
processing form data. With Windows Forms applications, you just need to
declare the variables to Public scope and they will be accessible from
the
other form instance.
Did you catch that, the Public variables should be declared on the
*first*
form, then they will be available from the second form.

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
> Yeah, I know this question was asked by someone elselike 2 weeks ago.
> But
> I
> need some additional help. I have a program I'm developing, and
> multiple
> different forms will be opened. For now though, I just have two forms.
> One
> is
> the main window, and the other sets some preferences like names, and
> email
> addresses.
>
> When someone opens this window, and enters values on this page, I'd
> like
> it
> to set the values of public variables on the main page.
>
> I'm assuming this can be done, but how?
>
> On the main page, I set a public form2 as 2ndForm = new secondForm
>
> I can access the 2nd form properties from the main page, but not
> visa-versa.
>
> ?????????
>
> Thanks


Jul 21 '05 #5
I'm getting close? Score!

Okay. The only thing I have on form2 right now is a button. Right now, all
that button does is set some variables on form1 (until I get things working).
Here is my code on form2:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim mainGame As game.gameMain 'set reference to form 1
mainGame.Owner = Me ' dont know if I need this
mainGame.player1.playerName = "Nive" 'set the player1's name to Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

the player1 variable is declared in form1 (from a seperate class):
Public player1 As player = New player 'create player1

Any ideas?
"Scott M." wrote:
You need to have an instance of Form1 before you can use any of its public
members in Form2.

This (form1.person1.name = "betty") won't work unless you have an instance
of Form1 still in accessible memory. And, this (public firstForm as Form1)
does declare an object variable of the right type (so no underline), but
doesn't give you an instance of the object to work with because you didn't
include the keyword "new" in the statement.

It actually is simple and you are very close. Your problem is that you
don't have an instance of Form1 in memory when you are using Form2. Show us
all of your code.
"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
Yeah. I'm talking about a windows form.
The web form thing I got down.

I did declare them public on the first form... I thought all I needed to
do
was to type something like:

(from form 2)
form1.person1.name = "betty"

however, in Visual Studio whenever I try to put something in like that it
underlines it saying "Reference to a non-shared member requires an object
reference."

so... my assumption was incorrect.

I tried creating a reference to it:

public firstForm as Form1

That makes the underlining go away, but during runtime, it throws an
exception:
"Object reference not set to an instance of an object."

so I don't know.
Maybe I'm just not understanding?
"Scott M." wrote:
Are you talking about a Windows or Web application?

With WebForm applications, we now use a 1 page paradigm for submitting
and
processing form data. With Windows Forms applications, you just need to
declare the variables to Public scope and they will be accessible from
the
other form instance.
Did you catch that, the Public variables should be declared on the
*first*
form, then they will be available from the second form.

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:C4**********************************@microsof t.com...
> Yeah, I know this question was asked by someone elselike 2 weeks ago.
> But
> I
> need some additional help. I have a program I'm developing, and
> multiple
> different forms will be opened. For now though, I just have two forms.
> One
> is
> the main window, and the other sets some preferences like names, and
> email
> addresses.
>
> When someone opens this window, and enters values on this page, I'd
> like
> it
> to set the values of public variables on the main page.
>
> I'm assuming this can be done, but how?
>
> On the main page, I set a public form2 as 2ndForm = new secondForm
>
> I can access the 2nd form properties from the main page, but not
> visa-versa.
>
> ?????????
>
> Thanks


Jul 21 '05 #6
Well your problem is just as I said, you aren't making a reference to form1
at all, you are only making an object variable of the form1 type. What you
don't say below is if there is an instance of Form1 already when Form2 is
launched.

If there is, then your code needs to be:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'I am assuming that "mainGame" is the name of the instance of the
Form1 class
mainGame.player1.playerName = "Nive" 'set the player1's name to Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

************************************************** ***********************

If there isn't, then the code should be:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'...Notice the keyword "NEW" below.....
Dim mainGame As NEW game.gameMain 'set reference to NEW INSTANCE OF
form 1
mainGame.player1.playerName = "Nive" 'set the player1's name to Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

So essentially it boils down to which form is instantiated first (form1 or
form2). If there is already an instance of form1, then you just simply
access its public members. If there is not an instance of form1 when form2
is running, then you need to make an instance of form1 (just setting a
variable to the type of form1 isn't enough).

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
I'm getting close? Score!

Okay. The only thing I have on form2 right now is a button. Right now, all
that button does is set some variables on form1 (until I get things
working).
Here is my code on form2:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim mainGame As game.gameMain 'set reference to form 1
mainGame.Owner = Me ' dont know if I need this
mainGame.player1.playerName = "Nive" 'set the player1's name to
Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

the player1 variable is declared in form1 (from a seperate class):
Public player1 As player = New player 'create player1

Any ideas?
"Scott M." wrote:
You need to have an instance of Form1 before you can use any of its
public
members in Form2.

This (form1.person1.name = "betty") won't work unless you have an
instance
of Form1 still in accessible memory. And, this (public firstForm as
Form1)
does declare an object variable of the right type (so no underline), but
doesn't give you an instance of the object to work with because you
didn't
include the keyword "new" in the statement.

It actually is simple and you are very close. Your problem is that you
don't have an instance of Form1 in memory when you are using Form2. Show
us
all of your code.
"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
> Yeah. I'm talking about a windows form.
> The web form thing I got down.
>
> I did declare them public on the first form... I thought all I needed
> to
> do
> was to type something like:
>
> (from form 2)
> form1.person1.name = "betty"
>
> however, in Visual Studio whenever I try to put something in like that
> it
> underlines it saying "Reference to a non-shared member requires an
> object
> reference."
>
> so... my assumption was incorrect.
>
> I tried creating a reference to it:
>
> public firstForm as Form1
>
> That makes the underlining go away, but during runtime, it throws an
> exception:
> "Object reference not set to an instance of an object."
>
> so I don't know.
> Maybe I'm just not understanding?
>
>
> "Scott M." wrote:
>
>> Are you talking about a Windows or Web application?
>>
>> With WebForm applications, we now use a 1 page paradigm for submitting
>> and
>> processing form data. With Windows Forms applications, you just need
>> to
>> declare the variables to Public scope and they will be accessible from
>> the
>> other form instance.
>> Did you catch that, the Public variables should be declared on the
>> *first*
>> form, then they will be available from the second form.
>>
>> "Casey" <Ca***@discussions.microsoft.com> wrote in message
>> news:C4**********************************@microsof t.com...
>> > Yeah, I know this question was asked by someone elselike 2 weeks
>> > ago.
>> > But
>> > I
>> > need some additional help. I have a program I'm developing, and
>> > multiple
>> > different forms will be opened. For now though, I just have two
>> > forms.
>> > One
>> > is
>> > the main window, and the other sets some preferences like names, and
>> > email
>> > addresses.
>> >
>> > When someone opens this window, and enters values on this page, I'd
>> > like
>> > it
>> > to set the values of public variables on the main page.
>> >
>> > I'm assuming this can be done, but how?
>> >
>> > On the main page, I set a public form2 as 2ndForm = new secondForm
>> >
>> > I can access the 2nd form properties from the main page, but not
>> > visa-versa.
>> >
>> > ?????????
>> >
>> > Thanks
>>
>>
>>


Jul 21 '05 #7
Your correct. The first form is already loaded when form 2 is opened.
Here's how I open form2 from form1.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim setupForm As secondForm = New secondForm
setupForm.Show()
End Sub

I guess, the only thing I'm still confused about is how I reference the
first form from form 2?
When I try to access form1's public variables, I get "Object reference not
set to an instance of an object."

So I have to do somehting...

Thanks for the Help Scott M. I'm mostly an asp.net programmer. Windows
programming is somehting I haven't needed to work with until now.

-- Casey

"Scott M." wrote:
Well your problem is just as I said, you aren't making a reference to form1
at all, you are only making an object variable of the form1 type. What you
don't say below is if there is an instance of Form1 already when Form2 is
launched.

If there is, then your code needs to be:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'I am assuming that "mainGame" is the name of the instance of the
Form1 class
mainGame.player1.playerName = "Nive" 'set the player1's name to Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

************************************************** ***********************

If there isn't, then the code should be:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'...Notice the keyword "NEW" below.....
Dim mainGame As NEW game.gameMain 'set reference to NEW INSTANCE OF
form 1
mainGame.player1.playerName = "Nive" 'set the player1's name to Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

So essentially it boils down to which form is instantiated first (form1 or
form2). If there is already an instance of form1, then you just simply
access its public members. If there is not an instance of form1 when form2
is running, then you need to make an instance of form1 (just setting a
variable to the type of form1 isn't enough).

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
I'm getting close? Score!

Okay. The only thing I have on form2 right now is a button. Right now, all
that button does is set some variables on form1 (until I get things
working).
Here is my code on form2:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim mainGame As game.gameMain 'set reference to form 1
mainGame.Owner = Me ' dont know if I need this
mainGame.player1.playerName = "Nive" 'set the player1's name to
Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

the player1 variable is declared in form1 (from a seperate class):
Public player1 As player = New player 'create player1

Any ideas?
"Scott M." wrote:
You need to have an instance of Form1 before you can use any of its
public
members in Form2.

This (form1.person1.name = "betty") won't work unless you have an
instance
of Form1 still in accessible memory. And, this (public firstForm as
Form1)
does declare an object variable of the right type (so no underline), but
doesn't give you an instance of the object to work with because you
didn't
include the keyword "new" in the statement.

It actually is simple and you are very close. Your problem is that you
don't have an instance of Form1 in memory when you are using Form2. Show
us
all of your code.
"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
> Yeah. I'm talking about a windows form.
> The web form thing I got down.
>
> I did declare them public on the first form... I thought all I needed
> to
> do
> was to type something like:
>
> (from form 2)
> form1.person1.name = "betty"
>
> however, in Visual Studio whenever I try to put something in like that
> it
> underlines it saying "Reference to a non-shared member requires an
> object
> reference."
>
> so... my assumption was incorrect.
>
> I tried creating a reference to it:
>
> public firstForm as Form1
>
> That makes the underlining go away, but during runtime, it throws an
> exception:
> "Object reference not set to an instance of an object."
>
> so I don't know.
> Maybe I'm just not understanding?
>
>
> "Scott M." wrote:
>
>> Are you talking about a Windows or Web application?
>>
>> With WebForm applications, we now use a 1 page paradigm for submitting
>> and
>> processing form data. With Windows Forms applications, you just need
>> to
>> declare the variables to Public scope and they will be accessible from
>> the
>> other form instance.
>> Did you catch that, the Public variables should be declared on the
>> *first*
>> form, then they will be available from the second form.
>>
>> "Casey" <Ca***@discussions.microsoft.com> wrote in message
>> news:C4**********************************@microsof t.com...
>> > Yeah, I know this question was asked by someone elselike 2 weeks
>> > ago.
>> > But
>> > I
>> > need some additional help. I have a program I'm developing, and
>> > multiple
>> > different forms will be opened. For now though, I just have two
>> > forms.
>> > One
>> > is
>> > the main window, and the other sets some preferences like names, and
>> > email
>> > addresses.
>> >
>> > When someone opens this window, and enters values on this page, I'd
>> > like
>> > it
>> > to set the values of public variables on the main page.
>> >
>> > I'm assuming this can be done, but how?
>> >
>> > On the main page, I set a public form2 as 2ndForm = new secondForm
>> >
>> > I can access the 2nd form properties from the main page, but not
>> > visa-versa.
>> >
>> > ?????????
>> >
>> > Thanks
>>
>>
>>


Jul 21 '05 #8
oh, oh. I think I got it now.
I have to pass the first form TO the second form when I call it from the
first form.
secondform.firstform = me

right? right?

Score!

"Scott M." wrote:
Well your problem is just as I said, you aren't making a reference to form1
at all, you are only making an object variable of the form1 type. What you
don't say below is if there is an instance of Form1 already when Form2 is
launched.

If there is, then your code needs to be:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'I am assuming that "mainGame" is the name of the instance of the
Form1 class
mainGame.player1.playerName = "Nive" 'set the player1's name to Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

************************************************** ***********************

If there isn't, then the code should be:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'...Notice the keyword "NEW" below.....
Dim mainGame As NEW game.gameMain 'set reference to NEW INSTANCE OF
form 1
mainGame.player1.playerName = "Nive" 'set the player1's name to Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

So essentially it boils down to which form is instantiated first (form1 or
form2). If there is already an instance of form1, then you just simply
access its public members. If there is not an instance of form1 when form2
is running, then you need to make an instance of form1 (just setting a
variable to the type of form1 isn't enough).

"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
I'm getting close? Score!

Okay. The only thing I have on form2 right now is a button. Right now, all
that button does is set some variables on form1 (until I get things
working).
Here is my code on form2:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim mainGame As game.gameMain 'set reference to form 1
mainGame.Owner = Me ' dont know if I need this
mainGame.player1.playerName = "Nive" 'set the player1's name to
Nive
mainGame.player1.Department = "Accounting" 'set her department
End Sub

the player1 variable is declared in form1 (from a seperate class):
Public player1 As player = New player 'create player1

Any ideas?
"Scott M." wrote:
You need to have an instance of Form1 before you can use any of its
public
members in Form2.

This (form1.person1.name = "betty") won't work unless you have an
instance
of Form1 still in accessible memory. And, this (public firstForm as
Form1)
does declare an object variable of the right type (so no underline), but
doesn't give you an instance of the object to work with because you
didn't
include the keyword "new" in the statement.

It actually is simple and you are very close. Your problem is that you
don't have an instance of Form1 in memory when you are using Form2. Show
us
all of your code.
"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
> Yeah. I'm talking about a windows form.
> The web form thing I got down.
>
> I did declare them public on the first form... I thought all I needed
> to
> do
> was to type something like:
>
> (from form 2)
> form1.person1.name = "betty"
>
> however, in Visual Studio whenever I try to put something in like that
> it
> underlines it saying "Reference to a non-shared member requires an
> object
> reference."
>
> so... my assumption was incorrect.
>
> I tried creating a reference to it:
>
> public firstForm as Form1
>
> That makes the underlining go away, but during runtime, it throws an
> exception:
> "Object reference not set to an instance of an object."
>
> so I don't know.
> Maybe I'm just not understanding?
>
>
> "Scott M." wrote:
>
>> Are you talking about a Windows or Web application?
>>
>> With WebForm applications, we now use a 1 page paradigm for submitting
>> and
>> processing form data. With Windows Forms applications, you just need
>> to
>> declare the variables to Public scope and they will be accessible from
>> the
>> other form instance.
>> Did you catch that, the Public variables should be declared on the
>> *first*
>> form, then they will be available from the second form.
>>
>> "Casey" <Ca***@discussions.microsoft.com> wrote in message
>> news:C4**********************************@microsof t.com...
>> > Yeah, I know this question was asked by someone elselike 2 weeks
>> > ago.
>> > But
>> > I
>> > need some additional help. I have a program I'm developing, and
>> > multiple
>> > different forms will be opened. For now though, I just have two
>> > forms.
>> > One
>> > is
>> > the main window, and the other sets some preferences like names, and
>> > email
>> > addresses.
>> >
>> > When someone opens this window, and enters values on this page, I'd
>> > like
>> > it
>> > to set the values of public variables on the main page.
>> >
>> > I'm assuming this can be done, but how?
>> >
>> > On the main page, I set a public form2 as 2ndForm = new secondForm
>> >
>> > I can access the 2nd form properties from the main page, but not
>> > visa-versa.
>> >
>> > ?????????
>> >
>> > Thanks
>>
>>
>>


Jul 21 '05 #9
No.

Here, I've tried this myself and it works:

On the first form (Form1):

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Jul 21 '05 #10
Bingo.
You've explained it perfectly, and I've gotten it to work.

You rock dude.

"Scott M." wrote:
No.

Here, I've tried this myself and it works:

On the first form (Form1):

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
.
.
.
#End Region

Public x As Integer = 5
Public y As String = "Test"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim secondForm As New Form2
secondForm.Show()
secondForm.getForm1(Me) ' <--- This is the sub that will pass the
first form instance to the second form
End Sub
End Class

**************************************

On the second form (Form2):

Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
.
.
.
#End Region

Public Sub getForm1(ByVal theForm As Form1)
theForm.x = 6 '<-- You are now accessing the values from form 1
theForm.y = "Something Else"
End Sub
End Class

Jul 21 '05 #11
Happy to help. Good luck!
"Casey" <Ca***@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
Bingo.
You've explained it perfectly, and I've gotten it to work.

You rock dude.

"Scott M." wrote:
No.

Here, I've tried this myself and it works:

On the first form (Form1):

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
.
.
.
#End Region

Public x As Integer = 5
Public y As String = "Test"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim secondForm As New Form2
secondForm.Show()
secondForm.getForm1(Me) ' <--- This is the sub that will pass
the
first form instance to the second form
End Sub
End Class

**************************************

On the second form (Form2):

Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
.
.
.
#End Region

Public Sub getForm1(ByVal theForm As Form1)
theForm.x = 6 '<-- You are now accessing the values from form
1
theForm.y = "Something Else"
End Sub
End Class

Jul 21 '05 #12
try to pass the main form as argument in the constructor of second form and
then set the values of main form from the second form. make sure that the
main form members are accessible to the second form from outside.

"Casey" wrote:
Yeah, I know this question was asked by someone elselike 2 weeks ago. But I
need some additional help. I have a program I'm developing, and multiple
different forms will be opened. For now though, I just have two forms. One is
the main window, and the other sets some preferences like names, and email
addresses.

When someone opens this window, and enters values on this page, I'd like it
to set the values of public variables on the main page.

I'm assuming this can be done, but how?

On the main page, I set a public form2 as 2ndForm = new secondForm

I can access the 2nd form properties from the main page, but not visa-versa.

?????????

Thanks

Jul 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Zlatko Matić | last post: by
6 posts views Thread by juky | last post: by
4 posts views Thread by rlntemp-gng | last post: by
4 posts views Thread by Lilith | last post: by
14 posts views Thread by =?Utf-8?B?Umljaw==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.