简体   繁体   English

从另一种形式更改一种形式的文本

[英]Changing text in one form, from another form

Im trying to change the text in Form1 when pushing the button on Form2按下 Form2 上的按钮时,我试图更改 Form1 中的文本

Form 2:表格 2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.textCh = "Text has been changed";
    }
}

Form 1:表格一:

public partial class Form1 : Form
{

    public string textCh { 
        get
        {
            return this.textCh;
        }
        set
        {
            this.label1.Text = value;
        } 
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog();
    }
}

When I'm pushing button nothing happens, the text remain the same.当我按下按钮时,没有任何反应,文本保持不变。

1

Another method of passing Form1 to Form2 via the Show() method and the .Owner property:另一种通过Show()方法和.Owner属性将 Form1 传递给 Form2 的方法:

// In Form1
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass Form1 via "this"

Then, in Form2, you CAST .Owner to type Form1:然后,在 Form2 中,您 CAST .Owner以键入 Form1:

// In Form2
Form1 f1 = this.Owner as Form1;
if (f1!=null && !f1.IsDisposed) 
{
    f1.textCh = "Text has been changed";
}

There are several way to do this here 2 examples这里有几种方法可以做到这一点2个例子

  1. This example use references whitout any use of events这个例子使用了没有使用任何事件的引用

    public partial class Form1: Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var frm = new Form2(this); frm.Show(); } }

here the Form2 is created passing the Form1 as parameter这里Form2是通过Form1作为参数创建的

 public partial class Form2 : Form
{
    Form1 _parent;
    public Form2(Form1 parent)
    {
        _parent = parent;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var lbl = (Label)_parent.Controls.Find("label1", false).First();
        lbl.Text = "new text";
        _parent.Update();
    }
}

Than the Form2 use that for set the value wanted.Form2使用它来设置想要的值。

  1. This example use events此示例使用事件

     public partial class Form1: Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { var frm = new Form2(); frm.UpdateLabelEvent += Frm_UpdateLabelEvent; frm.Show(); } private void Frm_UpdateLabelEvent(string str) { label1.Text = str; } }

and here the code of Form2这里是Form2的代码

 public partial class Form2 : Form
{

    public event Action<string> UpdateLabelEvent;
    
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UpdateLabelEvent("new string value");
    }
}

Nothing changes in your Form1 because you are creating a new Form1 first and change the text there. Form1 中没有任何变化,因为您首先创建一个新的 Form1 并更改那里的文本。 The new Form1 is never shown, so you see no changes.新的 Form1 从未显示,因此您看不到任何更改。

Solution解决方案

 private void button1_Click(object sender, EventArgs e)
{
    //Form1 f1 = new Form1(); GET rid of this line
    f1.textCh = "Text has been changed";
}

you need to make sure off course that f1 is known in form2, if you dont know how here is a simple way to do that你需要确保在form2中知道f1,如果你不知道这是一个简单的方法来做到这一点

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.f1 = this;
    f2.ShowDialog();
}

public Form2()
{
    public Form1 f1 { get; set; }
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM