Created
December 13, 2018 04:21
-
-
Save abc126/e657c72dda16d219715de4f06878dd95 to your computer and use it in GitHub Desktop.
RadioGroupBox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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