MouseWheel事件火灾

时间:2010-05-13 08:39:39

标签: c# winforms events mousewheel

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

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

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

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

1 个答案:

答案 0 :(得分:2)

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

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

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

`

相关问题