C#:单选框和单选框互斥

添加单选按钮控件。  选择  工具箱 -》   公共控件   -》 RadioButton(单选按钮控件),单击选择到窗体中。在单选按钮的属性窗口中,更改单选按钮文本(Text 栏),文字大小(Font)等参数。

   注意:发送模式下的两个单选按钮为一组,接收模式下的单选按钮为另一组。为了发送和接收单选按钮相互独立,需将其划分组别,因此要引入容器控件。选择  工具箱   -》   容器    -》Panel(容器控件),将同组别单选按钮划分在一起。先放置容器控件,再往其中填入单选按钮控件,不然会被覆盖。

 

(1)别人文章的讲解

 

RadioButton控件

单选按钮,当与其他单选按钮成对出现时,允许用户从一组选项中选择单个选项。也就是说,当同一个容器中(Form、Panel、GroupBox、PictureBox等)存在两个以上的单选按钮时,只能有一个被选中。但不在同一个容器中的几组单选按钮彼此不关联,是可以有多个被选中的。

属性

Checked属性:最重要的属性之一,该属性是一个布尔类型的值,如果被选中,Checked的值为true,否则为false。常用于判断选项是否被选中。

事件

这两个控件的事件有很多,但主要用到的事件只有一个,那就是CheckedChanged事件。该事件在”Checked”属性发生改变时发生。

用法(单项选择)

     

    

后台代码:

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.ComponentModel;

  4. using System.Data;

  5. using System.Drawing;

  6. using System.Linq;

  7. using System.Text;

  8. using System.Windows.Forms;

  9.  
  10. namespace RadioButton

  11. {

  12.     public partial class RadioButton : Form

  13.     {

  14.         public RadioButton()

  15.         {

  16.             InitializeComponent();

  17.         }

  18.  
  19.         private void radioButton1_CheckedChanged(object sender, EventArgs e)

  20.         {

  21.             label2.ForeColor = Color.Black;

  22.             if (radioButton1.Checked)

  23.                 label2.Text = "你的答案是:" + radioButton1.Text;

  24.         }

  25.  
  26.        

  27.  
  28.         private void radioButton2_CheckedChanged(object sender, EventArgs e)

  29.         {

  30.             label2.ForeColor = Color.Black;

  31.             if (radioButton2.Checked)

  32.                 label2.Text = "你的答案是:" + radioButton2.Text;

  33.         }

  34.  
  35.         private void radioButton3_CheckedChanged(object sender, EventArgs e)

  36.         {

  37.             label2.ForeColor = Color.Black;

  38.             if (radioButton3.Checked)

  39.                 label2.Text = "你的答案是:" + radioButton3.Text;

  40.         }

  41.  
  42.         private void radioButton4_CheckedChanged(object sender, EventArgs e)

  43.         {

  44.             label2.ForeColor = Color.Black;

  45.             if (radioButton4.Checked)

  46.                 label2.Text = "你的答案是:" + radioButton4.Text;

  47.         }

  48.  
  49.         private void button1_Click(object sender, EventArgs e)

  50.         {

  51.             label2.ForeColor = Color.Red;

  52.             if (radioButton2.Checked)

  53.                 label2.Text = "恭喜你,回答正确";

  54.             else

  55.                 label2.Text = "对不起,回答错误";

  56.         }

  57.  
  58.     }

  59.  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛1988

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值