gpt4 book ai didi

c# - 将任何控件与滚动条一起使用时,不会触发 MouseWheel 事件(在 C# Windows 窗体中)

转载 作者:可可西里 更新时间:2023-11-01 08:07:45 24 4
gpt4 key购买 nike

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();
}
}

最佳答案

我遇到了同样的问题,对我有用的是在控件中为事件 MouseEnter 添加一个处理程序,该处理程序在有焦点或没有焦点的情况下触发。

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

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

关于c# - 将任何控件与滚动条一起使用时,不会触发 MouseWheel 事件(在 C# Windows 窗体中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2094378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com