Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a beginner in programming and I'm trying to learn it by myself.So I have to add 2 different images to a picturebox ( in this case a table and a chair that are linked to 2 toolstripbuttons). I need to place multiple tables and chairs so I can move them or remove them afterwards. But when I placed tables and want to add the chairs my tables disappear.
Is there also a way to save the locations of the images in the picturebox so when I reopen the project the placed images will be on their previous locations?

What I have tried:

This is the main form
C#
<pre>namespace WORKSPACE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SPACE space;

        enum OPTION
        {
            PLACE,
            REMOVE,
            REPLACE
        }

        OPTION OPTIONS = OPTION.PLACE;

        Image figure;

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void pLACEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OPTIONS = OPTION.PLACE;
        }

        private void rEMOVEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OPTIONS = OPTION.REMOVE;

        }

        private void rEPLACEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OPTIONS = OPTION.REPLACE;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            toolStripStatusLabel1.Text = "X:" + e.X.ToString() + "Y:" + e.Y.ToString();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (OPTIONS== OPTION.PLACE)
            {
                space.PLACE(e.X, e.Y);
            }
            else
                if (OPTIONS == OPTION.REPLACE)
            {
                space.REPLACE(e.X, e.Y);
            }
            else
                if (OPTIONS == OPTION.REMOVE)
            {
                space.REMOVE(e.X, e.Y);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            figure = toolStripButton1.Image;
            space = new SPACE(pictureBox1.CreateGraphics(), figure);
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            figure = toolStripButton2.Image;
            space = new SPACE(pictureBox1.CreateGraphics(), figure);
        }
    }
}
This is the class I created


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace WORKSPACE
{
    class SPACE
    {
        int COUNTER = 0;
        
        
        Graphics G;
        List<Rectangle> SPACES;
        Rectangle MOUSE;
        Boolean STATUSREPLACE = false;
        int SPACECOUNTER;
        int DUMMY;
        Image figure;
        

        public SPACE(Graphics g,Image f)
        {
            G = g;
            figure = f;
          
            
            SPACES = new List<Rectangle>();
        }

        public void PLACE(int X, int Y)
        {
            COUNTER++;
            SPACES.Add(new Rectangle(X, Y, 20, 20));
            DRAWSPACE();
        }

        public void DRAWSPACE()
        {
            SPACETELLER = 1;
            G.Clear(Color.White);
            foreach (Rectangle r in SPACES)
            {
                G.DrawImage(figure,r.X,r.Y);
                SPACECOUNTER++;
            }
        }
        public void DRAWSPACECHAIR()
        {
            SPACECOUNTER = 1;
            G.Clear(Color.White);
            foreach (Rectangle r in SPACES)
            {
                G.DrawImage(figure, r.X, r.Y);
                SPACECOUNTER++;
            }
        }

        public void REPLACE(int X, int Y)
        {
            SPACECOUNTER = 1;
            MOUSE = new Rectangle(X, Y, 2, 2);
            foreach (Rectangle r in SPACES)
            {
                if (MOUSE.IntersectsWith(r))
                {
                    if (STATUSREPLACE == false)
                    {
                        SPACES.Remove(r);
                        STATUSREPLACE = true;
                        DUMMY = SPACECOUNTER;
                        return;
                    }
                }
                SPACECOUNTER++;
            }
            if (STATUSREPLACE == true)
            {
                SPACES.Insert(--DUMMY, new Rectangle(X, Y, 20, 20));
                STATUSREPLACE = false;
                DRAWSPACE();
                DRAWSPACECHAIR();
            }
        }
        public void REMOVE(int X, int Y)
        {
            SPACECOUNTER = 1;
            MOUSE = new Rectangle(X, Y, 2, 2);
            foreach (Rectangle r in SPACES)
            {
                if (MOUSE.IntersectsWith(r))
                {
                    
                    SPACES.Remove(r);
                    break;
                    
                }
                
            }
            DRAWSPACE();
            DRAWSPACECHAIR();
        }

    }  
}
Posted
Updated 25-Nov-17 3:51am
v2

1 solution

You can only put ONE image in a PictureBox control, so you have to paint the images yourself. Truthfully, I wouldn't use a PictrueBox for this at all, but scine you're learning, keep it.

You have to create a new Bitmap, paint your required images in that Bitmap, then set the PictureBox to show the new image.

When using the GDI graphic objects, make sure you call Dispose on the objects. You've got global-everyhing variables, which is bad practice. You only declare them where you need them.
 
Share this answer
 

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



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