c# - Checkbox CheckedChanged Firing Twice - Stack Overflow

I have a lot of checkboxes that are all connected to one event cause I want only one checkbox to be abl

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
Add a comment  | 

2 Answers 2

Reset to default 1

CheckedChanged 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

相关推荐

  • c# - Checkbox CheckedChanged Firing Twice - Stack Overflow

    I have a lot of checkboxes that are all connected to one event cause I want only one checkbox to be abl

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信