简体   繁体   English

使用SQL Server的C#.net Winform应用程序中的事件处理

[英]event handling in c# .net winform application which uses sql server

i have a combobox with a list of SQLusers, which have permissions granted from beforehand. 我有一个带有SQLuser列表的组合框,这些列表具有事先授予的权限。 a table PERMISSION_DETAIL with columns username , permission_name consists the details of permissions granted or denied 具有usernamepermission_name列的表PERMISSION_DETAIL包含授予或拒绝的权限的详细信息

now when a user selects a SQLuser from combobox then the permissions are shown as a checkbox, like if permission is already granted, then the checkbox appears and its tick is checked, else UNCHECKED. 现在,当用户从组合框中选择一个SQLuser时,权限将显示为一个复选框,例如是否已授予权限,则将显示该复选框并选中其勾号,否则为UNCHECKED。

for this i use: 为此,我使用:

if(permission is previously granted)

checkbox1.checked = true;   // here the Checkbox_CheckChanged event is called, but i dont want to call it.
checkbox1.enabled = false;

now, besides the checkbox, there is a button, on clicking it, the checkbox gets enabled, that is, to modify the permission, the user will click the button. 现在,除了该复选框之外,还有一个按钮,单击它会启用该复选框,即,要修改权限,用户将单击该按钮。

now the user will tick or untick the checkbox to grant or deny permission and the checkbox change event 现在,用户将选中或取消选中复选框以授予或拒绝权限以及复选框更改事件

will be called, this would be fine. 将被调用,这会很好。

i want that as the checkbox appears, its tick is automatically checked, but this calls the checkbox_Checkchanged event, but i dont want to call that event. 我希望当复选框出现时,它的勾号会自动选中,但这调用了checkbox_Checkchanged事件,但我不想调用该事件。

i advice you to change Checkbox_CheckChanged event to Checkbox_Click event. 我建议您将Checkbox_CheckChanged事件更改为Checkbox_Click事件。

private void Checkbox_Click(object sender, EventArgs e)
        {
            if ((sender as CheckBox).Checked)
            {
                MessageBox.Show("checked");
                // add role to user
            }
            else
            {
                MessageBox.Show("un_checked");
                /remove role ffrom user
            }
        }

You could check if the checkbox is enabled, before doing anything in the EventHandler: 您可以在EventHandler中执行任何操作之前检查复选框是否已启用:

protected void Checkbox_CheckChanged (object sender, ..EventArgs e)
{
    //return if not enabled
    if(!((CheckBox)sender).IsEnabled) return;

    //DO THE REST
}

I think that if you make an event handler for CheckChanged, it should be called always when "check is changed", and you should handle logic thereafter. 我认为,如果为CheckChanged创建事件处理程序,则应始终在“检查已更改”时调用它,然后再处理逻辑。

The event is going to be called regardless, what matters is how you handle that event. 无论如何,都将调用该事件,重要的是如何处理该事件。 An event has occurred, you can't pretend it hasn't. 发生了一个事件,您不能假装没有发生。 The only thing you can do is decide whether that event means anything to you at a particular point in time. 您唯一可以做的就是确定该事件在特定时间点对您是否有任何意义。

For example: 例如:

public void chkPermission_CheckChanged(object sender, EventArgs e)
{
    bool isChecked = chkPermission.Checked;
    if (this.user.HasPermission() == isChecked) { return; }
    // otherwise, we need to change some permissions!
}

Analyse whether or not you need to do anything, and go from there. 分析您是否需要做任何事情,然后从那里去。 Don't just blindly assume something needs to change because an event fired. 不要仅仅因为发生事件而盲目地认为某些事情需要改变。

Add a boolean variable, IsCheckboxEventShouldFire, to your form. 将一个布尔变量IsCheckboxEventShouldFire添加到您的窗体。 Set to true initially. 最初设置为true。 In the code that sets the checkbox to checked automatically, also now set IsCheckboxEventShouldFire to false. 在将复选框设置为自动选中的代码中,现在还将IsCheckboxEventShouldFire设置为false。 In the checkchanged event, first check if the IsCheckboxEventShouldFire is true. 在checkchanged事件中,首先检查IsCheckboxEventShouldFire是否为true。 If it is, run your code. 如果是这样,请运行您的代码。 If not, then don't. 如果没有,那就不要。 Set IsCheckboxEventShouldFire to true at the end of the event's code. 在事件代码的末尾将IsCheckboxEventShouldFire设置为true。

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

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