Good night, I'm trying to solve a problem I encountered when trying to dynamically remove the picturebox I created:
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
int count = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// pictureBox1.Controls.Add(pictureBox2);
//pictureBox2.Location = new Point(0, 0);
// pictureBox2.BackColor = Color.Transparent;
}
public void drawimg(int x, int y, bool clear_)
{
PictureBox marker = new PictureBox();
if (!clear_)
{
count++;
marker.Location = new Point(x, y);
marker.Name = "marker" + count.ToString();
marker.Size = new Size(25, 25);
marker.BackColor = Color.Transparent;
marker.Image = Resource1.marcador;
marker.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Controls.Add(marker);
}
else
{
foreach (Control control in this.Controls)
{
if (control is PictureBox)
{
for (int i = 0; i <= count; i++)
{
if (control.Name == "marker" + i.ToString())
{
this.Controls.Remove(control);
//Não funciona, e gostaria de apenas remover as pictureboxes que eu criei //não todas...
}
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int locX_, locY_;
switch (DistritosList.GetItemText(DistritosList.SelectedItem))
{
case "Porto":
locX_ = 10;
locY_ = 10;
drawimg(locX_, locY_, false);
break;
case "Aveiro":
locX_ = 50;
locY_ = 50;
drawimg(locX_, locY_, false);
break;
case "Coimbra":
drawimg(0, 0, true);
break;
case "Leiria":
break;
}
}
}
}
Any suggestions or criticisms are always welcome!