Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having Trouble to check picture box


C#
if (pictureBox1.Image != null)
{
    Common.MessageBoxUtilities.WarningMessageBox("You Have Image");
}
else if (pictureBox1.Image == null)
{
    Common.MessageBoxUtilities.WarningMessageBox("You Dont Have Image");
}


control goes to if else statement every time i run it, whether i have image selected or not. please help me to solve
Posted
Updated 3-Nov-13 22:13pm
v2
Comments
lukeer 4-Nov-13 4:10am
   
Use the "Improve question" link and provide the code you use to "select image". Wrap code in tags like these:
<pre lang="c#">YourCodeHere();</pre>

Oh, and leave a comment on this comment so that I get informed of your updated question. Thanks.
raeeschaudhary 4-Nov-13 4:13am
   
ok thanx

Your code:
C#
if (pictureBox1.Image != null)
    {
    Common.MessageBoxUtilities.WarningMessageBox("You Have Image");
    }
else if (pictureBox1.Image == null)
    {
    Common.MessageBoxUtilities.WarningMessageBox("You Dont Have Image");
    }
(which is the same as this:
C#
if (pictureBox1.Image != null)
    {
    Common.MessageBoxUtilities.WarningMessageBox("You Have Image");
    }
else
    {
    Common.MessageBoxUtilities.WarningMessageBox("You Dont Have Image");
    }
but less efficient and less readable.)

If it always shows "You Dont Have Image", then that is because you don't have an image: the Image property is null, or empty. We can't fix that, because you either haven't set an image into the PictureBox, or you have cleared it out - we can't access your HDD or see your screen, so we can't say: "look here - it's that bit."

So, use Visual Studio: right click the word pictureBox1 in your code view, and select "Find all references". This will bring up a list of everywhere the PictureBox is used in your code. Do any of them set the Image property? If not, that's your problem. If they do, what do they set it with? Set a breakpoint and look at it when you run your code.
   
Make Sure To Remember the Property you use.
I was setting BackgroundImage Property and Accessing the Image Property, sounds awful though.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900