我有一个派生自形式的课程。在该类中,我将ShowDialog事件隐藏在我自己的实现之后,它会巧妙地禁用所有其他表单,显示对话框,然后重新启用它们。它工作得很好。将显示一个对话框,所有其他表单都显示为灰色。
问题是这样做可以防止在焦点返回到调用表单时触发Activated事件 - 我依赖于其他代码。
我假设激活事件未被触发,因为当焦点返回时,表单全部被禁用。为了解决这个问题,我尝试手动触发事件以激活表单
if (fParent != null) fParent.Activate();
为了使事情复杂化,在这种情况下,ParentForm是一个mdiChildForm,其激活事件是从父表单的activatedMDIChild事件中继的
在这种情况下,我如何能够激活被激活的事件?
public DialogResult ShowDialog()
{
return setFormsToBackground(null);
}
public DialogResult ShowDialog(Form fParent)
{
return setFormsToBackground(fParent);
}
private DialogResult setFormsToBackground(Form fParent)
{
List<Form> lstFormsToEnable = new List<Form>();
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
try
{
Form checkfrm = Application.OpenForms[i];
if (checkfrm != this && checkfrm.Enabled)
{
lstFormsToEnable.Add(checkfrm);
checkfrm.Enabled = false;
}
}
catch (Exception ex)
{
}
}
DialogResult result = DialogResult.None;
if (fParent == null) result = base.ShowDialog();
else result = base.ShowDialog(fParent);
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
try
{
Form checkfrm = Application.OpenForms[i];
checkfrm.Enabled = true;
}
catch (Exception ex)
{
}
}
if (fParent != null) fParent.Activate();
return result;
}
答案 0 :(得分:0)
private DialogResult setFormsToBackground(Form fParent)
{
Form dummyForm = new Form();
dummyForm.ShowInTaskbar = false;
dummyForm.FormBorderStyle = FormBorderStyle.None;
dummyForm.Load += ((object sender, EventArgs e) => { (sender as Form).Size = new Size(0, 0); });
List<Form> lstFormsToEnable = new List<Form>();
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
try
{
Form checkfrm = Application.OpenForms[i];
if (checkfrm != this && dummyForm != this && checkfrm.Enabled)
{
lstFormsToEnable.Add(checkfrm);
checkfrm.Enabled = false;
}
}
catch (Exception ex)
{
}
}
dummyForm.Show();
DialogResult result = DialogResult.None;
if (fParent == null) result = base.ShowDialog();
else result = base.ShowDialog(fParent);
for (int i = lstFormsToEnable.Count - 1; i >= 0; i--)
{
try
{
Form checkfrm = Application.OpenForms[i];
checkfrm.Enabled = true;
}
catch (Exception ex)
{
}
}
dummyForm.Close();
if (fParent != null) fParent.Focus();
return result;
}