private
void
rchk3Yes_Click(
object
sender, EventArgs e)
{
if
(((Telerik.WinControls.UI.RadCheckBox)sender).Checked ==
true
)
{
//already checked, it will uncheck
rchk3Yes.Checked =
false
;
}
else
{
//checks itself and unchecks other checkboxes
if
(rchkUnknown.Checked ==
true
)
{
rchkUnknown.Checked =
false
;
}
if
(rchk3No.Checked ==
true
)
{
rchk3No.Checked =
false
;
}
}
}
Hi I am trying to make 3 checkboxes that only allow one or none to be checked, if one is checked the others are unchecked. Here is the code I have but it doesnt work right.
4 Answers, 1 is accepted
Please take a look at the following example:
using
System.Windows.Forms;
using
Telerik.WinControls.UI;
public
partial
class
Form1 : Form
{
private
FlowLayoutPanel flowPanel;
private
bool
localChange;
public
Form1()
{
InitializeComponent();
flowPanel =
new
FlowLayoutPanel();
flowPanel.Dock = DockStyle.Fill;
this
.Controls.Add(flowPanel);
for
(
int
i = 0; i < 10; i++)
{
var checkBox =
new
RadCheckBox();
checkBox.ToggleStateChanged +=
new
StateChangedEventHandler(radCheckBox_ToggleStateChanged);
checkBox.Text =
"CheckBox"
+ i;
flowPanel.Controls.Add(checkBox);
}
}
void
radCheckBox_ToggleStateChanged(
object
sender, StateChangedEventArgs args)
{
if
(localChange)
{
return
;
}
ToggleState(sender
as
RadCheckBox);
}
private
void
ToggleState(RadCheckBox radCheckBox)
{
localChange =
true
;
foreach
(var control
in
flowPanel.Controls)
{
var checkBox = control
as
RadCheckBox;
if
(checkBox !=
null
&& checkBox != radCheckBox && checkBox.ToggleState != Telerik.WinControls.Enumerations.ToggleState.Off)
{
checkBox.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;
}
}
localChange =
false
;
}
}
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
private void rchk3Yes_Click(object sender, EventArgs e)
{
if (((Telerik.WinControls.UI.RadCheckBox)sender).Checked == true)
{
//already checked, it will uncheck
rchk3Yes.Checked = false;
}
else
{
//checks itself and unchecks other checkboxes
if (rchkUnknown.Checked == true)
{
rchkUnknown.Checked = false;
}
if (rchk3No.Checked == true)
{
rchk3No.Checked = false;
}
}
}
www.welookups.com
Hello Uwe,
I am not sure what you mean about OptionSet Box. This is why I made some research and it seems that it is used to show a list of predefined options displayed in a drop-down that a user can choose from by checking/unchecking. In this case, I can suggest several controls from Telerik UI for Winforms suite that might be suitable for you:
- RadCheckedListBox control - RadCheckedListBox is an enhanced alternative to the standard Windows Forms checked list box control. It displayed a list box in which a checkbox is displayed to the left of each item.
- RadListView control - RadListView has built-in checkboxes functionality which can be shown by setting the ShowCheckBoxes property of RadListView to true. You can find more information here.
- RadCheckedDropDownList control - RadCheckedDropDownList combines RadDropDownList and RadAutoCompleteBox in order to provide the functionality to check items in the drop-down area and tokenize them in the text area.
Could you please take a look at the suggested controls and consider if any of them should be suitable for you?
Should you have any other questions do not hesitate to ask.
Regards,
the Telerik team