62,041
社区成员
发帖
与我相关
我的任务
分享
//生成图像组件代码
PictureBox pb = new PictureBox();
pb.Name = name;
pb.Location = new Point(locX, locY);
pb.BackColor = Color.LightBlue;
pb.ImageLocation =strFilepath;
pb.Tag = 0;// nameX;//预设值等于自增
MapArea.Controls.Add(pb);
pb.Parent = this.MapArea;
//清空图片区子成员
public void ClearMap()
{
foreach (Control picBox in this.MapArea.Controls)
{
if (picBox is PictureBox)
{
if (picBox.Name != "BgImg")
{
PictureBox pb = picBox as PictureBox;
MapArea.Controls.Remove(pb); //移除它
}
}
}
}
for (int i = this.MapArea.Controls.Count; i > 0; i--)
{
if (picBox is PictureBox)
{
if (picBox.Name != "BgImg")
{
PictureBox pb = picBox as PictureBox;
MapArea.Controls.Remove(pb); //移除它
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
//测试数据
this.panel1.Controls.Add(new Label() { Top = 10, Text = "1111",Width=20,Height=20 });
this.panel1.Controls.Add(new Label() { Top = 30, Text = "2222", Width = 20, Height = 20 });
this.panel1.Controls.Add(new Label() { Top = 50, Text = "3333", Width = 20, Height = 20 });
}
private void button1_Click(object sender, EventArgs e)
{
for (int i=0;i<this.panel1.Controls.Count;i++)
{
if (((Label)this.panel1.Controls[i]).Text != "2222")
{
this.panel1.Controls.Remove(this.panel1.Controls[i]);
i = 0; //删除后,重置索引
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>() { 0,1,2,3,4,5,6,7,8,9 };
List<int> list2 = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Console.WriteLine("---- list ----");
try
{
foreach (int i in list)
{
if (i % 2 == 0)
{
Console.WriteLine("list:{0} has been removed.", i);
list.Remove(i);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("---- list2 ----");
for(int i=list2.Max();i>=0;i--)
{
if (i % 2 == 0)
{
Console.WriteLine("list2:{0} has been removed.", i);
list.Remove(i);
}
}
Console.Read();
}
}
}