Reputation: 2883
I want to enabled the button only if the picturebox on windows forms have a picture on it. Otherwise, the button will remains false.
I already tried this below code, but when the picturebox contains a picture, the button won't enabled at all (the button enabled is true by default)
if (pictureBox1 == null || pictureBox1.Image == null)
{
this.button2.Enabled = false;
}
else if (pictureBox1 != null || pictureBox1.Image != null)
{
this.button2.Enabled = true;
}
if (pictureBox1 == null || pictureBox1.BackgroundImage == null)
{
this.button2.Enabled = false;
}
else
{
this.button2.Enabled = true;
}
Your answer much appreciated!
Upvotes: 0
Views: 1392
Reputation: 54417
If that code is executed on the Load
event of the form and the PictureBox
is empty at that point then the Button
will be disabled. If you load an Image
later then you would have to execute the code again. There is no event raised when an Image
is loaded so you should simply write a method for the purpose:
private void SetPictureBoxImage(Image img)
{
this.pictureBox1.Image = img;
this.SetButtonEnabledState();
}
private void SetButtonEnabledState()
{
this.button2.Enabled = (this.pictureBox1.Image != null);
}
Whenever you want to set or clear the Image
, call that first method. You can call that second method on the Load
event of the form as well, in case you set the Image
in the designer, which would bypass the SetPictureBoxImage
method.
Upvotes: 2