日期:2014-05-18  浏览次数:20976 次

关于listview中控件的激活问题。
打算实现如下功能:当点击listview中的某一项时,就新建一个picturebox来显示imagelist中存储的图片,代码如下:
C# code
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (e.IsSelected)
            {
                PictureBox pix1 = new PictureBox();
                //pix1.Image = imageList1.Images[listView1.FocusedItem.ImageIndex];
                pix1.Image = pic[listView1.FocusedItem.ImageIndex];
                pix1.SizeMode = PictureBoxSizeMode.AutoSize;
                pix1.MouseDown += new MouseEventHandler(pix1_MouseDown);
                pix1.MouseMove += new MouseEventHandler(pix1_MouseMove);
                this.panel1.Controls.Add(pix1);
            }
        }

  如何实现对于存在项的判断呢?就是当listview中的某一项已经点击过并新建了picturebox之后,再次点击这一项,不再新建picturebox,而是激活这一项对应的picturebox。求解答。

------解决方案--------------------
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
{
PictureBox pix1 = new PictureBox();
//pix1.Image = imageList1.Images[listView1.FocusedItem.ImageIndex];
pix1.Image = pic[listView1.FocusedItem.ImageIndex];
pix1.SizeMode = PictureBoxSizeMode.AutoSize;
pix1.MouseDown += new MouseEventHandler(pix1_MouseDown);
pix1.MouseMove += new MouseEventHandler(pix1_MouseMove);
pix1.Name = "picturebox_id" + listView1.SelectIndex.ToString();
this.panel1.Controls.Add(pix1);
}
}

判断:

if (this.Controls.Cast<Control>().Any(x => x.Name == "picturebox_id" + SelectIndex.ToString()))
{
...//存在
}
------解决方案--------------------
不用每次都new新的picturebox,一个就可以了

------解决方案--------------------
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)

看不懂先查msdn。问google,百度。