95. 체크박스(CheckBox) / 라디오버튼(RadioButton)
==> MainForm.cs
#region 컨텍스트메뉴
private void cmsAbout_Click(object sender, EventArgs e)
{
//FrmAbout fa = new FrmAbout();
//fa.ShowDialog();
miAbout_Click(null, null); // 재 사용
}
private void cmsExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
#endregion
private void miLable_Click(object sender, EventArgs e)
{
MyWinForms.Controls.FrmButtonLableTextBox blt = new MyWinForms.Controls.FrmButtonLableTextBox();
blt.MdiParent = this;
blt.Show();
}
private void miCheckBoxRadioButton_Click(object sender, EventArgs e)
{
MyWinForms.Controls.FrmCheckBoxRadioButton cbrb = new MyWinForms.Controls.FrmCheckBoxRadioButton();
cbrb.MdiParent = this;
cbrb.Show();
}
==============================================================================================
==> FrmCheckBoxRadioButton.cs
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 MyWinForms.Controls
{
public partial class FrmCheckBoxRadioButton : Form
{
public FrmCheckBoxRadioButton()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
string msg = "";
// 관심사항
if (this.chkSeeSharp.Checked) //[1] 체크되었다면
{
msg += this.chkSeeSharp.Text + "\r\n";
}
if (this.chkAspNet.Checked == true) //[2] 체크 true면
{
msg += this.chkAspNet.Text + "\r\n";
}
if (!chkSilverlight.Checked) //[3] 체크가 되지 않았다면,
{
// Empty
}
else
{
msg += this.chkSilverlight.Text + "\r\n";
}
// 성별
if (this.rdoMan.Checked)
{
msg += String.Format("{0}{1}{0}", "\r\n", rdoMan.Text);
}
else
{
msg += String.Format("{0}{1}{0}", "\r\n", optWomen.Text);
}
this.txtResult.Text = msg;
}
private void FrmCheckBoxRadioButton_Load(object sender, EventArgs e)
{
// 폼 로드시 동적으로 라디오버튼 체크
this.rdoMan.Checked = true;
}
}
}
< 실행결과 >
==> 체크박스에 체크하고 라디오버튼에서 선택한 후 "확인"버튼을 누르면 아래 텍스트박스에서 체크한 결과가 출력됨.(여기서는 초기에 FrmCheckBoxRadioButton.cs[디자인]에서 "ASP.NET"체크박스의 속성을 Checked --> True로 해주었고, FrmCheckBoxRadioButton.cs코드에서 this.rdoMan.Checked = true;로 하여 폼 로드시 동적으로 "남자"라디오버튼이 체크되게 하였다.)
체크박스는 다중선택이 가능하지만 라디오버튼은 택1만 가능하다.