您可以在图像上看到具有暗淡背景的模态形式,这就是我的操作方式。
我有两个表格,同时正在打开它们。
private DimBackground dim;
private Modal modal;
private void ListEmployee_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
var row = employee.ListEmployee.Rows[e.RowIndex];
Employee emp = new Employee().Get(row.Cells["Employee_ID"].Value.ToString());
dim = new DimBackground();
dim.Show();
modal = new Modal(emp,dim);
modal.ShowDialog();
}
}
对我来说,这不是一个好方法。这样做有问题
要解决此问题,我关闭模式形式并隐藏暗淡形式
当我尝试禁用表单关闭时,表单被冻结
我怎样才能以更好/更清洁的方式实现这一目标?
答案 0 :(得分:1)
可能适用于您变暗的叠加形式和模式形式的修改:
[DimBackground].Show(this);
),以便覆盖可以使用所有者对象来适应其尺寸。 FormClosed
事件。 FormClosed
事件,则覆盖图会自行关闭并激活调用者窗体。 添加所需的null
检查。
//Caller Form
private void ListEmployee_CellClick(object sender, DataGridViewCellEventArgs e)
{
//(...)
var row = employee.ListEmployee.Rows[e.RowIndex];
Employee emp = new Employee().Get(row.Cells["Employee_ID"].Value.ToString());
DimBackground overlay = new DimBackground(new Modal(emp));
overlay.Show(employee);
}
public partial class DimBackground : Form
{
Form frmDialog = null;
public DimBackground(Form dialog)
{
InitializeComponent();
this.frmDialog = dialog;
this.frmDialog.FormClosed += (obj, evt) => { this.Close(); };
}
private void DimBackground_Shown(object sender, EventArgs e) => this.frmDialog.ShowDialog();
private void DimBackground_Load(object sender, EventArgs e)
{
if (this.Owner == null) return;
this.Bounds = this.Owner.Bounds;
}
private void DimBackground_FormClosed(object sender, FormClosedEventArgs e)
{
this.Owner?.Activate();
}
}
public partial class Modal : Form
{
public Modal() : this(null) { }
public Modal(Employee employee)
{
InitializeComponent();
// Do something with the employee object
}
}