使用带有滚动条的任何控件时,MouseWheel 事件不会触发(在 C# Windows 窗体中)

发布于 2024-08-18 18:15:00 字数 1234 浏览 6 评论 0原文

MouseWheel 事件未触发 当我使用带有滚动条的任何控件(ListBox、Panel、TextBox)时。

重现问题:

public class Form1 : Form
 {
  private readonly Button button1;
  private readonly TextBox textBox1;

  private void button1_MouseWheel(object sender, MouseEventArgs e)
  {
   ToString(); // doesn't fire when uncomment lines below
  }

  public Form1()
  {
   button1 = new Button();
   textBox1 = new TextBox();
   SuspendLayout();

   button1.Location = new System.Drawing.Point(80, 105);
   button1.Size = new System.Drawing.Size(75, 23);
   button1.MouseWheel += button1_MouseWheel;
   button1.Click += button1_Click;

   textBox1.Location = new System.Drawing.Point(338, 105);
   //textBox1.Multiline = true; // uncomment this
   //textBox1.ScrollBars = ScrollBars.Vertical;  // uncomment this 
   textBox1.Size = new System.Drawing.Size(100, 92);

   ClientSize = new System.Drawing.Size(604, 257);
   Controls.Add(textBox1);
   Controls.Add(button1);
   ResumeLayout(false);
   PerformLayout();
  }

  // Clicking the button sets Focus, but even I do it explicit Focus() or Select()
  // still doesn't work
  private void button1_Click(object sender, System.EventArgs e)
  {
   button1.Focus();
   button1.Select();
  }
 }

MouseWheel event doesn't fire
when I' am using any control (ListBox, Panel, TextBox) with scrollbars.

To reproduce problem:

public class Form1 : Form
 {
  private readonly Button button1;
  private readonly TextBox textBox1;

  private void button1_MouseWheel(object sender, MouseEventArgs e)
  {
   ToString(); // doesn't fire when uncomment lines below
  }

  public Form1()
  {
   button1 = new Button();
   textBox1 = new TextBox();
   SuspendLayout();

   button1.Location = new System.Drawing.Point(80, 105);
   button1.Size = new System.Drawing.Size(75, 23);
   button1.MouseWheel += button1_MouseWheel;
   button1.Click += button1_Click;

   textBox1.Location = new System.Drawing.Point(338, 105);
   //textBox1.Multiline = true; // uncomment this
   //textBox1.ScrollBars = ScrollBars.Vertical;  // uncomment this 
   textBox1.Size = new System.Drawing.Size(100, 92);

   ClientSize = new System.Drawing.Size(604, 257);
   Controls.Add(textBox1);
   Controls.Add(button1);
   ResumeLayout(false);
   PerformLayout();
  }

  // Clicking the button sets Focus, but even I do it explicit Focus() or Select()
  // still doesn't work
  private void button1_Click(object sender, System.EventArgs e)
  {
   button1.Focus();
   button1.Select();
  }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怀中猫帐中妖 2024-08-25 18:15:00

我遇到了同样的问题,对我有用的是在控件中添加事件 MouseEnter 的处理程序,无论是否有焦点都会触发该处理程序。

private void chart1_MouseEnter(object sender, EventArgs e)
{
    chart1.Focus();
}

之后我就可以毫无问题地获取 mouseWheel 事件。

I was having the same problem, and what worked for me was to add a handler for the event MouseEnter in the control, that one is triggered with or without focus.

private void chart1_MouseEnter(object sender, EventArgs e)
{
    chart1.Focus();
}

After that I could get the mouseWheel events with no problems.

农村范ル 2024-08-25 18:15:00

通常,您需要确保要处理 MouseWheel 事件的控件处于活动状态。

例如,尝试在 Form Load(或 Shown)事件中调用 button1.Select(),然后使用滚轮。

例如:

private void Form1_Load(object sender, EventArgs e)
{
    button1.MouseWheel += new MouseEventHandler(button1_MouseWheel);

    button1.Select();  
}

You normally need to make sure the control you want to handle the MouseWheel event is active.

For example try calling button1.Select() in the Form Load (or Shown) event and then using the scroll wheel.

eg:

private void Form1_Load(object sender, EventArgs e)
{
    button1.MouseWheel += new MouseEventHandler(button1_MouseWheel);

    button1.Select();  
}
旧城烟雨 2024-08-25 18:15:00

我找到了解决方案,gility是默认的“鼠标配置”。 联想USB光学滚轮鼠标默认配置为:

控制面板/鼠标/滚轮/滚轮->启用通用滚动

我更改为:

控制面板/鼠标/滚轮/轮->仅使用 Microsoft Office 97 滚动仿真

现在在 .net 代码中 MouseWheel 与聚焦 Control< /em>.


但问题是:

  • 如何在 .net 代码中修复它?
  • 如何在 .net 代码中检测到这种情况?

有什么想法吗?

I found solution, gility is default "Mouse Configuration". Lenovo USB Optical Wheel Mouse default configuration is:

Control Panel/Mouse/Wheel/Whell->Enable Universal Scrolling;

I changed to:

Control Panel/Mouse/Wheel/Whell->Use Microsoft Office 97 Scrolling Emulation Only

Now in .net code MouseWheel working with Focused Control.


But questions are:

  • how can I fix it in .net code?
  • how can I detect this situation in .net code?

Any ideas ?

如若梦似彩虹 2024-08-25 18:15:00

我尝试了您的示例,并且无论这些行是否被注释,MouseWheel 事件仅在按钮聚焦时才会触发。此行为是设计使然。 (MouseWheel 事件与键盘事件一样,转到聚焦的控件)

I tried your example, and, whether the lines were commented or not, the MouseWheel event only fires if the button is focused. This behavior is by design. (the MouseWheel event, like keyboard events, goes to the focused control)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文