I have a lot of checkboxes that are all connected to one event cause I want only one checkbox to be able to be "checked" at a time like you cant check multiple so when I check one I uncheck all the others but for some reason it shows the Notification twice for the checkbox that was checked and for the new checkbox that i just checked
private void Region_CheckedChanged(object sender, BunifuCheckBox.CheckedChangedEventArgs e)
{
BunifuCheckBox checkbox = (BunifuCheckBox)sender;
if (checkbox.CheckState == CheckStates.Checked)
{
string regionName = regionCheckBoxes[checkbox];
Bufferly.Instance.BufferlyNotification.Show(
Bufferly.Instance,
$"You chose the Region {regionName}!",
position: BunifuSnackbar.Positions.TopRight,
type: BunifuSnackbar.MessageTypes.Success
);
foreach (var otherCheckbox in regionCheckBoxes.Keys)
{
if (otherCheckbox != checkbox)
{
otherCheckbox.CheckState = CheckStates.Unchecked;
}
}
}
}
I have a lot of checkboxes that are all connected to one event cause I want only one checkbox to be able to be "checked" at a time like you cant check multiple so when I check one I uncheck all the others but for some reason it shows the Notification twice for the checkbox that was checked and for the new checkbox that i just checked
private void Region_CheckedChanged(object sender, BunifuCheckBox.CheckedChangedEventArgs e)
{
BunifuCheckBox checkbox = (BunifuCheckBox)sender;
if (checkbox.CheckState == CheckStates.Checked)
{
string regionName = regionCheckBoxes[checkbox];
Bufferly.Instance.BufferlyNotification.Show(
Bufferly.Instance,
$"You chose the Region {regionName}!",
position: BunifuSnackbar.Positions.TopRight,
type: BunifuSnackbar.MessageTypes.Success
);
foreach (var otherCheckbox in regionCheckBoxes.Keys)
{
if (otherCheckbox != checkbox)
{
otherCheckbox.CheckState = CheckStates.Unchecked;
}
}
}
}
Share
Improve this question
asked Nov 19, 2024 at 21:00
IliasIlias
31 bronze badge
1
- Can you add how you are setting up the multiple check boxes please? Are they all added/setup via the designer or is it done via code? – JayV Commented Nov 19, 2024 at 21:22
2 Answers
Reset to default 1CheckedChanged is triggered when Checked state changes. Changing from Checked to Unchecked is a change, and changing from Unchecked to Checked is a change too. This is the reason why you see two notifications.
It looks like your code can already deal with that. But you could get read of "foreach" cycle if you use RadioButton control instead of Checkbox. If you select any RadioButton, all other RadioButtons in the same container will be uchecked automatically. If you have two separate groups of RadioButtons on the same form, you can put one of the groups (or both) inside GroupBox or Panel.
Use the CheckedChangedEventArg instead of the sender:
if (e.CheckState == BunifuCheckBox.CheckStates.UnChecked) return;
var checkbox = (BunifuCheckBox)sender;
string regionName = regionCheckBoxes[checkbox];
Bufferly.Instance.BufferlyNotification.Show(
Bufferly.Instance,
$"You chose the Region {regionName}!",
position: BunifuSnackbar.Positions.TopRight,
type: BunifuSnackbar.MessageTypes.Success);
foreach (var otherCheckbox in regionCheckBoxes.Keys)
{
if (otherCheckbox != checkbox)
otherCheckbox.CheckState = CheckStates.Unchecked;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742399022a4436532.html
评论列表(0条)