【问题标题】:WinForms raise event from another FormWinForms 从另一个表单引发事件
【发布时间】:2017-02-12 02:33:30
【问题描述】:

我有一个名为 MainForm 的主窗体和一个名为 ChildForm 的子窗体 我想填充 ChildForm 的文本框,并在 MainForm_ButtonClick 中触发 ChildForm_ButtonClick 事件。

子窗体:

public partial class ChildForm :Form
  {
        public delegate void delPassData(TextEdit text);

 private void button1_Click(object sender, EventArgs e)
    {
       string depart = "";

       MainForm mfrm = new MainForm();
       delPassData del = new delPassData(frm.funData);
       del(this.Item_CodeTextEdit);
    }
}

主窗体:

public partial class MainForm : Form
 {

 public void funData(TextEdit txtForm1)
    { 
        string ss = "";
        ss = txtForm1.Text;
        MessageBox.Show(ss);
    }

  private void NavigationPanelBtns_ButtonClick(object sender, ButtonEventArgs e)
    {
        switch (e.Button.Properties.Caption)
        {
            case "Save":
             // i want to call funData() here but i get an empty messageBox
            break;
        }
    }

}

【问题讨论】:

  • 看起来像一个非常初学者的问题(如this one)。更努力地使用搜索!
  • @kyr 你为什么不在你的问题中发布那部分?所以会很容易理解和回答
  • 您应该为此使用 MVC(模型视图控制器)或 MVP(模型视图演示器)方法。这是一个示例:stackoverflow.com/questions/15605161/…

标签: c# winforms delegates


【解决方案1】:

子窗体

public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
        MainForm.OnChildTextChanged += MainForm_OnChildTextChanged;
        MainForm.OnButtonClick += MainForm_OnButtonClick;
        bttn1.Visible = false;
    }

    void MainForm_OnButtonClick(object sender, EventArgs e)
    {
        this.bttn1.PerformClick();
    }

    void MainForm_OnChildTextChanged(string value)
    {
        this.textBox1.Text = value;
    }

    private void bttn1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("I am hide. But shows message");
    }

}

public class Bttn : Button
{
    public new void PerformClick()
    {
        this.OnClick(EventArgs.Empty);
    }
}

创建父表单

public partial class MainForm : Form
{
    public delegate void OnMyTextChanged(string value);
    public delegate void ButtonClicked(object sender, EventArgs e);

    public static event OnMyTextChanged OnChildTextChanged;
    public static event ButtonClicked OnButtonClick;

    ChildForm frm = new ChildForm();

    public MainForm()
    {
        InitializeComponent();
        frm.Show();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        OnChildTextChanged("this is new value");
        OnButtonClick(sender, e);
    }
}

【讨论】:

  • 非常感谢您的回复。你知道我在哪里可以阅读代表的一些例子吗?
  • 我已经用事件更新了答案。如果有用,请标记答案:)
  • 这正是我所需要的。对不起,我无法更好地解释它。非常感谢。
  • 我还想问更多问题。如果我设置 button1.Visible = false;然后 this.button1.PerformClick();不起作用。我不希望 button1 在运行时可见。
  • 这是自定义要求。所以需要创建一个自定义按钮来解决这个问题。请找到更新的答案
【解决方案2】:

以其他形式访问文本框:

  1. 在子窗体中将文本框的Modifier 属性设置为public

  2. 在主窗体中,通过子窗体的对象访问文本框。

    例如:

    obj.txtBox.Text="MyValue";
    

以另一种形式访问事件:

  1. 设置事件处理函数为public

  2. 通过传递null作为表单对象的参数来调用函数。

    例如:

    obj.MyButton_Click(null, null);
    

【讨论】:

  • 关于MyButton_Click(null, null);,不要设置事件public,不好。而是将MyButton 设置为公开并调用类似MyButton.PerformClick() 的事件。
  • 我试过 MyButton.PerformClick();但它只适用于 Form_Load 事件。
猜你喜欢
  • 2015-05-08
  • 2019-08-06
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多