radiobutton控件 (推荐学习:phpstorm)
单选按钮,当与其他单选按钮成对出现时,允许用户从一组选项中选择单个选项。也就是说,当同一个容器中(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 = "对不起,回答错误"; } }}
以上就是radiobutton怎么用的详细内容。