Pass Data between 2 forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
 
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?
 
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
 
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.
 
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?
 
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).
 
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
 
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!
 
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 "
 
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.
 
Back
Top