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
<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();
}
}
}