Click and doubleclick event in c # not working

I have the following code in my C # application

The first determines what happens when the user double-clicks the image.

 private void pictureDoubleClick(Object sender, EventArgs e)
        {
            PictureBox picture = (PictureBox)sender;
            Console.WriteLine(picture.ImageLocation);
            MessageBox.Show("Test");
        }

      

And one more in one click:

private void picutureClick(Object sender, EventArgs e)
        {
            PictureBox picture = (PictureBox)sender;

            if (picture.BorderStyle == BorderStyle.None)
            {
                picture.BorderStyle = BorderStyle.Fixed3D;
                picture.BackColor = Color.Red;
            }
            else
            {
                picture.BorderStyle = BorderStyle.None;
                picture.BackColor = Color.White;
            }
        }

      

I named both functions:

box.Click += new System.EventHandler(this.picutureClick);
box.DoubleClick += new System.EventHandler(this.pictureDoubleClick);

      

However, I ran into a strange error, the DoubleClick event will not be triggered, the only way I can get it to work is if I comment out one click. One click works regardless of whether I comment or not comment on the Doubleclick event. I looked around but I couldn't find a solution that resolved my problem.

+3


source to share


2 answers


It is strange behavior that changing BorderStyle

the picture window will stop the click for propagation (the event Click

will always be raised before the event DoubleClick

).

I really don't know how to handle it properly, but we could hack a little bit to work. We can introduce a "delay" between a Click

and DoubleClick

to check DoubleClick

before Click

.

Here we use Timer

to complete the task:

private Timer _timer;
private PictureBox _sender;
private int _clicks;

public Form1()
{
    InitializeComponent();

    pictureBox.Click += picutureClick;
    pictureBox.DoubleClick += (s, e) =>
                                    {
                                        // do your double click handling

                                        _clicks = 0;
                                    };

    // this Interval comes from trail and error, it a balance between lag and  
    // correctness. To play safe, you can use SystemInformation.DoubleClickTime,
    // but may introduce a bit long lagging after you click and before you
    // see the effects.
    _timer = new Timer {Interval = 75}; 
    _timer.Tick += (s, e) =>
                        {
                            if (_clicks < 2)
                            {
                                ClickHandler(_sender);
                            }

                            _clicks = 0;

                            _timer.Stop();
                        };
}

private void picutureClick(Object sender, EventArgs e)
{
    _clicks++;
    _sender = (PictureBox) sender;

    if (_clicks == 1)
    {
        _timer.Start();
    }
}

private void ClickHandler(PictureBox picture)
{
    if (picture.BorderStyle == BorderStyle.None)
    {
        // this line is not thread safe, but you could comment out the .InvokeIfRequire()
        // and uncomment this line to have a look at your form behavior
        //picture.BorderStyle = BorderStyle.Fixed3D;
        picture.InvokeIfRequired(c => (c as PictureBox).BorderStyle = BorderStyle.Fixed3D);
        picture.BackColor = Color.Red;
    }
    else
    {
        // same for this
        //picture.BorderStyle = BorderStyle.Fixed3D;
        picture.InvokeIfRequired(c => (c as PictureBox).BorderStyle = BorderStyle.None);
        picture.BackColor = Color.White;
    }
}

      

In the above code, I used an extension method to handle the cross-thread update:

public static void InvokeIfRequired(this Control c, Action<Control> action)
{
    if (c.InvokeRequired)
    {
        c.Invoke(new Action(() => action(c)));
    }
    else
    {
        action(c);
    }
}

      



Edit:

The extension method I used above is to simplify the code written to update cross-flow properties. More on this topic can be found here: Code Automation InvokeRequired

Here are some details about the extension method :

An extension method only works if it is declared in an inequivalent, non-nested static class. To make it work, you need to declare a new one public static class

to hold this method.

// your form class declared here
public partial class Form1 : Form
{
    // code omitted here
}

// declare the extension method in this extension class
public static class ControlExtensions
{
    public static void InvokeIfRequired(this Control c, Action<Control> action)
    {
        if (c.InvokeRequired)
        {
            c.Invoke(new Action(() => action(c)));
        }
        else
        {
            action(c);
        }
    }
}

      

+2


source


When you change the border of the image window, you can reset the click event. This is why it doesn't work and only toggles the one click event, if you comment out the border change, it starts working ... Sorry, but I don't know why this is happening and I don't know if my info helped me = /



+1


source







All Articles