新寫的一個委託程式!目的為了鞏固!委託的用法!

來源:互聯網
上載者:User

代碼如下:public delegate void selectLable(string e);
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkBox1.Enabled = false;
            checkBox2.Enabled = false;
            checkBox3.Enabled = false;
            if (str == "")
            {
                label1.Text = "請輸入字串!";
            }
        }
        public string str { get { return textBox1.Text.ToString(); } set { str = value; } }
       

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true && checkBox2.Checked == false && checkBox3.Checked == false)
            {
                label1.ForeColor = System.Drawing.Color.Red;
            }
            else if (checkBox1.Checked == false && checkBox2.Checked == true && checkBox3.Checked == false)
            {
                label1.ForeColor = System.Drawing.Color.Green;
            }
            else if (checkBox1.Checked == false && checkBox2.Checked == false && checkBox3.Checked == true)
            {
                label1.ForeColor = System.Drawing.Color.Black;
            }
            else
            {
                label1.Text = str;
            }
        }
        public event selectLable onselectLable;
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                checkBox1.Checked = radioButton1.Checked;
                checkBox2.Checked = false;
                checkBox3.Checked = false;
                if (radioButton1 != null)
                {
                    onselectLable(radioButton1.Checked.ToString());
                }
            }
            catch {
                label1.Text = str;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                checkBox1.Checked = false;
                checkBox2.Checked = radioButton2.Checked;
                checkBox3.Checked = false;
                if (radioButton2 != null)
                {
                    onselectLable(radioButton2.Checked.ToString());
                }
            }
            catch {
                label1.Text = str;
            }
          
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = radioButton3.Checked;
                if (radioButton3 != null)
                {
                    onselectLable(radioButton3.Checked.ToString());
                }
            }
            catch {
                label1.Text = str;
            }
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                radioButton1.Checked = false;
                radioButton2.Checked = false;
                radioButton3.Checked = false;
               
            }
            catch
            {
                str = "資料已經清空!";
                label1.Text = str;
            }
        }
    }

 

介面如下:

 

 

當你點擊取消選擇的時候,你會發現,不用重複的去寫checkBox的狀態,而是直接寫radioButton即可達到清空的目的!這裡輸入框沒清空,抱歉~呵呵~不過很好清空的了!

委託的函數的功能就是在選擇下面的radioButton的時候,上面的checkBox會連動,或許你會說,我直接在radioButton寫誰是 checkBox1.Checked = true;
                checkBox2.Checked = false;
                checkBox3.Checked = false;

何必要checkBox1.Checked = false;
                checkBox2.Checked = false;
                checkBox3.Checked = radioButton3.Checked;
                if (radioButton3 != null)
                {
                    onselectLable(radioButton3.Checked.ToString());
                }

而且還要處理異常!多麻煩啊。等等,朋友,當你多此調用的時候你會回來寫我的,這也就是自己寫時間,調用委託函數的好處。多的就不解釋了!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.