我使用以下代码关闭并打开隐藏的表单(通过检查打开的appl)。是否有可能检索一个隐藏的表单并关闭另一个表单对应于同一个表单(在运行时的不同时段隐藏form1)
var frm = Application.OpenForms.OfType<Form>().FirstOrDefault(x => x.GetType() == typeof(FrmAdd_To_Cart));
if (frm != null)
{
frm.Close(); or frm.show();
}
答案 0 :(得分:1)
您可以使用Tag属性识别另一个
中的一个表单FrmAdd_To_Cart formToClose = null;
var frmCartList = Application.OpenForms.OfType<FrmAdd_To_Cart>();
if (frmCartList != null)
{
foreach(FrmAdd_To_Cart frm in frmCartList)
{
// Your logic could be based on the value that you set
// in the Tag property when you create the form
// For example you could have a CustomerID stored in the Tag
// int customerID = Convert.ToInt32(frm.Tag);
// But probably it is better to have custom public property
// in the definition of your FrmAdd_To_Cart form class like
// if(frm.CustomerID == CurrentCustomer.ID)
// .....
// Or if you want to close the form that you identify with the tag
if (this.lblBil.Text == frm.Tag.ToString())
{
formToClose = frm;
break; // exit the loop and then close
// Can't do this here because this will change
// the iterating collection and this is not allowed
// frm.Close();
}
}
if (formToClose != null)
formToClose.Close();
}
请注意,您可以使用OfType扩展来仅获取您感兴趣的表单。这也意味着您返回了IEnumerable,因此您需要使用foreach循环。