admin管理员组文章数量:1336633
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;
}
本文标签: cCheckbox CheckedChanged Firing TwiceStack Overflow
版权声明:本文标题:c# - Checkbox CheckedChanged Firing Twice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742399022a2467508.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论