Mirco Franchetti
Mirco Franchetti

Reputation: 21

Programmatically created CheckBox does not fire Checked/Unchecked events

I have a simple WPF application that uses a "frame" for multi-page navigation. One of that pages creates a series of CheckBoxes and adds a couple of handlers (Checked/Unchecked) for each checkbox created. The CheckBoxes work as intended and are programmatically accessible, they can be checked or unchecked by click but none of the two events is ever fired if I click.

Here is the creation of the CheckBoxes:

ModuleStackpanels[i].Children.Add(ModuleCheckBoxes[i]);
StackPanel.SetZIndex(ModuleCheckBoxes[i], 2);
ModuleCheckBoxes[i].Checked += new RoutedEventHandler(ModuleCheckBoxClick);
ModuleCheckBoxes[i].Unchecked += new RoutedEventHandler(ModuleCheckBoxClick);

Where I go from 0 to 30. Then I have the handler:

private void ModuleCheckBoxClick(object sender, RoutedEventArgs e)
{
    int CheckBoxCounter = 0;

    for(int i=0;i<30;i++)
    {
        if (ModuleCheckBoxes[i].IsChecked == true) CheckBoxCounter++;
    }

    if(CheckBoxCounter > 1)
    {
        Button_QueryStatus.IsEnabled = false;
    }
}

But nothing is fired. Someone has got an idea?

Upvotes: 1

Views: 2014

Answers (3)

Narish
Narish

Reputation: 760

I just dealt with a similar issue on a WPF app that I did not write the front end for and here it came down to: Checked and similar event handlers will fire if the checkbox is triggered manually or programmatically. Other types are not guaranteed in the same way if you are changing the checkbox by setting to IsChecked.

ex. in my case, they attempted to use a Clicked event handler which would only fire from actual user interaction and not programmatic change, which makes perfect sense because setting the IsChecked status is truly not a click event (even though both can potentially check or uncheck the checkbox)

Upvotes: 0

Mirco Franchetti
Mirco Franchetti

Reputation: 21

Good news! Not all the checkboxes had the event handler because of a fault in the creation of the buttons. 3 on 30 had it, and these buttons represented a different kind of item.

Upvotes: 1

A. Wolf
A. Wolf

Reputation: 1347

Try to use CheckedChanged instead of Checked and Unchecked.

For example:

public bool checkedthecheckbox { get; set; }

CheckBox testchbox = new CheckBox();

private void Form1_Load(object sender, EventArgs e)
{
    testchbox.CheckedChanged += new EventHandler(testchbox_CheckedChanged);
}

void testchbox_CheckedChanged(object sender, EventArgs e)
{
    if (testchbox.Checked)
        checkedthecheckbox = true;
    else
        checkedthecheckbox = false;
}

In your case:

ModuleStackpanels[i].Children.Add(ModuleCheckBoxes[i]);
StackPanel.SetZIndex(ModuleCheckBoxes[i], 2);
ModuleCheckBoxes[i].CheckedChanged += new EventHandler(ModuleCheckBoxClick);

private void ModuleCheckBoxClick(object sender, RoutedEventArgs e)
{
    int CheckBoxCounter = 0;

    for(int i=0;i<30;i++)
    {
        if (ModuleCheckBoxes[i].IsChecked == true) CheckBoxCounter++;
    }

    if(CheckBoxCounter > 1)
    {
        Button_QueryStatus.IsEnabled = false;
    }
}

Upvotes: 2

Related Questions