MouseWheel事件火 [英] MouseWheel Event Fire

查看:169
本文介绍了MouseWheel事件火的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem on calling my private method on MouseWheel event. In fact my mouse wheel event gets fired properly when i only increment a variable or display something in Title bar etc. But when i want to call a private method, that method gets called only one time which is not the requirement i want to call that method depending on the speed of scroll i.e. when scroll is done one time slowly call the private method one time but when the scroll is done in high speed call the private method more than one time depending on the scroll speed.

为进一步说明,我将放置示例代码,显示i的值在标题栏中,并根据滚动速度正确添加到列表框控件,但是当我想要调用私有方法更多一次取决于滚动速度,该方法只被调用一次。

For further explanation i am placing the sample code which displays the value of i in Title bar and add it in the Listbox control properly depending on the scroll speed but when i want to call the private method more than one time depending upon the scroll speed, that method gets called only one time.

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

如何调用私有方法一次以上按照鼠标滚轮的速度滚动?

How to call the private method more than one time depending upon the speed of the mouse wheel scroll?

推荐答案

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

You can try using the MouseEventArgs.Delta field to calculate the number of calls:

        int timesToCall = Math.Abs(e.Delta/120);

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

`

这篇关于MouseWheel事件火的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆