最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Checkbox CheckedChanged Firing Twice - Stack Overflow

programmeradmin0浏览0评论

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;
}
发布评论

评论列表(0)

  1. 暂无评论