开发者

How to carry text values over to a text box on another form?

开发者 https://www.devze.com 2023-02-19 19:37 出处:网络
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:

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.una开发者_JAVA技巧me'


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


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消