Changing text box from another class

2019-02-22 03:43发布

I'm trying to change value of a text box located in

public partial class Form1 : Form

from another class. I've tried something like this

public void echo(string text)
{
    this.textBox1.AppendText(text + Environment.NewLine);
}

From another class I'm calling it like

Form1 cout = new Form1();
cout.echo("Does this work?");

And I get blank output. I also tried to add the static keyword to the echo method, but I got the same result. I searched over Stack Overflow and didn't get any solution to work. And one thing that triggers me, if I add cout.Show() the same form pop out with valid textBox1 content. Why is that?

Why it is not showing content right away? And how do I fix this?

5条回答
手持菜刀,她持情操
2楼-- · 2019-02-22 04:16

Instead of cout try using MessageBox.Show("Does this work?");

Now sending textbox value from one form to another.

protected void btnNext_Click(object sender, EventArgs e)
{
  MyForm2 x = new MyForm2();
  x.Query = "My Query";   // here "Query" is your custom public string variable on form2
  x.Show()
}
查看更多
戒情不戒烟
3楼-- · 2019-02-22 04:22

The problem is here:

Form1 cout = new Form1() ;
cout.echo("Does this work?");

You're creating a new version of your main form, Form1.

What is this other class, and how is it being instantiated?

You have two options:

  1. When your code in Form1 creates the class, give him an instance to this, and call your echo method on that reference to (the only) instance of Form1.

  2. Add an event to this other class, that is fired when he wants to provide some information. Your Form1 code will register an event handler on this event, and make the call to echo himself, when the event fires.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-22 04:37

You're making an entirely new Form1. The reason one appears when you call Show() is because you are showing the new window you've made. I'm not sure what your actual intent is, because I don't know where you're calling this from, but what you want to do is call echo for the Form1 that has already been created.

查看更多
成全新的幸福
5楼-- · 2019-02-22 04:38

You don't need to create another object of Form1.

Try this code and I think, you will guess what is happening:

Form1 cout = new Form1();
cout.Show();
cout.echo("Does this work?");
查看更多
欢心
6楼-- · 2019-02-22 04:41

Each time you say new Form1(), you are creating a distinct and separate instance of that form. Instead, you need to create a variable in the class that you are trying to access your form. For example, let's pass it in the constructor:

public class MyClass {
    public Form1 MyForm;

    public MyClass(Form1 form){
        this.MyForm = form;
    }

    public void echo(string text) {
        this.MyForm.textBox1.AppendText(text + Environment.NewLine);            
    }

}

Notice that you access the particular instance of Form1 in your echo method:

public void echo(string text) {
     this.MyForm.textBox1.AppendText(text + Environment.NewLine);
}
查看更多
登录 后发表回答