How do I pass a string to a new form

  • Thread starter Thread starter Brad McMillan
  • Start date Start date
B

Brad McMillan

Hi:

I have MS C# 2005 Express Edition and I'm trying to pass a string to a
new form in my project.

First I created the form, Customer, with a textbox, "textBox1", and
declared a string "name":

namespace myProject
{
public partial class Customer : Form
{
public string name;
public Customer()
{
InitializeComponent();
textBox1.Text = name;
}
}
}

Then, in my main program I have a buttom click event that activates
the form:

private void button1_Click(object sender, EventArgs e)
{
Customer dlg1 = new Customer();
dlg1.name = "test";
dlg1.ShowDialog();
}

But, when I press button1 the string "test" does not show up in
textBox1.

Can anyone tell me what I need to do to get the string to pass to my
new form?

TIA,
Brad McMillan
 
Brad McMillan said:
I have MS C# 2005 Express Edition and I'm trying to pass a string to a
new form in my project.

First I created the form, Customer, with a textbox, "textBox1", and
declared a string "name":

namespace myProject
{
public partial class Customer : Form
{
public string name;
public Customer()
{
InitializeComponent();
textBox1.Text = name;
}
}
}

Then, in my main program I have a buttom click event that activates
the form:

private void button1_Click(object sender, EventArgs e)
{
Customer dlg1 = new Customer();
dlg1.name = "test";
dlg1.ShowDialog();
}

But, when I press button1 the string "test" does not show up in
textBox1.

Can anyone tell me what I need to do to get the string to pass to my
new form?

Let's look at what you're doing:

1) You call the parameterless constructor. This sets textBox1.Text to
the current value of "name", which is null.

2) You set the new value of "name". This doesn't nothing other than
changing the value of the field.

That's why it doesn't work at the moment.

The easiest fix is to change "name" to be private (which is always a
good idea - public fields should be avoided), give the constructor a
parameter, and pass the name in as that parameter. That means that when
you've created the text box, you can set the text of it before even
leaving the constructor.
 
Hi Jon:

It worked.

Thanks!

Let's look at what you're doing:

1) You call the parameterless constructor. This sets textBox1.Text to
the current value of "name", which is null.

2) You set the new value of "name". This doesn't nothing other than
changing the value of the field.

That's why it doesn't work at the moment.

The easiest fix is to change "name" to be private (which is always a
good idea - public fields should be avoided), give the constructor a
parameter, and pass the name in as that parameter. That means that when
you've created the text box, you can set the text of it before even
leaving the constructor.
 
Maybe you need not use constructor with paremeter,just using Perperty.
like this,

public string SomeName
{
set{textbox1.Text=value;}
set{return textBox1.Text;} //if needed
}

And,

Customer customer=new Customer();
customer.SomeName='...';

Brad McMillan said:
Hi Jon:

It worked.

Thanks!
 
Back
Top