旗下导航:搜·么
当前位置:网站首页 > .Net教程 > 正文

从0自学C#05--窗体之间的互相接见【C#.Net教程】,C#,窗体之间,相互访问

作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:70评论:0


导读:在父窗体上,转变CheckBox控件的状况,完成子窗体的翻开和封闭。在子窗体上,点击封闭按钮后,父窗体CheckBox控件变成未选中状况。1.要领这里用托付事宜的要领,...
在父窗体上,转变CheckBox控件的状况,完成子窗体的翻开和封闭。在子窗体上,点击封闭按钮后,父窗体CheckBox控件变成未选中状况。

1.要领

这里用托付事宜的要领,完成窗体的互相接见。

2.父窗体(主窗体)

父窗体上安排5个CheckBox控件。并将他们赋值到CheckBox[]数组,以便代码举行轮回挪用。

CheckBox[] checkBox;public MainFormBERT()
        {
            InitializeComponent();            
            checkBox = new CheckBox[5];

            checkBox[0] = this.checkBox1;
            checkBox[1] = this.checkBox2;
            checkBox[2] = this.checkBox3;
            checkBox[3] = this.checkBox4;            
            checkBox[4] = this.checkBox5;            
        }

给CheckBox控件增加事宜。并定义OpenOrCloseSubFormPPG(int i)要领完成翻开或封闭子窗体。该要领挪用了子窗体的事宜,以相应RecvInfo(int number)要领,设置CheckBox控件为未选中状况:

subFormPPGTx[i].SendToParent += new SubFormPPG.SendFun(RecvInfo);
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            OpenOrCloseSubFormPPG(0);
        }private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            OpenOrCloseSubFormPPG(1);
        }private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            OpenOrCloseSubFormPPG(2);
        }private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            OpenOrCloseSubFormPPG(3);
        }private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            OpenOrCloseSubFormPPG(4);
        }

SubFormPPG[] subFormPPGTx = { null, null, null, null, null};private void OpenOrCloseSubFormPPG(int i)
        {            
            try
            {                if (checkBox[i].Checked)
                {                    string formTitle, formName;                    if (i < 0 || i > 4)
                    {                        throw new IndexOutOfRangeException("Channel is out of range");
                    }                    else if (i == 4)
                    {
                        formTitle = "ParallelWrite";
                        formName = "subFormPPG" + formTitle;
                    }                    else
                    {
                        formTitle = "Tx" + i;
                        formName = "subFormPPG" + formTitle;
                    }                    if (subFormPPGTx[i] == null || subFormPPGTx[i].IsDisposed)
                    {
                        subFormPPGTx[i] = new SubFormPPG();
                        subFormPPGTx[i].Text = formTitle;
                        subFormPPGTx[i].Name = formName;
                        subFormPPGTx[i].Tag = i;
                        subFormPPGTx[i].SendToParent += new SubFormPPG.SendFun(RecvInfo);//挪用子窗体的事宜
                        subFormPPGTx[i].Show(this);
                    }                    else
                    {
                        subFormPPGTx[i].WindowState = FormWindowState.Normal;
                        subFormPPGTx[i].Activate();
                    }
                }                else
                {                    //subFormPPGTx[0].Close();
                    subFormPPGTx[i].Dispose();
                    subFormPPGTx[i] = null;
                }
            }            catch (IndexOutOfRangeException ex)
            {
                MessageBox.Show(ex.Message);
            }            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }private void RecvInfo(int number)
        {            this.checkBox[number].Checked = false;
        }

3.子窗体

增加事宜SendToParent,当子窗体封闭时,相应事宜,将信息传递给父窗体。

public delegate void SendFun(int number);
public event SendFun SendToParent;
private void SubFormPPG_FormClosed(object sender, FormClosedEventArgs e)
        {            if (SendToParent != null)
            {
                SendToParent((int)this.Tag);
            }             
        }

以上就是 从0自学C#05--窗体之间的互相接见的内容,更多相关内容请关注ki4网(www.ki4.cn)!

标签:C#窗体之间相互访问


欢迎 发表评论: