鼠标滚轮事件触发

发布于 2024-09-01 12:56:27 字数 1891 浏览 6 评论 0 原文

我在调用 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());
    }
}

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

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.

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?

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

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

发布评论

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

评论(1

江心雾 2024-09-08 12:56:28

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

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

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

`

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

`

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