Ebikeneser
Ebikeneser

Reputation: 2364

Using CheckBoxes in Silverlight

I am using a checkbox in a child window in Silverlight.

When a user clicks the checkbox this sends sends a toggle function to a database, where if the checkobox is checked, the value = 1, and unchecked value = -1, so the toggle is changing these values.

By using the following code-

private int checkCounter1 = 0;

private void checkBox1_Checked(object sender, RoutedEventArgs e)
{
    if (checkCounter1 == 0)
    {
    }
    else
    {
        //Web Service toggle function 
    }

    checkCounter1 = 1;
}

private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
{
    if (checkCounter1 == 0)
    {
    }
    else
    {
        //Web Service toggle function
    }

    checkCounter1 = 1;
}

When the user clicks the checkbox this is invoking the webservice call as expected and the value is getting toggled.

My problem being is that when the child window is loaded up, this for some reason is invoking the web service call on the checkBox1_Checked. I do not know why. So by adding the checkCounter I was hoping this would skip past the toggle function on page load.

On page load I am using another web service call to call the values of the checkbox at present, for intsance if in the database the value is = to 1, then the checkbox should be checked on the form -

void Settings_Loaded(object sender, RoutedEventArgs e)
{
    cosainWebService.Service1SoapClient();
    client.callFlagsCompleted += new EventHandler<callFlagsCompletedEventArgs>(client_callFlagsCompleted);
    client.callFlagsAsync();
}

So I then take the result of callFlags and use it here-

void client_callFlagsCompleted(object sender, callFlagsCompletedEventArgs e)
{            
    string flag = System.Convert.ToString(e.Result);

    foreach (string x in e.Result)
    {
        string[] array = x.Split(',');

        if (array[1] == "Checkbox Name" ) 
        {
            if (array[0] == "1") //this is the value in the database
            {
                checkBox1.IsChecked = true;
            }
            else
            {

            }
        }
    }
}

The checkbox XAML looks like-

<CheckBox Content="CheckBox Name" Height="16" HorizontalAlignment="Left" Margin="12,42,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked"/>

So when debugging, clicking the checkbox is toggling the value in the database, so when a user has checked the box, the value should be stored as '1', I should then be able to close the window, load it up again, and the box should be ticked, but for some reason this is not working, and when the window opens, it is toggling the value. Where am I going wrong?

Upvotes: 0

Views: 1423

Answers (1)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

I think this is the sequence of events causing your webservice to be fired at page load.

  1. In design mode this checkbox1 is checked.
  2. In run mode when this window gets loaded, it calls InitializeComponents() (method which initializes controls on window. Don't know real name of this method in SL but in Winforms and WPF its InitializeComponents).

  3. There it checks the checkbox because that's the way its in design mode. This causes the event to be fired which call your service.

First don't save value in database as 1 or -1.

Save as 0 or 1. These are int values and directly convertible to bool's true and false which can further be assigned to your checkbox.

In UI don't check your checkbox. In Window Load when data is retrieved from database, do this

void Windows_Loaded(....)
{
    bool i = value_from_database(); //------- Line No. 1
    checkBox1.Checked -= new RoutedEventHandler(checkBox1_Checked); //Line No. 2
    checkbox1.Ischecked = i; //------- Line No. 3
    checkBox1.Checked += new RoutedEventHandler(checkBox1_Checked); //Line No. 4
}

When you assign database value to checkbox, its checked event will not fire (since it has been removed by Line No. 2).

Now when it assigns that value to it. (Line 3)

We will again add that event to our checkbox. (Line 4)

Upvotes: 1

Related Questions