如何使用自定义 ShowDialog 在 winfowms 中分配事件处理程序?

发布于 2024-09-15 23:47:25 字数 631 浏览 4 评论 0原文

我有一个带有多个参数的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你げ笑在眉眼 2024-09-22 23:47:25

好吧,当然,您可以在表单构造函数中传递委托实例,就像名称一样。这样做没有任何好处,代码与在对话框表单中声明事件非常相似。只是不标准。

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.

总攻大人 2024-09-22 23:47:25

您可以根据需要分配事件和事件处理程序。这是一些示例代码。有两个表单,每个表单都有一个按钮和一个标签。 Form2 有一个文本框,您可以在其中输入 tesxt。
单击 Form2 上的按钮时,它会更改标签文本并触发 Form1 拾取的事件。然后,Form1 上的标签将更改以与 Form2 上的标签相匹配。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 dlg = new Form2();
            dlg.NameChanged += OnNameChanged;
            dlg.ShowDialog();
        }

        private void OnNameChanged(object sender, NameChangeEventArgs args)
        {
            this.label1.Text=args.NewName;
        }
    }
    public class NameChangeEventArgs : EventArgs
    {
        public NameChangeEventArgs(string name)
        {
            this.NewName=name;
        }
        public string NewName{get;private set;}
    }
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text;
            if (this.NameChanged != null)
            {
                this.NameChanged(this, new NameChangeEventArgs(label1.Text));
            }
        }

        public event EventHandler<NameChangeEventArgs> NameChanged;
    }
}

编辑 - 抱歉我没有正确阅读您的问题。

如果您想避免这种情况

        Form2 dlg = new Form2();
        dlg.NameChanged += OnNameChanged;
        dlg.ShowDialog();

那么您可以将 Form2 构造函数更改为:

public Form2(EventHandler<NameChangeEventArgs> eventhandler)
{
    InitializeComponent();
    NameChanged += eventhandler;
}

然后按如下方式设置并显示 Form2:

    Form2 dlg = new Form2(OnNameChanged);
    dlg.ShowDialog();

不幸的是,现在确实有办法避免在分配事件处理程序时使用 += 语句,因为一旦实例化父类就必须分配它们。
当然,您可以实例化一次对话框,直到需要时才显示它,但是您必须处理对话框被关闭和销毁的情况,这意味着不使用 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.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 dlg = new Form2();
            dlg.NameChanged += OnNameChanged;
            dlg.ShowDialog();
        }

        private void OnNameChanged(object sender, NameChangeEventArgs args)
        {
            this.label1.Text=args.NewName;
        }
    }
    public class NameChangeEventArgs : EventArgs
    {
        public NameChangeEventArgs(string name)
        {
            this.NewName=name;
        }
        public string NewName{get;private set;}
    }
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text;
            if (this.NameChanged != null)
            {
                this.NameChanged(this, new NameChangeEventArgs(label1.Text));
            }
        }

        public event EventHandler<NameChangeEventArgs> NameChanged;
    }
}

edit - Sorry I didn't read your question correctly.

If you want to avoid this

        Form2 dlg = new Form2();
        dlg.NameChanged += OnNameChanged;
        dlg.ShowDialog();

Then you could change the Form2 constructor to:

public Form2(EventHandler<NameChangeEventArgs> eventhandler)
{
    InitializeComponent();
    NameChanged += eventhandler;
}

And then you setup and show Form2 as follows:

    Form2 dlg = new Form2(OnNameChanged);
    dlg.ShowDialog();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文