Hello World
Hello World

Reputation: 1437

Handling unchecked checkboxes

I am trying to create a method that is used to check if my checkbox is unticked and in the event it is it will display a messagebox and then run my loop that should set all of my departments to not display, however I can't seem to get my code to detect when the checkbox is unticked, it simply hits the first if statement and gets no further even if the tickbox is unchecked which sets it to false.

    private void displayOnWebsite_Checked(object sender, RoutedEventArgs e)
    {
        CheckBox displayOnWebsite = sender as CheckBox;

        if (displayOnWebsite.IsChecked == false)
        {
            var departments = model.Name;
            var departmentChildren = model.Children;
            var messagebox = System.Windows.MessageBox.Show("Do you wish to hide all sub deparments and products.",
                 "List of Box",
                 MessageBoxButton.YesNo);

            if (!session.IsConnected)
                session.Reconnect();

            // Used to hide child departments and products.
            if (messagebox == MessageBoxResult.Yes)
            {
                if (departmentChildren != null)
                {
                    int zeroChildren = 0;

                    if (departmentChildren.Count.Equals(zeroChildren))
                    {
                        foreach (Department children in departmentChildren)
                            children.IsVisibleOnWebsite = false;
                    }
                }
            }
            else
                System.Windows.MessageBox.Show("Nevermind then...");
        }
        else
            System.Windows.MessageBox.Show("nothing happened here");
    }

WPF:

                        <CheckBox IsChecked="{Binding IsVisibleOnWebsite}" Name="displayOnWebsite" Style="{StaticResource Label}" Checked="displayOnWebsite_Checked">Display On Website</CheckBox>

Upvotes: 0

Views: 100

Answers (1)

mbm
mbm

Reputation: 955

Use "Unchecked" event to handle unchecking.

Upvotes: 2

Related Questions