110,566
社区成员
发帖
与我相关
我的任务
分享
/************************************* Module Header **************************************\
* Module Name: MainForm.cs
* Project: CSWinFormGroupRadioButtons
* Copyright (c) Microsoft Corporation.
*
* This sample demonstrates how to group the RadioButtons in the different containers.
*
* For more information about the RadioButton control, see:
*
* Windows Forms RadioButton control
* http://msdn.microsoft.com/en-us/library/f5h102xz.aspx
*
* This source is subject to the Microsoft Public License.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
* All other rights reserved.
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\******************************************************************************************/
using System;
using System.Windows.Forms;
namespace CSWinFormGroupRadioButtons
{
public partial class MainForm : Form
{
// store the old RadioButton
//第一步创建一个RadioButton控件的对象
private RadioButton radTmp = null;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// we checked the rad1 in the MainForm.Designer.cs file
// then this RadioButton would be the old one
//第二步在初始加载的时候rad1为选中状态
radTmp = this.rad1;
}
// let the 4 Radiobuttons use this method to deal with their CheckedChanged event
// in the MainForm.Designer.cs file
//每当更改属性值的时候触发事件
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
// uncheck the old one
//第三步当点击其他RadioButton更改了属性值状态的时候,其他RadioButton控件为未选中,在根据参数选中状态,Lable控件显示的是当前控件选中的Name;
radTmp.Checked = false;
radTmp = (RadioButton)sender;
// find out the checked one
//这个是处理相同的业务
if (radTmp.Checked)
{
this.lb.Text = radTmp.Name + " 当前被选中的控件";
}
//这个是单独处理业务
if (rad1.Checked)
{
//rad1被选中处理业务
}
else if (rad2.Checked)
{
//rad2被选中处理业务
}
}
}
}
//每当更改属性值的时候触发事件
private void radioButton_CheckedChanged(object sender, EventArgs e)