gpt4 book ai didi

c# - 当我处理 UserControl 时,关闭由 UserControl 的按钮打开的模态窗体

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:14 31 4
gpt4 key购买 nike

我有一个应用程序,我需要确保在使用 ShowDialog() 单击用户控件上的按钮打开的表单将在我处理用户控件时关闭和处理。

我通过计时器在主窗体中调用 userControl.Dispose()

有什么办法可以做到吗?

谢谢...

以下是有关表单流程的更多详细信息:

我的应用程序的 MainForm 正在创建一个 UserControl,它有一个 Button。当用户单击用户控件的按钮时,它会使用 ShowDialog 显示模型表单。

与此同时,几分钟后,主窗体中的计时器将现有用户控件替换为用户控件的另一个实例。主窗体调用上一个用户控件的Dispose 方法,然后显示新的。

但问题是模态对话框仍然在屏幕上打开,挡住了主窗体。我想关闭它,放在ShowDialog方法后面的代码应该不会执行。

最佳答案

简答

您可以订阅 UserControlDisposed 事件并关闭它显示的表单。关于问题下的评论,看起来你有一个包含 ButtonUserControl 并且在按钮的 Click 事件中,你显示了一个Form 使用 ShowDialog()

要关闭和处置表单,您需要订阅 UserControlDisposed 事件,将表单显示为对话框之前。

更多详情

如果你想根据表单的对话结果决定运行一些逻辑,你可以检查对话结果,如果没问题,运行你需要的自定义逻辑。

为了稍微增强流程,您可以在用户控件中定义一些事件和属性,并在主窗体中处理它们:

  • OKSelected 事件,如果对话框结果正常,可以在关闭对话框后立即触发。它会让您在主窗体中处理此事件,例如,如果用户在对话框中单击“确定”,则停止计时器。
  • ProcessingFinished,当对话框结果正常时,您可以在关闭对话框后完成一些处理后引发它。您可以在主窗体中处理它,例如再次启动计时器。
  • 您可以定义一些属性,以防您想与主窗体传递一些值。

下面是主要形式的代码示例:

MyUserControl uc = null;
private void timer1_Tick(object sender, EventArgs e)
{
if (!(uc == null || uc.IsDisposed || uc.Disposing))
{
this.Controls.Remove(uc);
uc.Dispose();
}
uc = new MyUserControl();
this.Controls.Add(uc);
uc.OKSelected += (obj, args) => { timer1.Stop(); };
uc.ProcessingFinished += (obj, args) =>
{
MessageBox.Show(uc.Info);
timer1.Start();
};
}

这是用户控件的示例:

public partial class MyUserControl : UserControl
{
public MyUserControl() { InitializeComponent(); }
public EventHandler OKSelected;
public EventHandler ProcessingFinished;
public string Info { get; private set; }
private void button1_Click(object sender, EventArgs e)
{
using (var f = new Form()) {
var button = new Button() { Text = "OK" };
f.Controls.Add(button);
button.DialogResult = DialogResult.OK;
this.Disposed += (obj, args) => {
if (!(f.IsDisposed || f.Disposing)) {
f.Close(); f.Dispose();
}
};
if (f.ShowDialog() == DialogResult.OK) {
//If you need, raise the OKSelected event
//So you can handle it in the main form, for example to stop timer
OKSelected?.Invoke(this, EventArgs.Empty);
//
//Do whatever you need to do after user closed the dialog by OK
//
//If you need, raise the ProcessingFinished event
//So you can handle it in the main form, for example to start timer
//You can also set some properties to share information with main form
Info = "something";
ProcessingFinished?.Invoke(this, EventArgs.Empty);
}
}
}
}

关于c# - 当我处理 UserControl 时,关闭由 UserControl 的按钮打开的模态窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59084045/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com