1-RadioButton控件的用法


 
RadioButton控件
單選按鈕,當與其他單選按鈕成對出現時,允許用戶從一組選項中選擇單個選項。也就是說,當同一個容器中(Form、Panel、GroupBox、PictureBox等)存在兩個以上的單選按鈕時,只能有一個被選中。但不在同一個容器中的幾組單選按鈕彼此不關聯,是可以有多個被選中的。
屬性
Checked屬性:最重要的屬性之一,該屬性是一個布爾類型的值,如果被選中,Checked的值為true,否則為false。常用於判斷選項是否被選中。
事件
這兩個控件的事件有很多,但主要用到的事件只有一個,那就是CheckedChanged事件。該事件在”Checked”屬性發生改變時發生。
用法(單項選擇)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace RadioButton
{
    public partial class RadioButton : Form
    {
        public RadioButton()
        {
            InitializeComponent();
        }
 
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Black;
            if (radioButton1.Checked)
                label2.Text = "你的答案是:" + radioButton1.Text;
        }
 
      
 
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Black;
            if (radioButton2.Checked)
                label2.Text = "你的答案是:" + radioButton2.Text;
        }
 
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Black;
            if (radioButton3.Checked)
                label2.Text = "你的答案是:" + radioButton3.Text;
        }
 
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Black;
            if (radioButton4.Checked)
                label2.Text = "你的答案是:" + radioButton4.Text;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            label2.ForeColor = Color.Red;
            if (radioButton2.Checked)
                label2.Text = "恭喜你,回答正確";
            else
                label2.Text = "對不起,回答錯誤";
        }
 
    }
}

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM