By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Pass Values between Forms in C# 2005

P: n/a
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.

Thanks in advance.
Jun 27 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.

Thanks in advance.
Do you want to pass data from Parent to Child... right???

-Cnu..
Jun 27 '08 #2

P: n/a
<Form A>
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex);
b.ShowDialog();
</FormA>

<FormB>
private string _LastName;
private string _FirstName;
private int _PersonTypeID;
public FormB(string LastName, string FirstName, int PersonTypeID) :
this()
{
_LastName = LastName;
_FirstName = FirstName;
_PersonTypeID = PersonTypeID;
}

public Form_Load(object sender, EventArgs e)
{
this.txtLastName = _LastName;
this.txtFirstName = _FirstName;
this.cboPersonType.SelectedIndex = PersonTypeID;
}
</FormB>

If you need to retrieve the values back out of FormB (you're changing
data) then you can add properties and pull them out in FormA via
properties. What I do in my code at home is every single form control
has a "Changed" event that gets fired and with this event the new
value is taken via a new EventArg. In my case I wanted to know if the
data had been changed and if so then I wanted to update, but I didn't
want to update it no matter what.
Jun 27 '08 #3

P: n/a
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.

Thanks in advance.
Try the following code
public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();

formB.data = this.textBox1.Text.ToString();

formB.Show();

}
}
public partial class FormB : Form
{
public string data;

public FormB()
{
InitializeComponent();
}

private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}
Jun 27 '08 #4

P: n/a
Yes

"Duggi" wrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.

Thanks in advance.

Do you want to pass data from Parent to Child... right???

-Cnu..
Jun 27 '08 #5

P: n/a
On Jun 20, 9:08 pm, Rick <R...@discussions.microsoft.comwrote:
Yes

"Duggi" wrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.
Thanks in advance.
Do you want to pass data from Parent to Child... right???
-Cnu..
Hope you got one example..

-Cnu
Jun 27 '08 #6

P: n/a
On Jun 20, 9:08 pm, Rick <R...@discussions.microsoft.comwrote:
Yes

"Duggi" wrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.
Thanks in advance.
Do you want to pass data from Parent to Child... right???
-Cnu..
Hope you got one example..

-Cnu
Jun 27 '08 #7

P: n/a
On Jun 20, 11:09*am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. *My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.
Thanks in advance.

Try the following code

public partial class FormA : Form
* * {
* * * * public FormA()
* * * * {
* * * * * * InitializeComponent();
* * * * }

* * * * private void Form1_Load(object sender, EventArgs e)
* * * * {

* * * * }

* * * * private void button1_Click(object sender, EventArgs e)
* * * * {
* * * * * * FormB formB = new FormB();

* * * * * * formB.data = this.textBox1.Text.ToString();

* * * * * * formB.Show();

* * * * }
* * }

public partial class FormB : Form
* * {
* * * * public string data;

* * * * public FormB()
* * * * {
* * * * * * InitializeComponent();
* * * * }

* * * * private void FormB_Load(object sender, EventArgs e)
* * * * {
* * * * * * MessageBox.Show(data);
* * * * }
* * }
I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.
Jun 27 '08 #8

P: n/a
cfps.Christian,
I do not want to pass a series of strings like the example you provided:
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex)

I may have 50 or 100 fields on FormA and it amy not be the right approach.
Do you have an example of another way f doing it; the example Duggi has
provided would also work but it would require each field to be set to Public
and I would rather not do it.
"cfps.Christian" wrote:
On Jun 20, 11:09 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.
Thanks in advance.
Try the following code

public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();

formB.data = this.textBox1.Text.ToString();

formB.Show();

}
}

public partial class FormB : Form
{
public string data;

public FormB()
{
InitializeComponent();
}

private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}

I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.
Jun 27 '08 #9

P: n/a
The critical issues are where your data is coming from in FormA and what you
are doing with it in FormB.

Until you determine that you cannot come up with a 'transfer' strategy.
"Rick" <Ri**@discussions.microsoft.comwrote in message
news:E1**********************************@microsof t.com...
cfps.Christian,
I do not want to pass a series of strings like the example you provided:
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex)

I may have 50 or 100 fields on FormA and it amy not be the right approach.
Do you have an example of another way f doing it; the example Duggi has
provided would also work but it would require each field to be set to
Public
and I would rather not do it.
"cfps.Christian" wrote:
>On Jun 20, 11:09 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:

I have seen examples of passing data from FormB to FormA (Parent To
Child)
but I need an example of passind data from FormA to FormB. My main
form is
FormA; I will enter some data in textboxes and click a button to
bring up
FormB which needs to display values entered in FormA.

Thanks in advance.

Try the following code

public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();

formB.data = this.textBox1.Text.ToString();

formB.Show();

}
}

public partial class FormB : Form
{
public string data;

public FormB()
{
InitializeComponent();
}

private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}

I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.
Jun 27 '08 #10

P: n/a
On Jun 20, 7:13 pm, Rick <R...@discussions.microsoft.comwrote:
cfps.Christian,
I do not want to pass a series of strings like the example you provided:
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex)

I may have 50 or 100 fields on FormA and it amy not be the right approach.
Do you have an example of another way f doing it; the example Duggi has
provided would also work but it would require each field to be set to Public
and I would rather not do it.

"cfps.Christian" wrote:
On Jun 20, 11:09 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.
Thanks in advance.
Try the following code
public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();
formB.data = this.textBox1.Text.ToString();
formB.Show();
}
}
public partial class FormB : Form
{
public string data;
public FormB()
{
InitializeComponent();
}
private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}
I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.
I mostly use #3 and #1 from cfps's list.
If i have to pass some data to a back to a parent form , i use events.
e.g from summary screen (grid) you double click a row , you open a
detail form. and you make changes and you save it. and you want to let
the summary form know that something was saved on the form so that it
can refresh.
All my forms implement an interface, and all fields have properties
from that interface. If i had to pass to pass 50 fields,
i would create a property of the the interface type on the child form
and assign it from parent.
Jun 27 '08 #11

P: n/a
If you've got that many fields on your parent form, you might want to
rethink how you're designing the presentation. Not to mention that's going
to seriously increase the complexity of your form.

In OO, typically you don't want to expose underlying private fields outside
of the class they were declared in. Someone once told me years ago that "you
don't show your friends your privates" when I was discussing this very same
thing with him. A good reason being this: say you've got a private integer
being exposed, and your object expects certain values to be in that field -
you'd have no way of verifying that the data is correct. Using a publicly
exposed property you can check the data before the field actually changes.
Besides, if you don't need it to test the data right now - what happens
later when you do? You'll need to find any references to the field, and add
checks all over your application. Properties just work out much easier.

Stick with properties - and if you absolutely must have that many fields
(why you would i'm not sure) on a single form consider creating a strongly
typed object to hold the data that you can easily pass back and forth
between the forms. One good reason for this approach, if your data on the
form changes - again you'll only have 1 place to update rather than having
to deal with it all over the place.

As for using the constructor to pass in the data to the form - I try to
limit arguments on constructors to those I absolutely must have available
for that object to work properly.

HTH

"Rick" <Ri**@discussions.microsoft.comwrote in message
news:E1**********************************@microsof t.com...
cfps.Christian,
I do not want to pass a series of strings like the example you provided:
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex)

I may have 50 or 100 fields on FormA and it amy not be the right approach.
Do you have an example of another way f doing it; the example Duggi has
provided would also work but it would require each field to be set to
Public
and I would rather not do it.
"cfps.Christian" wrote:
>On Jun 20, 11:09 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:

I have seen examples of passing data from FormB to FormA (Parent To
Child)
but I need an example of passind data from FormA to FormB. My main
form is
FormA; I will enter some data in textboxes and click a button to
bring up
FormB which needs to display values entered in FormA.

Thanks in advance.

Try the following code

public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();

formB.data = this.textBox1.Text.ToString();

formB.Show();

}
}

public partial class FormB : Form
{
public string data;

public FormB()
{
InitializeComponent();
}

private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}

I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.
Jun 27 '08 #12

P: n/a
The data on FormA is typed in. When user clicks on a button provided on
FormA; FormB is displayed with all the data that were entered on FormA. All
fields on FormA and FormB are identical; think of FormB as print preview
screen.

"Stephany Young" wrote:
The critical issues are where your data is coming from in FormA and what you
are doing with it in FormB.

Until you determine that you cannot come up with a 'transfer' strategy.
"Rick" <Ri**@discussions.microsoft.comwrote in message
news:E1**********************************@microsof t.com...
cfps.Christian,
I do not want to pass a series of strings like the example you provided:
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex)

I may have 50 or 100 fields on FormA and it amy not be the right approach.
Do you have an example of another way f doing it; the example Duggi has
provided would also work but it would require each field to be set to
Public
and I would rather not do it.
"cfps.Christian" wrote:
On Jun 20, 11:09 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:

I have seen examples of passing data from FormB to FormA (Parent To
Child)
but I need an example of passind data from FormA to FormB. My main
form is
FormA; I will enter some data in textboxes and click a button to
bring up
FormB which needs to display values entered in FormA.

Thanks in advance.

Try the following code

public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();

formB.data = this.textBox1.Text.ToString();

formB.Show();

}
}

public partial class FormB : Form
{
public string data;

public FormB()
{
InitializeComponent();
}

private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}

I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.

Jun 27 '08 #13

P: n/a
I have no problems passing data back to Parent form FormB using properties
but I want to use the same technique for passing data from FormA to FormB;
could you please provide me a sample code?

"parez" wrote:
On Jun 20, 7:13 pm, Rick <R...@discussions.microsoft.comwrote:
cfps.Christian,
I do not want to pass a series of strings like the example you provided:
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex)

I may have 50 or 100 fields on FormA and it amy not be the right approach.
Do you have an example of another way f doing it; the example Duggi has
provided would also work but it would require each field to be set to Public
and I would rather not do it.

"cfps.Christian" wrote:
On Jun 20, 11:09 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:
I have seen examples of passing data from FormB to FormA (Parent To Child)
but I need an example of passind data from FormA to FormB. My main form is
FormA; I will enter some data in textboxes and click a button to bring up
FormB which needs to display values entered in FormA.
Thanks in advance.
Try the following code
public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();
formB.data = this.textBox1.Text.ToString();
formB.Show();
}
}
public partial class FormB : Form
{
public string data;
public FormB()
{
InitializeComponent();
}
private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}
I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.

I mostly use #3 and #1 from cfps's list.
If i have to pass some data to a back to a parent form , i use events.
e.g from summary screen (grid) you double click a row , you open a
detail form. and you make changes and you save it. and you want to let
the summary form know that something was saved on the form so that it
can refresh.
All my forms implement an interface, and all fields have properties
from that interface. If i had to pass to pass 50 fields,
i would create a property of the the interface type on the child form
and assign it from parent.
Jun 27 '08 #14

P: n/a
Well, how the data gets ito FormA is irrelevant.

The important bit is "All fields on FormA and FormB are identical".

By your definition, if you have a TextBox control named textbox1 on FormA
then you also have a TextBox control named textbox1 on FormB.

Unless you have manually changed the access level of the controls on FormB
from thier default (internal) then, in the handler for the 'button' click
event on FormA, you can simply use:

FormB formB = new FormB();
formB.textbox1.Text = this.textbox1.Text;
...
formB.Show();
"Rick" <Ri**@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
The data on FormA is typed in. When user clicks on a button provided on
FormA; FormB is displayed with all the data that were entered on FormA.
All
fields on FormA and FormB are identical; think of FormB as print preview
screen.

"Stephany Young" wrote:
>The critical issues are where your data is coming from in FormA and what
you
are doing with it in FormB.

Until you determine that you cannot come up with a 'transfer' strategy.
"Rick" <Ri**@discussions.microsoft.comwrote in message
news:E1**********************************@microso ft.com...
cfps.Christian,
I do not want to pass a series of strings like the example you
provided:
FormB b = new FormB(this.TextBox1.Text, this.TextBox2.Text,
this.ComboBox1.SelectedIndex)

I may have 50 or 100 fields on FormA and it amy not be the right
approach.
Do you have an example of another way f doing it; the example Duggi has
provided would also work but it would require each field to be set to
Public
and I would rather not do it.
"cfps.Christian" wrote:

On Jun 20, 11:09 am, Duggi <DuggiSrinivasa...@gmail.comwrote:
On Jun 20, 7:28 pm, Rick <R...@discussions.microsoft.comwrote:

I have seen examples of passing data from FormB to FormA (Parent
To
Child)
but I need an example of passind data from FormA to FormB. My
main
form is
FormA; I will enter some data in textboxes and click a button to
bring up
FormB which needs to display values entered in FormA.

Thanks in advance.

Try the following code

public partial class FormA : Form
{
public FormA()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
FormB formB = new FormB();

formB.data = this.textBox1.Text.ToString();

formB.Show();

}
}

public partial class FormB : Form
{
public string data;

public FormB()
{
InitializeComponent();
}

private void FormB_Load(object sender, EventArgs e)
{
MessageBox.Show(data);
}
}

I'm not generally a fan of the public member variable to pass data.
I've worked on too many VB6 projects where that's about the only way
to pass variables and its hell to maintain.

Jun 27 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.