0

I am attempting to carry the value of a single textbox into another textbox on another form.

In form 1 i have the following code:

private SecondForm secondForm;
public void SecondFormTextBox()
{
    uname.Text = secondForm.uname.Text;
}

In form 2 I have the following code.

public TextBox uname
{
    get
    {
        return uname;
    }
}

I recieve the following errors, hope you can help!!

An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.AdministratorHome.uname.get'

Ambiguity between 'WindowsFormsApplication1.AdministratorHome.uname' and 'WindowsFormsApplication1.AdministratorHome.uname'

Steven Ryssaert
  • 1,989
  • 15
  • 25
Joe Bell
  • 209
  • 2
  • 11
  • 19
  • possible duplicate of [C# beginner help, How do I pass a value from a child back to the parent form?](http://stackoverflow.com/questions/280579/c-beginner-help-how-do-i-pass-a-value-from-a-child-back-to-the-parent-form) – Mitch Wheat Mar 23 '11 at 15:32
  • Within the call to uname (which is a property method), you're returning the value of the same property, is kinda recursive so It won't compile. – james_bond Mar 23 '11 at 15:32
  • 1
    Instead of declaring private SecondForm secondForm; declare it as Public – Developer Mar 23 '11 at 15:33
  • I think you will need to show us a bit more code. That error likely means you treating a class name like a variable and trying to make a static call and not an instance call. – madmik3 Mar 23 '11 at 15:33

4 Answers4

4

Lets do an exampe with passing a value from textBox on form1 on button press, to a textBox on form2. Code:

//form1:
Form2 form2;

private void button1_Click()
{
   if(form2 == null)
   {
       form2 = new Form2();
       form2.Show(); 
   }
   form2.PassToForm2(textBox1.Text);
}

//form2:
private void PassToForm2(string msg)
{
   textBox1.Text = msg;
}

Hope it helps, Mitja

Mitja Bonca
  • 4,268
  • 5
  • 24
  • 30
1

In the above code snippet, the property get is referencing itself; that's a bad idea(tm). If you want to get the text value, you would have some kind of property like so:

public string TextBoxText
{
   get { return myTextBox.Text; } // myTextBox is the symbol for your text box
}

Then the other form can simple access that text via the property. Next up, in your first code snippet, you haven't assigned an instance to your second form variable; it's null.

Tejs
  • 40,736
  • 10
  • 68
  • 86
0

First of all TextBox uname doesn't have setter - or at least you haven't pasted it.

Second, do you set secondForm property in Form1 to the instance of Form2 (for example in the constructor)?

Rafal Spacjer
  • 4,838
  • 2
  • 26
  • 34
0

From the code you have provided, it does not look like you are creating an instance to the secondForm. If you are doing so, can you please provide more code.

Alex Mendez
  • 5,120
  • 1
  • 25
  • 23