Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing string values 1

Status
Not open for further replies.

choudhmh

Programmer
Aug 9, 2004
34
GB
I have two forms, Form1 and Form2. In Form1 there are two textBoxes, when i enter data on the textBoxexand press enter, form2 opens. In form2 i have two label, i want the data from the textBoxex in form1 to show up in the labels in form2. Any idea how i would go about showing the values of the textBoxes in the labels of Form2.
Thank You.
 
{this is a commonly asked question, but the keyword search facility isn't working yet}

In .NET, forms are objects, thus, all the object visibility rules apply.

In order to pass a value from one form to another, you need a variable which contains a reference to the other form. Plus, you need a public variable/method on the other form to accept the value you intend to pass.

So, if FormA needs to pass a value to FormB, you'd have code something like this:
Code:
public class FormA
{
   public void btn_Click(/**/)
   {
      FormB otherForm = new FormB();
      otherForm.CustomerName = "John Doe";
      otherForm.Show();
   }
}


public class FOrmB
{
   public string CustomerName()
   {
      set
      {
         this.lblCustomerName = value;
      }
   }
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
With the above code, two errors are thrown:

CustomerName on Form1 does not contain a definition, and on form2, the set variable is not recognised. Any other solutions
Thanks
 
(I just typed my reply to this and hit submit, and tek-tips went "boom!" and I lost everything I wrote. SO - if this appears twice, it's tek-tips fault! ;-)

First, the code provided by ChipH should work - it worked when I tried it. If you did a cut-and-paste into your project, you might have an issue witht he case of FOrmB, and using FormB when you are creating the object.

Secondly, as you say you want to pass in multiple values to populate, I think the public property implementation won't be your best choice because you'll have to create a new public property for each label you want to populate. That will result in some difficult code to maintain, and what happens if in the future someone only sets one property?

I suggest one of two implementations:

1) override the constructor for FormB, like this:

Code:
public Form2(string Label1Text, string Label2Text)
{
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   label1.Text = Label1Text;
   label2.Text = Label2Text;
}

Then, in FormA:

Code:
private void button1_Click(object sender, System.EventArgs e)
{
   Form2 frm2 = new Form2(textBox1.Text, textBox2.Text);
   frm2.ShowDialog();
}

In this example, understand taht you have to call InitalizeComponent() before you set the label values. That's because the labels are added to the controls collection of hte form in InitializeComponent().

If you do this with the constructor, it's one call made when you are instantiating the frm2 object from Form1.

The other way you can do this is:

Code:
... // inside Form2
public void setLabelText(string Label1Text, string Label2Text)
{
   label1.Text = Label1Text;
   label2.Text = Label2Text;
}

Then, in Form1

Code:
private void button1_Click(object sender, System.EventArgs e)
{
   Form2 frm2 = new Form2();
   frm2.setLabelText(textBox1.Text, textBox2.Text);
   frm2.ShowDialog();
}

This solution is better if you aren't destroying and recreating Form2 between changing the text.

Hope this helps!

______________________________________________
When told, "goto hell", a programmer finds the method, not the destination as harmful.
 
Yes, cutting & pasting my code won't work -- it was meant to illustrate the point only, and I typed it in from memory.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Fantastic it work perfectly. One other thing, the secong label, i dont want the text to be visible to the viewer. Is there anyway where the text is on the form but no else knows about it except the creator (not visible to anyone else).
Thanks
Mac
 
Forms are classes, so you can just use a private variable that is accessed via a property. No visual component needed.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Sacheson, no one gave you a star, so I am letting you know that your answer above helped me solve a problem. Passing parameters to a constructor seems like a good solution for this and it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top