我有一个使用ShowDialog()显示的表单,因此它是模式窗口。
private void OpenForm(object sender, ItemClickEventArgs e)
{
MyForm testForm = new MyForm();
...
testForm.Enabled = true;
testForm.ShowDialog(this);
var dialogOk = testForm.DialogOK;
if(dialogOk)
{
//do some stuff 1
}
}
表单上有一个“确定”按钮。单击确定时,将DialogOk设置为true。在MyForm类中:
private void OkClick(object sender, EventArgs e)
{
// do some stuff 2
...
DialogOK = true;
Hide();
}
我需要将其转换为非模态窗口。解决方案似乎是使用Show()而不是ShowDialog(),但是当我使用Show()时,代码不会停止并等待单击OK按钮,因此永远不会调用“ do something something 1”。
使用Show(),如何在单击“确定”按钮后保持行为“运行一些东西1”?
更新:这是我现在要尝试的内容:
public partial class MyForm: XtraForm
{
public bool DialogOk;
private void OkClick(object sender, EventArgs e)
{
// do some stuff 2
...
DialogOk = true;
Close();
}
}
方法1:
public partial class MyMainForm : XtraForm
{
private MyForm testForm;
private void OpenForm(object sender, ItemClickEventArgs e)
{
if(testForm == null)
{
testForm = new MyForm();
}
...
testForm.Enabled = true;
testForm.FormClosed += (s, a) => {
var dialogOk = testForm.DialogOk;
if (dialogOk)
{
// do some stuff 1
}
};
testForm.Show(this);
}
}
方法2:
public partial class MyMainForm : XtraForm
{
private MyForm testForm;
private void OpenForm(object sender, ItemClickEventArgs e)
{
if(testForm == null)
{
testForm = new MyForm();
}
...
testForm.FormClosed += testForm_Closed;
testForm.Show(this);
}
private void testForm_Closed(object sender, EventArgs args)
{
var testForm = (Form)sender;
testForm.Closed -= testForm_Closed;
if (testForm.DialogResult == DialogResult.OK)
{
// do some stuff 1
}
}
}
答案 0 :(得分:3)
您可以处理Form.Closed
事件:
MyForm testForm = new MyForm();
testForm.Closed += testForm_Closed;
testForm.Show();
private void testForm_Closed(object sender, EventArgs args)
{
var testForm = (Form)sender;
testForm.Closed -= testForm_Closed;
if (testForm.DialogResult == OK)
// do some stuff 1
}
答案 1 :(得分:1)
最简单的方法是将代码从OpenForm
移动到事件处理程序OkClick
。但是,如果这不是放置代码的好地方,因为您可能想使用相同的表单执行不同的任务,则可以为FormClosed
事件添加处理程序,该事件在表单关闭并运行后调用代码,例如:
private void OpenForm(object sender, ItemClickEventArgs e)
{
MyForm testForm = new MyForm();
...
testForm.Enabled = true;
testForm.FormClosed += (s, a) => {
var dialogOk = testForm.DialogOK;
if(dialogOk)
{
//do some stuff 1
}
};
testForm.Show(this);
}
答案 2 :(得分:0)
您可以使用与TaskCompletionSource
绑定的异步事件处理程序,该处理程序可以监听并唤醒表单的结束
overlapped ID idd iduser iddate name beg end date_uploaded
UNIQUE 52 -1907372231 666 201802 sol 2018-09-01 2018-09-10 2018-09-12
OVER 53 -1907372231 666 201802 sol 2018-09-10 2018-09-12 2018-09-13
使用可以保持逻辑在private asyc void OpenForm(object sender, ItemClickEventArgs e) {
var source = new TaskCompletionSource<DialogResult>();
EventHandler handler = null;
handler = (s, args) => {
var form = (MyForm)s;
form.FormClosed -= handler;
source.SetResult(form.DialogResult);
}
var testForm = new MyForm();
testForm.FormClosed += handler; //subscribe
//...
testForm.Enabled = true;
testForm.Show();
var dialogOk = await source.Task;
if(dialogOk == DialogResult.Ok) {
//do some stuff 1
}
}
,并使得代码等待而不会阻塞。
在表单中,单击按钮后,您只需设置对话框结果并关闭表单即可。
OpenForm
答案 3 :(得分:0)
这对我有用,所以不确定为什么不适合您(抓头)...此表单有两个按钮,一个按钮再次打开相同的表单,另一个按钮关闭表单。 “父”表单将事件添加到“已关闭”事件中。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 test = new Form1();
test.FormClosed += Test_FormClosed;
test.Show();
}
private void Test_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("closed -- do something else here!");
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
}