views:

156

answers:

1

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?

+2  A: 

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

`

Radoslav Hristov
In this way the timesToCall reaches maximum to 2 but if you only display value of i int Title bar it is much bigger almost 6
Rahat Ali
Well, I just tried with modified code in which the timesToCall is displayed in the title bar and I got 6. It really depends on the speed of the wheel rotation.
Radoslav Hristov
And, also, if you change the line that adds entries to the list box to show the value of Delta, you will see a lot of entries like 360, 480, etc. That's, of course, on my computer, and the mouse is different.
Radoslav Hristov
the point which i don't understand is with the same sroll speed with which i get 6 in Title why i won't get my method called 6 times?
Rahat Ali
But you will only if you do the change I suggested. The event is invoked with values for Delta "a signed count of the number of detents the mouse wheel has rotated, multiplied by the WHEEL_DELTA constant. A detent is one notch of the mouse wheel.". WHEEL_DELTA id defined as 120. Then you calculate the number of detents (in absolute value because it might be positive or negative). And finally you call in a loop your function detents number of times. Isn't that what you need?
Radoslav Hristov
Thankx a lot for suggestions to Mr Radoslav Hristov
Rahat Ali
Actually the problem was with the command i want to execute infact zoom a ip camera, so htankx a lot i reexamined my sample project and find out that the problem is not with launching the event but with the capability of the camera to do zoom simultaneously.
Rahat Ali