C#复选框和鼠标单击事件

时间:2022-02-13 00:01:48

I am self teaching myself C# and ran into a problem I haven't seem to find an answer too. I have a Form that when I mouse click the check box the state goes to true but also immediately triggers the mouse click event I have code follows:

我自己教自己C#并遇到了一个我似乎也没有找到答案的问题。我有一个表单,当我鼠标单击复选框时状态变为true但也立即触发鼠标单击事件我有代码如下:

private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
    //MouseEventArgs me = (MouseEventArgs) e;
    if (uxMouseCopyCheckBox.Checked)
    {
        MessageBox.Show("Test");
        uxMouseCopyCheckBox.Checked = false;

    }
}

I have searched the stack overflow and Google and found similar items but not in C# but no luck on the fixed solution. What I want to do it use the first click to change the check box to true without triggering the mouse click event. I want to delay the event to the 2nd mouse click and not the first.

我搜索了堆栈溢出和谷歌,发现了类似的项目,但没有在C#中,但在固定解决方案上没有运气。我想要做的是使用第一次单击将复选框更改为true而不触发鼠标单击事件。我想将事件延迟到第二次鼠标点击,而不是第一次。

I have tried the following:

我尝试过以下方法:

  • for loop
  • Clicks == 2 with if statement
  • 使用if语句单击== 2

  • subscribing but at a loss on what to use
  • 订阅但不知道该使用什么

3 个解决方案

#1


Just use a boolean variable as a flag.

只需使用布尔变量作为标志。

private bool wasAlreadyClickedOnce;

private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
    if (!wasAlreadyClickedOnce)
    {
        wasAlreadyClickedOnce = true;
        return;
    }

    if (uxMouseCopyCheckBox.Checked)
    {
        MessageBox.Show("Test");
        uxMouseCopyCheckBox.Checked = false;

    }
}

#2


Instead of the Click event you could subsribe to the CheckedChanged event :

您可以将其替换为CheckedChanged事件,而不是Click事件:

The Handler will look look exactly like yours :

处理程序看起来与您的完全一样:

private void uxMouseCopyCheckBox_CheckedChanged(object sender, EventArgs e)
{
    if (!uxMouseCopyCheckBox.Checked)
    {
        MessageBox.Show("Test");
        uxMouseCopyCheckBox.Checked = false;
    }
}

The only difference is that we want the Message box to be shwon only on the second click so when you will uncheck the checkbox.

唯一的区别是我们希望仅在第二次单击时才会显示消息框,因此当您取消选中该复选框时。

Be careful though, if you change the default state of the checkbox, it will no longer work.

但要小心,如果更改复选框的默认状态,它将不再起作用。

If you want a really robust solution Grant's one is IMHO the best, mine was just here to show you how to adapt your code for it to work

如果你想要一个非常强大的解决方案Grant的一个是恕我直言最好的,我的只是在这里向你展示如何调整你的代码让它工作

#3


Try using the Click event instead of CheckedChanged event to check or uncheck the CheckBox and then you can use the MouseClick event for other stuff.

尝试使用Click事件而不是CheckedChanged事件来检查或取消选中CheckBox,然后您可以将MouseClick事件用于其他内容。

#1


Just use a boolean variable as a flag.

只需使用布尔变量作为标志。

private bool wasAlreadyClickedOnce;

private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
    if (!wasAlreadyClickedOnce)
    {
        wasAlreadyClickedOnce = true;
        return;
    }

    if (uxMouseCopyCheckBox.Checked)
    {
        MessageBox.Show("Test");
        uxMouseCopyCheckBox.Checked = false;

    }
}

#2


Instead of the Click event you could subsribe to the CheckedChanged event :

您可以将其替换为CheckedChanged事件,而不是Click事件:

The Handler will look look exactly like yours :

处理程序看起来与您的完全一样:

private void uxMouseCopyCheckBox_CheckedChanged(object sender, EventArgs e)
{
    if (!uxMouseCopyCheckBox.Checked)
    {
        MessageBox.Show("Test");
        uxMouseCopyCheckBox.Checked = false;
    }
}

The only difference is that we want the Message box to be shwon only on the second click so when you will uncheck the checkbox.

唯一的区别是我们希望仅在第二次单击时才会显示消息框,因此当您取消选中该复选框时。

Be careful though, if you change the default state of the checkbox, it will no longer work.

但要小心,如果更改复选框的默认状态,它将不再起作用。

If you want a really robust solution Grant's one is IMHO the best, mine was just here to show you how to adapt your code for it to work

如果你想要一个非常强大的解决方案Grant的一个是恕我直言最好的,我的只是在这里向你展示如何调整你的代码让它工作

#3


Try using the Click event instead of CheckedChanged event to check or uncheck the CheckBox and then you can use the MouseClick event for other stuff.

尝试使用Click事件而不是CheckedChanged事件来检查或取消选中CheckBox,然后您可以将MouseClick事件用于其他内容。