开发者

how to make mouse events work as a loop

I have to implement three types of mouse events in a sequence.Each mouse event is allowed only certain number of times.So, I initialized a variabl开发者_Python百科e and put it like this

int a = 0;
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
     if (a < 2)
     {
          // a little code here  
          //this makes mouse up to work for 2 times.
     }
}
private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if (a > 1 && a <= 3)
    {


                c[f] = e.X;
                d[f] = e.Y;
                a++;
                f++;


    } 
}    
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    if (a > 3 && a < 11)
    {
        //some more code
        //this makes MouseClick to work for 7 times
    }
}

now i want to run the last two events again, when ever i press a button.Please give me some ideas how can make them run again.

I tried to implement this one but didnot work.

private void button2_Click(object sender, EventArgs e)
{
    a = 2;// I thought it would set a=2 and the MouseDoubleClick would be implemented but my assumption proved to be wrong.
}


You can just explicitly call the Event Methods passing in null values:

private void button2_Click(object sender, EventArgs e)
{
    a = 2;
    pictureBox1_MouseClick(null,null);
    pictureBox1_MouseDoubleClick(null,null);

}

private void pictureBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    if(a > 1 && a <=3)
    {
        if(e!=null)
        {
            c[f] = e.X;
            d[f] = e.Y;
            a++;
            f++;
        }
    }
}

This will stop the error from occurring but when the event method is fired from the button2_Click method it will not actually do anything as the values for e.X and e.Y will be null.

You will need to come up with a different way of solving this problem as I don't fully understand what you are trying to achieve.


You can specialize whatever object you're listening to events from. In your case the PictureBox object I guess. As a convenience it's a convention to create protected virtual methods to broadcast events. This allows you to override and add additional behavior when events occur.

For example:

public class FooPicbox : PictureBox {

   protected override void OnMouseDown( MouseEventArgs e ) {
      base.OnMouseDown( e );
      /* Insert code here */
      /* Example: */
      base.OnMouseUp( e );
      base.OnMouseClick( e );
   }
}

Then, when the MouseDown event occurs, it also broadcasts the two other events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜