How do i pass data from form 1 to form 2

A

AshParr

Hi all,

I have 2 forms, "Form1" & "Form2", i have a button that currently
opens form2 from form1 and then hides itself:

Form2 form2 = new Form2();
form2.show;
this.Visible = False;

and then the same on form2 to get back to my 1st form:

Form1 form1 = new Form1();
form1.show;
this.Visible = False;


Ok, now i have 3 values on form 1 and they are "carCount", "bikeCount"
and "DisabledCount" how can i show them on my 2nd form?

Many Thanks,

Ash
 
M

maxxx

Hi all,

I have 2 forms, "Form1" & "Form2", i have a button that currently
opens form2 from form1 and then hides itself:

Form2 form2 = new Form2();
form2.show;
this.Visible = False;

and then the same on form2 to get back to my 1st form:

Form1 form1 = new Form1();
form1.show;
this.Visible = False;

Ok, now i have 3 values on form 1 and they are "carCount", "bikeCount"
and "DisabledCount" how can i show them on my 2nd form?

Many Thanks,

Ash

1st thing - you don't want to create a new Form1 when you click the
button on Form2 do you? Don't you really want to make Form1 visible
again?
If Form1 is going to be invisible, you could show form 2 modally..
e.g.
Form2 form2 = new Fomr2();
this.Visible = false;
form2.showdialog;
this.Visible = trrue;

then in the click event of Form2, simply close the form.

As for passing data, the simplest way is to have public properties in
the Form2 of CarCount, BikeCOunt and DisabledCount.
then extend the code in Form1 to have...
Form2 form2 = new Fomr2();
form2.CarCount = carcount;
form2.BikeCOunt = bikeCount;
form2.DisabledCount = disabledCount;
this.Visible = false;
form2.showdialog;
this.Visible = trrue;

Or somethng like that...
Cheers
..\\axxx
 
A

AshParr

1st thing - you don't want to create a new Form1 when you click the
button on Form2 do you? Don't you really want to make Form1 visible
again?
If Form1 is going to be invisible, you could show form 2 modally..
e.g.
Form2 form2 = new Fomr2();
this.Visible = false;
form2.showdialog;
this.Visible = trrue;

then in the click event of Form2, simply close the form.

As for passing data, the simplest way is to have public properties in
the Form2 of CarCount, BikeCOunt and DisabledCount.
then extend the code in Form1 to have...
Form2 form2 = new Fomr2();
form2.CarCount = carcount;
form2.BikeCOunt = bikeCount;
form2.DisabledCount = disabledCount;
this.Visible = false;
form2.showdialog;
this.Visible = trrue;

Or somethng like that...
Cheers
.\\axxx- Hide quoted text -

- Show quoted text -

Thankyou for your response, i will give that a shot and see how it
goes. the only reason i make form 1 invisible is so then i just set it
as visible when i close my 2nd form

Many thanks,

Ash
 
A

AshParr

form2.CarCount = carcount;
form2.BikeCount = bikeCount;
form2.DisabledCount = disabledCount;

i have done that and i get an error:

"form2.CarCount inaccessible due to protection level" i think and its
the same for the other 2. Any ideas?

Many thanks,
Ash
 
M

Marc Gravell

"form2.CarCount inaccessible due to protection level"
Well, are they public properties as was suggested? i.e.

class Form2 : Form {
...
private int carCount;
public int CarCount {
get {return carCount; }
set {carCount = value;}
}
...
}
 
A

AshParr

Well, are they public properties as was suggested? i.e.

class Form2 : Form {
...
private int carCount;
public int CarCount {
get {return carCount; }
set {carCount = value;}
}
...

=====================================

ok sorry about this, im struggleing a little:

so far i have this in form 2 like you said:

class Form2 : Form
{
private int carCount;
public int CarCount
{
get {return carCount; }
set {carCount = value;}
}


but i get a message saying that Form2.Form2 allready has carCount
defined
 
M

maxxx

=====================================

ok sorry about this, im struggleing a little:

so far i have this in form 2 like you said:

class Form2 : Form
{
private int carCount;
public int CarCount
{
get {return carCount; }
set {carCount = value;}

}

but i get a message saying that Form2.Form2 allready has carCount
defined

I would check your capitalisation. Personally I tend to prefix my
private variables with an underscore...
So
private int _carCount;
public int CarCount
{
....etc
}
if the compiler says it already has carCopunt defined then you can bet
it has - so just look through checking you have capitalised correctly.

Cheers
 
C

csharp

Hey have a Look for Model-View-Controller(MVC) patterns on google. It
will definitely cater for information being sent from one form to
another , might be over kill in this case

Niq
 
A

AshParr

thankyou all for your responses and help, i have managed to do that

thankyou all

Many thanks,

Ash
 

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