CheckBox's and PictureBox1 in a unique way

rikor420

Member
Joined
Mar 5, 2018
Messages
9
Programming Experience
Beginner
C#:
 private void button1_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            this.pictureBox1.Image = Image.FromFile("images\\1.png");
        }
        if (checkBox2.Checked == true)
        {
            checkBox2.Checked = false;
            checkBox1.Checked = true;
            checkBox3.Checked = true;
            this.pictureBox1.Image = Image.FromFile("images\\2.png");
        }
        if (checkBox3.Checked == true)
        {
            checkBox2.Checked = false;
            checkBox1.Checked = false;
            this.pictureBox1.Image = Image.FromFile("images\\3.png");
        }
    }

code.png

Question is this, I want to be able to check checkbox1 and checkbox2 to show Image 3 then when i want to change the picture I clear it then able to select box 2 and 3 to show image 1, i am a beginner C# .NET forms, and i thought this was correct, but It seems i am doing something wrong in the code. Can somebody help me on what mistake i have taken and show me the right and correct way to get it to work. Thanks.
 
Well I just wasted time posted this, I just answered my question. I show it how I did it.
C#:
private void button2_Click(object sender, EventArgs e)
        {
            this.pictureBox1.Image = null;
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                this.pictureBox1.Image = Image.FromFile("images\\1.png");
            }
            if (checkBox2.Checked)
            {
                this.pictureBox1.Image = Image.FromFile("images\\2.png");
            }
            if (checkBox3.Checked)
            {
                this.pictureBox1.Image = Image.FromFile("images\\3.png");
            }
            if (checkBox1.Checked && checkBox2.Checked)
            {
                this.pictureBox1.Image = Image.FromFile("images\\3.png");
            }
        }
 
That's not really the best way to do it. As that code is, if both checkBox1 and checkBox2 are checked then you're going to create three Image objects and simply discard the first two. That's bad. Think it through. Test the most specific case first and only test other cases if it makes sense to do so. With that in mind, you should be testing like this:
if (checkBox3.Checked || (checkBox1.Checked && checkBox2.Checked))
{
    this.pictureBox1.Image = Image.FromFile("images\\3.png");
}
else if (checkBox1.Checked)
{
    this.pictureBox1.Image = Image.FromFile("images\\1.png");
}
else if (checkBox2.Checked)
{
    this.pictureBox1.Image = Image.FromFile("images\\2.png");
}

Also, if you are going to display a new Image object then you should be disposing the one you're replacing.
 
Ok i will give that a try, I am doing this project Mod for a game, there is going to a'lot of images to be incorporated, by checking certain checkbox's up 3 checkbox's to show an image.
 
Even more reason not to create objects that you don't need. You might also want to consider embedding the images in your EXE as resources rather than distributing them as loose files.
 
Even more reason not to create objects that you don't need. You might also want to consider embedding the images in your EXE as resources rather than distributing them as loose files.

C#:
private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox3.Checked || (checkBox1.Checked && checkBox2.Checked))
            {
                //this.pictureBox1.Image = Image.FromFile("images\\3.png");
                this.pictureBox1.Image = Resource1._1;
            }
        }

is this the correct way on a resource file?
 
You would add the resource on the Resources page of the project properties and then access it via Properties.Resources.Default.

Just note that you are creating a new object each time you access a resource property, so it is generally best to do it once only, assign the result to a field and use that repeatedly. Otherwise, be sure to dispose the objects in between.
 
You would add the resource on the Resources page of the project properties and then access it via Properties.Resources.Default.

Just note that you are creating a new object each time you access a resource property, so it is generally best to do it once only, assign the result to a field and use that repeatedly. Otherwise, be sure to dispose the objects in between.

Ok now i am confused. when i tried that properties.Resources.Default, there is no method or property for (Default) there is 2 methods (Equals,ReferenceEquals) and 2 properties (Culture, ResourceManager).
 
Sorry, I got mixed up with Properties.Settings,Default there. The property is on Properties.Resources, not Properties.Resources.Default. If you're not seeing a property for your image then you didn't add it via the Resources page of the project properties, as I instructed earlier. Once it's added as a resource, it's no longer a file. The whole point of resources is that they are data compiled into the executable itself.
 
Back
Top Bottom