Skip to content

Instantly share code, notes, and snippets.

@abc126
Created December 13, 2018 04:21
Show Gist options
  • Select an option

  • Save abc126/e657c72dda16d219715de4f06878dd95 to your computer and use it in GitHub Desktop.

Select an option

Save abc126/e657c72dda16d219715de4f06878dd95 to your computer and use it in GitHub Desktop.
RadioGroupBox
public class RadioGroupBox : GroupBox
{
public event EventHandler SelectedChanged = delegate { };
int _selected;
public int Selected
{
get
{
return _selected;
}
set
{
int val = 0;
var radioButton = this.Controls.OfType<RadioButton>()
.FirstOrDefault(radio =>
radio.Tag != null
&& int.TryParse(radio.Tag.ToString(), out val) && val == value);
if (radioButton != null)
{
radioButton.Checked = true;
_selected = val;
}
}
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
var radioButton = e.Control as RadioButton;
if (radioButton != null)
radioButton.CheckedChanged += radioButton_CheckedChanged;
}
void radioButton_CheckedChanged(object sender, EventArgs e)
{
var radio = (RadioButton)sender;
int val = 0;
if (radio.Checked && radio.Tag != null
&& int.TryParse(radio.Tag.ToString(), out val))
{
_selected = val;
SelectedChanged(this, new EventArgs());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment