C# Winform在父窗体关闭子窗体

Timer 控件循环执行,如果子窗口已经打开就执行语句关闭 子窗口, 如果没有打开就打开子程序

FormCollection childCollection = Application.OpenForms;
for (int i = childCollection.Count; i-- > 0;)
{
    if (childCollection[i].Name != "Monitor") childCollection[i].Close();
}
        private Form open = null;
        private void timer1_Tick(object sender, EventArgs e)
        {
            DataSet dt = SqlHelper.ExecuteDataset(connectionBu, "SUZ_UDP_PTS_GetMonitorConfig", BU, Type);
            if (dt.Tables[0].Rows.Count > 0)
            {
                string connectionudp = dt.Tables[0].Rows[0][2].ToString();
                string UdpName = dt.Tables[0].Rows[0][3].ToString();
                ds = SqlHelper.ExecuteDataset(connectionudp, UdpName);

                if (ds.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        string LastSN = ds.Tables[0].Rows[i][1].ToString();
                        string NextSN = ds.Tables[0].Rows[i][2].ToString();
                        string Line = ds.Tables[0].Rows[i][3].ToString();
                        string Station = ds.Tables[0].Rows[i][4].ToString();
                        if (open == null || open.IsDisposed)//先判断子程序是否已经打开 ,如果已经打开就关闭
                        {
                            FormCollection childCollection = Application.OpenForms;
                            for (int j = childCollection.Count; j-- > 0;)
                            {
                                if (childCollection[j].Name != "Monitor") childCollection[j].Close();
                            }
                            Show aa = new Show();
                            aa.Show();
                        }
                        else
                        {

                            Show aa = new Show();
                            aa.Show();

                        }
                    }
                }
                else
                {

                    FormCollection childCollection = Application.OpenForms;
                    for (int i = childCollection.Count; i-- > 0;)
                    {
                        if (childCollection[i].Name != "Monitor") childCollection[i].Close();
                    }
                }
            }
            else
            {
                MessageBox.Show("没有配置Server/UDP , 请配置数据!(table :SUZ_UDT_GetMonitorConfig)");
            }

        }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值