简体   繁体   English

将事件从一种形式发送到另一种形式

[英]Send event from one form to another form

I am really getting hard time to solve this seemingly easy problem: I have one form (Child Form) with a button which when clicked should set focus the textbox on another form (Parent Form). 我真的很难解决这个看似简单的问题:我有一个带有按钮的窗体(子窗体),单击该按钮应将文本框的焦点放在另一窗体(父窗体)上。

I have tried to use some codes as shown below:My question is this the right aproach? 我试图使用如下所示的代码:我的问题是正确的方法吗? And if so how to solve this error message 如果是这样,如何解决此错误消息

Cannot Implicitly Convert type 'System.EventHandler' to 'ChildForm.SomeEventHandler 无法将类型'System.EventHandler'隐式转换为'ChildForm.SomeEventHandler

public class ChildForm : Form
{
    public delegate SomeEventHandler(object sender, EventArgs e);
    public event SomeEventHandler SomeEvent;


}

public class ParentForm : Form
{
    ChildForm child = new ChildForm();
    child.SomeEvent += new EventHandler(this.HandleSomeEvent);

    public void HandleSomeEvent(object sender, EventArgs e)
    {
        this.someTextBox.select();
    }
}

You need to change the assignment from 您需要从

child.SomeEvent += new EventHandler(this.HandleSomeEvent);

to

child.SomeEvent += new ChildForm.SomeEventHandler(this.HandleSomeEvent);

Edit 1: It looks perfect for the use of handling an event in Parent Form when an action occurs in Child form. 编辑1:当子窗体中发生操作时,它看起来非常适合处理父窗体中的事件。 Though it doesn't look thread-safe. 尽管它看起来不是线程安全的。

Also take a look at Form.Activate 还要看看Form.Activate

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

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