Pass Values between Forms in C# 2005

R

Rick

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.
 
C

cfps.Christian

You can do this a multitude of ways ranging on what kind of programmer
you are.
1. Put public properties on form B and once you instantiate form B
set those properties which in turn set textboxes on the form
(before .Show)
2. Make the textboxes on form B public and use tactic in #1
3. Overload the constructor on form B to allow values to be passed
into the constructor so on Form.Load you can set all the values (my
personal favorite).
4. You could take the long hard way of doing something with window
handles and instantiating form A from form B and pulling public
properties out (not worth it)

I'm sure the majority of people here would do #3 or #1 (in that
order).
 
D

Duggi

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..
 
C

cfps.Christian

<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.
 
D

Duggi

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);
}
}
 
C

cfps.Christian

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.
 
R

Rick

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.
 
S

Stephany Young

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.
 
P

parez

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.

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.
 
J

Jeff Winn

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
 
R

Rick

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.
 
R

Rick

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?
 
S

Stephany Young

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();
 

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