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)");
            }

        }

你可能感兴趣的:(Winform)