C # checkbox eventhandler

    private void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        SetValuesInDB( System.Security.Principal.WindowsIdentity.GetCurrent().Name,
                       DateTime.Today.Date);

    }

      

Now I want to set the values ​​of my id and current date in the database only if I directly clicked on this checkbox.

I don't want to update these values ​​in the database if some other event fires this event handler. For example, on load every time a check box is checked but the database value for that check box is not checked. so every time this event handler runs and the database value is updated. How do I take care of this?

0


source to share


2 answers


Use the CheckBox.Click event instead. It is triggered if the user clicks this checkbox or uses Space to toggle the checkbox.



+5


source


I'm not sure what you want to do.

If you want to keep the value in the database and UI always up to date, you can try DataBindings ...





internal class MyDataSource {

    public bool MyBooleanValue {
        get { return ReadValueFromDB("MyUser", "MyBool"); }
        set { SaveValueToDB("MyUser", value); }
    }

}

...

internal class MyControl {


    internal MyControl() {
        dataSource = new MyDataSource();

        InitializeComponents();

        myCheckbox.DataBindings.Add(
            "Checked", dataSource, "MyBooleanValue"
        );
    }

    private MyDataSource dataSource;

}

      

code>

otherwise, you can only write the value to the database when the user completes the operation. For example, a form close event handler or an okButton event handler.

0


source







All Articles