Witam
Chciałbym się dowiedzieć czy jest możliwe usunięcie danej grafiki z Panelu. Chodzi mi o to by usunąć tylko ten obraz, grafikę, whatever. Poszukuję bardziej pomyślanego rozwiązania niż wyczyścić cały panel i od nowa rysować wszystkie elementy bez ostatniego.
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Pen kolor;
public Form1()
{
InitializeComponent();
}
int i = 0;
List<Figura> elementy= new List<Figura>();
Graphics a;
struct Figura
{
public int pos_x, pos_y, width;
public string typ, color;
public Figura(int x, int y, int widthh, string typp, string colorr)
{
pos_x = x;
pos_y = y;
width = widthh;
typ = typp;
color = colorr;
}
public void rysuj(Graphics g,Pen color)
{
switch (typ)
{
case "Koło":
g.DrawEllipse(color, pos_x, pos_y, width, width);
break;
case "Kwadrat":
g.DrawRectangle(color, pos_x, pos_y, width, width);
break;
case "Prostokąt":
g.DrawRectangle(color, pos_x, pos_y, width, width * 2);
break;
case "Trójkąt":
int r = Convert.ToInt32((double)(width * Math.Sqrt(3)) / 6);
g.DrawLine(color, pos_x, pos_y - 2 * r, pos_x + width / 2, pos_x + r);
g.DrawLine(color, pos_x + width / 2, pos_y + r, pos_x - width / 2, pos_y + r);
g.DrawLine(color, pos_x - width / 2, pos_y + r, pos_x, pos_y - 2 * r);
break;
}
}
}
private void panel1_Paint(object sender, PaintEventArgs e) // panel
{
a = panel1.CreateGraphics();
}
private void button2_Click(object sender, EventArgs e) // usuń
{
a.Clear(Color.White);
panel1.Refresh();
}
private void button1_Click(object sender, EventArgs e) // dodaj garfike
{
elementy.Add(new Figura(int.Parse(textBox1.Text),int.Parse(textBox2.Text),
int.Parse(numericUpDown1.Text), comboBox2.Text,comboBox1.Text));
switch (comboBox1.Text)
{
case "Red":
kolor = Pens.Red;
break;
case "Blue":
kolor = Pens.Blue;
break;
case "Yellow":
kolor = Pens.Yellow;
break;
case "Black":
kolor = Pens.Black;
break;
}
elementy[i].rysuj(a,kolor);
listBox1.Items.Add(elementy[i].typ);
i++;
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Red");
comboBox1.Items.Add("Yellow");
comboBox1.Items.Add("Blue");
comboBox1.Items.Add("Black");
comboBox2.Items.Add("Koło");
comboBox2.Items.Add("Kwadrat");
comboBox2.Items.Add("Trójkąt");
comboBox2.Items.Add("Prostokąt");
}
}
}