Disable click on picturebox

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
I have a picturebox that is clickable on my form. I would like to disable the click at a certain time but when I use

pictureBox1.Enabled=false;

to disable it the gif animation stops. How can I disable the click without stopping the animation?
 
How would I make this click event only do these commands once and another set every other time it is clicked? Use a while or for structure with a static variable?

private void pictureBox1_Click(object sender, System.EventArgs e)
{
string outputStr = "";
foreach(char myChar in x.Text)
{//even gets spaces:
if(!Char.IsSymbol(myChar)&&(!Char.IsLetter(myChar)))
{
outputStr += myChar.ToString();
}
}
 
Well, I would like to be able to set a limit on how many times the user can press that button at certain times.
 
Let's just make it simple and say that the picture_Click will be disabled after they use it once.
 
I'm not totally sure if this is a great way to do it, but it's one way. If
you want to stop the code in the click event from firing, you could
simply unhook the event handler for the click event.

The code:
C#:
pictureBox1.Click -= new System.EventHandler(pictureBox1_Click);
Will stop all click events from being processed and sent to the
'pictureBox1_Click' sub.
 
Definately there is. Create a boolean variable, like this:
C#:
bool doClick = true;
at the top of your form (above all
subs and functions), and then in the click event, do it like this:
C#:
private void pictureBox1_Click(object sender, System.EventArgs e)
{ 
  if (doClick) {
    // do whatever you need to do in the click event
    doClick = false;
  }
}
Set it back to true if you want to re-enable the button.
 
Back
Top