C# 检测鼠标左键单击 Winform

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14645233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:34:13  来源:igfitidea点击:

Detecting a left button mouse click Winform

c#winforms

提问by N0xus

What I'm trying to do is get my winform to display a debug line when ever I click in my winform. However, when I do, nothing happens. I know how to get a button / other click event to happen. But what I need is to be able to click anywhere within my winform.

我想要做的是让我的 winform 在我点击我的 winform 时显示调试行。但是,当我这样做时,什么也没有发生。我知道如何让按钮/其他点击事件发生。但我需要的是能够在我的 winform 中的任何地方单击。

I've googled this for the past hour but can't see what I'm doing wrong. As far as I'm aware, this code should be correct in detecting a mouse click. This method is held withing the form1.cs class:

我在过去的一个小时里用谷歌搜索了这个,但看不出我做错了什么。据我所知,这段代码在检测鼠标点击时应该是正确的。此方法与 form1.cs 类一起保存:

  private void mouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {    
            Trace.WriteLine("Mouse clicked");
        }
    }

I've tried setting brake points, but these don't get triggered either. What is it I'm doing wrong?

我试过设置刹车点,但这些也没有被触发。我做错了什么?

Sorry for the stupidly newbie question, but I am very new to winform programming.

很抱歉这个愚蠢的新手问题,但我对 winform 编程很陌生。

采纳答案by codeteq

How to add the EventHandler:

如何添加事件处理程序:

public Form1()
{
    InitializeComponent();
    // This line should you place in the InitializeComponent() method.
    this.MouseClick += mouseClick;
}

回答by ColinE

I would recommend reading Events C# Programming Guide. You need to add an event handler like so:

我建议阅读 Events C# Programming Guide。您需要像这样添加一个事件处理程序:

form1.MouseClick += mouseClick;

回答by ColinE

The method itself is correct. I think your actual problem is: you haven't added this method to MouseClickevents.

方法本身是正确的。我认为您的实际问题是:您尚未将此方法添加到MouseClick事件中。

In C# – and most other languages too – event is handled by an event handler. Windows forms and controls have events for all the events happening in your controls, such as OnClickor OnResize. You can append methods to these events, and the methods will automatically get called when the actual event happens. Simply add the following line to your form's constructor, Form_Load-method, InitializeComponent-method, or such:

在 C# 以及大多数其他语言中,事件由事件处理程序处理。Windows 窗体和控件具有控件中发生的所有事件的事件,例如OnClickOnResize。您可以将方法附加到这些事件中,当实际事件发生时,这些方法将自动被调用。只需Form_Load将以下行添加到表单的构造函数、-method、InitializeComponent-method 等中:

this.MouseClick += mouseClick;

Now when ever MouseClickevent happens, your method mouseClickwill be called automatically.

现在,当MouseClick事件发生时,您的方法mouseClick将被自动调用。

回答by Jason

Using the editor built-in to Visual Studio:

使用 Visual Studio 内置的编辑器:

enter image description here

在此处输入图片说明

  1. Go to the properties window (if you don't see it, press Alt + Enter).
  2. Select the events icon (looks like lightning).
  3. Double-click on the empty ComboBox to the right of Click.
  4. You'll be taken to an empty method where you can put your code.
  1. 转到属性窗口(如果没有看到,请按Alt + Enter)。
  2. 选择事件图标(看起来像闪电)。
  3. 双击Click右侧的空 ComboBox 。
  4. 您将被带到一个空方法,您可以在其中放置代码。