如何使用自定义 ShowDialog 在 winfowms 中分配事件处理程序?
我有一个带有多个参数的 ShowDialog。有没有办法也向它传递一个委托来处理父窗体上的事件? (与实例化对话框,然后添加事件,然后调用默认的 ShowDialog 相反)
简化示例,如果我想要对对话框进行更改以立即反映在打开的表单上:
public event NameChangeEventHandler OnChange;
internal DialogResult ShowDialog(string pPersonName)
{
using (diagSample dialog = new siagSample(pPersonName)
{
DialogResult result = dialog.ShowDialog();
return result;
}
}
public diagSample(string pPersonName)
{
InitializeComponent();
_PersonName = pPersonName;
}
编辑:我的主要目标是,我有一个经常使用的每当对话框上的字段发生更改时,该对话框需要调用父窗体上的事件处理程序。除了在每次使用 ShowDialog() 之前实例化 New() 对话框并为其事件处理程序分配一个事件之外,还有其他方法可以做到这一点吗?
I have a ShowDialog that takes several parameters. Is there a way to also pass it a delegate to handle an event on the parent form? (As opposed to instantiating the dialog, then adding the event, then calling the default ShowDialog)
Simplified sample, if I wanted a change on the dialog to reflect instantly on the opening form:
public event NameChangeEventHandler OnChange;
internal DialogResult ShowDialog(string pPersonName)
{
using (diagSample dialog = new siagSample(pPersonName)
{
DialogResult result = dialog.ShowDialog();
return result;
}
}
public diagSample(string pPersonName)
{
InitializeComponent();
_PersonName = pPersonName;
}
Edit: My main goal is, I have a frequently used dialog that needs to call an event handler on the parent form whenever fields on the dialog are changed. Is there a way of doing this besides instantiating a New() dialog and assigning an event to its event handler each time before using ShowDialog()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,当然,您可以在表单构造函数中传递委托实例,就像名称一样。这样做没有任何好处,代码与在对话框表单中声明事件非常相似。只是不标准。
Well, sure, you can pass the delegate instance in the form constructor, just like the name. There is no advantage of doing this, the code will be very similar to having the event declared in the dialog form. Just non-standard.
您可以根据需要分配事件和事件处理程序。这是一些示例代码。有两个表单,每个表单都有一个按钮和一个标签。 Form2 有一个文本框,您可以在其中输入 tesxt。
单击 Form2 上的按钮时,它会更改标签文本并触发 Form1 拾取的事件。然后,Form1 上的标签将更改以与 Form2 上的标签相匹配。
编辑 - 抱歉我没有正确阅读您的问题。
如果您想避免这种情况
那么您可以将 Form2 构造函数更改为:
然后按如下方式设置并显示 Form2:
不幸的是,现在确实有办法避免在分配事件处理程序时使用
+=
语句,因为一旦实例化父类就必须分配它们。当然,您可以实例化一次对话框,直到需要时才显示它,但是您必须处理对话框被关闭和销毁的情况,这意味着不使用 ShowDialog,而是使用 Show,因此需要您放置代码以确保对话框仍然是模态的。
You can assign events and event handlers just as you like. Here's some example code. There's two forms each with a button and a label. Form2 has a text box that you can enter tesxt in.
When the button on Form2 is clicked it changes the label text and fires an event that is picked up by Form1. The label on Form1 then changes to match that on Form2.
edit - Sorry I didn't read your question correctly.
If you want to avoid this
Then you could change the Form2 constructor to:
And then you setup and show Form2 as follows:
Unfortunately there really is now way of avoiding the
+=
statement in assigning event handlers as they must be assigned once the parent class has been instantiated.You could of course instantiate the dialog once and just not show it until required, but then you'll have to cope with the dialog being closed and destroyed, which means not using ShowDialog, but Show instead therefore requiring you to emplace code to ensure that the dialog remains modal.