473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pass Values between Forms in C# 2005

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
14 3576
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
<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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: VbUser25 | last post by:
hi.. i have a drop-down menu with a list of products.when i select any one product i open a popup displaying hte product categories in it.i then enter the no. of items i want.and finally when i...
1
by: JM | last post by:
Hello, Using Access 2000 queries, you can reference(pass) form values directly using syntax like Forms!frmPaint!txtColor. I want to do a pass through query to SQL Server 2000, but I don't know...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
8
by: Brian F | last post by:
Exactly what the subject says. I have an ASP page that I want my C# windows application to open in Internet Explorer, and if possible have it send along a list of values from a list box. Thank you.
0
by: **Developer** | last post by:
Would someone pass this on to MS and/or check to see it the problem persists in 2005? I don't have 2005 and do not know how to pass it on. Nor do I have the time to spend learning how unless...
1
by: colleen1980 | last post by:
There is a form where it ask the two dates and then run a query then report. Private Sub Command36_Click() DoCmd.OpenQuery "qryResultsReport" DoCmd.OpenReport "rpt_TopTen", acPreview, "", ""...
2
by: gumby | last post by:
I would like to call this stored procedure, but I am unable to pass parameters to the @Start and @End. Is thier a way to pass parameters to a pass through query from MS Access? SELECT ...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
6
by: =?Utf-8?B?Unlhbg==?= | last post by:
I am trying to pass a value from a texbox in Form1 to a textbox in Form2 using properties in VS2005 but it doesn't work; please help (project is attached). Code for Game Class: using System;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.