C#实现在listview中插入图片实例代码

时间:2022-01-21 06:01:21

C#实现在listview中插入图片实例代码

第一步:在窗体中拖入ListView控件和imageList控件;

第二步:设置imageList控件的Images属性,添加你想要的图片;

第三步:设置ListView控件的SmallImageList、LargeImageList、StateImageList属性为imageList;

第四步:编辑ListView控件项的ImageIndex行为你就会发现图片成功显示出来了!

附:在ListView控件中添加选项的代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
private void button1_Click(object sender, EventArgs e)
 
  {
 
    if (textBox1.Text == "")
 
    {
 
      MessageBox.Show("添加的内容不能为空");
 
      textBox1.Focus(); //获取焦点
 
 
 
    }
 
    else
 
    {
 
      if (listView1.Items.Count > 0) //判断列表框中是否有项
 
      {
 
        //循环比较是否有重复项,有则放弃添加
 
        for (int i = 0; i < listView1.Items.Count; i++)
 
        {
 
          if (string.Compare(listView1.Items[i].Text.ToString(), textBox1.Text) == 0)
 
          {
 
            MessageBox.Show("项目重复,不能添加!");
 
            textBox1.Text = ""; //清空文本框
 
            textBox1.Focus();
 
            return;
 
          }
 
        }
 
        listView1.Items.Add(textBox1.Text.ToString());
 
        textBox1.Text = "";
 
      }
 
      else
 
      {
 
        listView1.Items.Add(textBox1.Text.ToString()); //将文本框中的数据添加到列表框
 
        textBox1.Text = "";
 
      }
 
 
 
    }
 
  }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://www.cnblogs.com/ngnetboy/archive/2012/03/11/2390137.html