GroupBox내에 있는 라디오 버튼의 체크된 값(Tag)을 가지고 온다.

매번 가져오기 귀찮아서 ㅡ_ㅡ;

image

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            this.groupBox1.SetRadioBoxValue("0001"); //값 세팅
        }

        private void button1_Click(object sender, EventArgs e)
        {
           var q= this.groupBox1.GetRadioBoxValue(); //체크된 박스의 tag값 읽어오기
           this.c1TextBox1.Text = q.ToString();
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public static class ControlExtention
    {
        /// <summary>
        /// Tag로 세팅된 값을 읽어온다.
        /// </summary>
        /// <param name="groupBox">The group box.</param>
        /// <returns></returns>
        public static object GetRadioBoxValue(this GroupBox groupBox)
        {
            try
            {
                foreach (Control item in groupBox.Controls)
                {
                    if (item.GetType() == typeof(RadioButton))
                    {
                        if (((RadioButton)item).Checked)
                        {
                            return item.Tag.ToString();
                        }
                    }
                }

                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Tag로 세팅된 저장한다.
        /// </summary>
        /// <param name="groupBox">The group box.</param>
        /// <returns></returns>
        public static void SetRadioBoxValue(this GroupBox groupBox , string tagValue)
        {

                foreach (Control item in groupBox.Controls)
                {
                    if (item.GetType() == typeof(RadioButton))
                    {
                        if (((RadioButton)item).Tag.ToString() == tagValue )
                        {
                            ((RadioButton)item).Checked = true;
                            return;
                        }
                    }
                }
            
        }//End For


    }
}

확장메서드

http://msdn.microsoft.com/ko-kr/library/bb383977.aspx

+ Recent posts