首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MouseWheel事件触发

MouseWheel事件触发
EN

Stack Overflow用户
提问于 2010-05-13 16:39:39
回答 1查看 2K关注 0票数 0

我在MouseWheel事件上调用我的私有方法时遇到了问题。事实上,当我只增加一个变量或者在标题栏中显示一些东西时,我的鼠标滚轮事件被正确地触发。但是当我想调用一个私有方法时,该方法只被调用一次,这不是我想调用该方法的要求,这取决于scroll的速度,即当scroll完成一次时,缓慢地调用私有方法一次,但是当scroll在高速下完成时,根据scroll速度调用私有方法多次。

为了进一步解释,我将显示i的值的示例代码放在标题栏中,并根据滚动速度适当地将其添加到列表框控件中,但当我想根据滚动速度多次调用私有方法时,该方法只被调用一次。

代码语言:javascript
复制
public partial class Form1 : Form
{
    ListBox listBox1 = new ListBox();
    int i = 0;

    public Form1()
    {
        InitializeComponent();

        // Settnig ListBox control properties
        this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.listBox1.FormattingEnabled = true;
        this.listBox1.Location = new System.Drawing.Point(13, 13);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(259, 264);
        this.listBox1.TabIndex = 0;

        // Attaching Mouse Wheel Event
        this.listBox1.MouseWheel += new MouseEventHandler(Form1_MouseWheel);

        // Adding Control
        this.Controls.Add(this.listBox1);
    }

    void Form1_MouseWheel(object sender, MouseEventArgs e)
    {
        i++;
        this.Text = i.ToString();
        this.listBox1.Items.Add(i.ToString());            

        // Uncomment the following line to call the private method
        // this method gets called only one time irrelevant of the
        // mouse wheel scroll speed.
        // this.LaunchThisEvent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.listBox1.Select();
    }

    private void LaunchThisEvent()
    {
        // Display message each time 
        // this method gets called.
        MessageBox.Show(i.ToString());
    }
}

如何根据鼠标滚轮滚动的速度多次调用私有方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-05-13 18:30:50

您可以尝试使用MouseEventArgs.Delta字段来计算调用次数:

代码语言:javascript
复制
        int timesToCall = Math.Abs(e.Delta/120);

        for (int k = 0; k < timesToCall; ++k)
        {
            this.LaunchThisEvent();
        }

`

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2825380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档