I am using a custom control that inherits from a picture box to simulate a button. I have most of the functionally I want working correctly. I want to put something in that will simulate a button press. So that when the user presses the button something will happen to the image on the control for a split second.
Is the code below an ok solution for this or can someone suggest a better method.
Would it be using a timer that replaces the image after time x or something like that?
Thanks
private void MediaButton_MouseClick(object sender, MouseEventArgs e)
{
this.Image = this.downImage;
this.Invalidate();
this.Refresh();
//Do something
System.Threading.Thread.Sleep(200);
this.Image = this.upImage;
this.Invalidate();
this.Refresh();
}
Update:
This is for a touch screen device
No, you don’t want to use
Thread.Sleep(xx)here… In fact, just about any time you see code using that, you can assume it’s wrong or at least a very bad idea. You also don’t want a timer, because you don’t want to wait a pre-determined amount of time.Instead, let the user determine how long the “down” or “active” image is displayed. Sync it up with the
MouseDownandMouseUpevents. Change the image to your “down” or “active” image when theMouseDownevent is raised, and reset it back to the original image in theMouseUpevent.Your “down” or “active” image will be displayed as long as the user is clicking the button (has their mouse button down over the control), and then removed when they release the mouse button.
You get the kind of visual feedback you’re looking for, without any of the bad design.