Using a Picture Box as a button, how do I ignore/disable double-click events?

I'm having some difficulty using picture boxes as buttons. I'd like to have custom button graphics rather than using the plain-old windows forms Button control.

It works pretty well, except the buttons seem to lag when a single button is pressed rapidly. I'd like the OnClick method to be called immediately every time the Picture Box is clicked, but it seems to be registering a double-click event, even though I am not using that event.

The short version is I'd like to:

  • (Ideal) Disable the double-click event, since it appears this is what the Control is waiting for on every alternating click of the Picture Box and I never use it. Or,
  • (Acceptable) Write a generic DoubleClick event I can use for all Picture Boxes I plan to use as buttons.

I did write up a test program which shows that this is, in fact, what's happening. I wrote a MouseUp event that increments a counter by 1, and also added a OnClick even that increments a different counter by 1. When rapidly clicking the Picture Box linked to these events, the MouseUp counter is exactly double that of the OnClick counter.

I then added a DoubleClick event that calls the OnClick event. This, although not ideal, solves the problem perfectly well. If there is a way to write a generic DoubleClick event that all buttons could use, I think this would be acceptable.

Right now my DoubleClick event is just

    private void pb_DoubleClick(object sender, EventArgs e)
    {
        pbOK_Click(sender, e);  //This needs to be generic
    }

I'd like to at least replace the DoubleClick line with something like

    private void pb_DoubleClick(object sender, EventArgs e)
    {
        (Sender.ToString)_Click(sender, e); // Doesn't work quite like this.
    }

I could write a special DoubleClick event for each Picture Box individually, but that is far from ideal and seems quite cumbersome, so I would prefer not to do that if it isn't necessary.

This is my first posted question, so thanks to anyone who takes the time to read this. Any advice would be greatly appreciated.

EDIT: This program is being developed for Windows CE, so the properties and events options for the objects is quite limited. I still may be missing something, but as far as I can tell the Properties list for buttons goes straight from GenerateMember to Location, with none of the Image options available. I apologize for not stating the target platform in the original post.

Answers


It is done with the Control.SetStyle() method, like this:

using System;
using System.Windows.Forms;

class MyButton : PictureBox {
    public MyButton() {
        this.SetStyle(ControlStyles.StandardDoubleClick, false);
    }
}

Okay, my original answer was trash. My update:

Here's a link on how to get a controls Method name: Link

In the link it has a Control c. To get your control (sender) use the following:

PictureBox picbox = (PictureBox) sender;

Next, call a method using a string(your class.Method.Name): Link

Now that should work. Hope it does.


Why don't you create your own control? This way, you don't have to do anything to get the behavior you want.

using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public class ButtonPictureBox : PictureBox
    {
        protected override void OnDoubleClick(System.EventArgs e)
        {
            OnClick(e);
        }
    }
}

I don't know if this is possible on Windows CE though.

If you also want to hide the DoubleClick event in the form designer, you can add this to the class:

        [Browsable(false)]
        public new event EventHandler DoubleClick;

Need Your Help

Spring Data Couchbase - Search without having admin rights on the cluster

spring couchbase spring-data-couchbase

I'm currently working on a POC with Couchbase, using Spring Data to put & get documents on/off a bucket on a cluster.