C# 情态形式问题中的情态形式
我的主窗体中的一个按钮执行以下操作:C# 情态形式问题中的情态形式,c#,modal-dialog,C#,Modal Dialog,我的主窗体中的一个按钮执行以下操作: private void buttonGabarito_Click(object sender, EventArgs e) { FormGabarito fg = (FormGabarito)Application.OpenForms["FormGabarito"]; if (fg == null) { fg = new FormGaba
private void buttonGabarito_Click(object sender, EventArgs e)
{
FormGabarito fg = (FormGabarito)Application.OpenForms["FormGabarito"];
if (fg == null)
{
fg = new FormGabarito(this);
}
fg.ShowDialog();
}
然后,在我的“FormGabarito”中有一个“saveButton”
我的问题是:当我通过选择路径或单击取消来关闭SaveFileDialog时,我的FormGabarito也会关闭!和我的主要形式,哈哈
我添加了以下行
this.DialogResult = DialogResult.None;
之后
现在,只有在单击SaveFileDialog中的cancel按钮时,才会出现此问题。
为什么会这样?如何修复它我尝试用类似的代码重新创建该问题,但没有出现错误 我猜它一定是导致了以下代码的错误(许可或其他原因)
//Manager.DtGabarito.WriteXml(fb.FileName);
//UpdateAll();
工作代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 fg = (Form2)Application.OpenForms["Form2"];
if (fg == null)
{
fg = new Form2();
}
fg.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var fb = new SaveFileDialog();
fb.Filter = "xml file (*.xml) | *.xml";
var res = fb.ShowDialog();
if (fb.FileName != null)
{
MessageBox.Show(fb.FileName);
//Manager.DtGabarito.WriteXml(fb.FileName);
//UpdateAll();
}
}
}
//Manager.DtGabarito.WriteXml(fb.FileName);
//UpdateAll();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 fg = (Form2)Application.OpenForms["Form2"];
if (fg == null)
{
fg = new Form2();
}
fg.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var fb = new SaveFileDialog();
fb.Filter = "xml file (*.xml) | *.xml";
var res = fb.ShowDialog();
if (fb.FileName != null)
{
MessageBox.Show(fb.FileName);
//Manager.DtGabarito.WriteXml(fb.FileName);
//UpdateAll();
}
}
}