简体   繁体   English

C#复选框事件处理程序

[英]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 setvalues of my id and current date in database only if I directly clicked on the checkbox. 现在,仅当我直接单击复选框时,才想在数据库中设置ID和当前日期的值。

I dont want to update those values in Database if some other event triggers this event handler. 如果某些其他事件触发了此事件处理程序,我不想更新数据库中的那些值。 For eg: while loading everytime the checkbox gets checked but the database value for this checkbox is unchecked. 例如:每次加载时都选中该复选框,但未选中此复选框的数据库值。 so, everytime this event handler is triggered and database value is updated. 因此,每次触发此事件处理程序并更新数据库值时。 How do I take care of it? 我该如何照顾它?

Use the CheckBox.Click event instead. 请改用CheckBox.Click事件。 It gets fired if either the user clicks on the checkbox or uses Space to toggle the checkbox. 如果用户单击复选框或使用空格切换复选框,则将触发该事件。

I'm not really sure what you want to do. 我不太确定你想做什么。

If you want the value in the database and userinterface to be always up to date you might try DataBindings... 如果您希望数据库和用户界面中的值始终保持最新,则可以尝试使用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; } 

otherwise you might wish to write the value only in the database when the user finishes an operation. 否则,您可能希望仅在用户完成操作时才将值写入数据库。 Such as closing event handler of a form or okButton click event handler. 例如关闭表单的事件处理程序或okButton单击事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM