第一步:在窗體中拖入ListView控件和imageList控件;
第二步:設置imageList控件的Images屬性,添加你想要的圖片;
第三步:設置ListView控件的SmallImageList、LargeImageList、StateImageList屬性為imageList;
第四步:編輯ListView控件項的ImageIndex行為你就會發現圖片成功顯示出來了!
附:在ListView控件中添加選項的代碼
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("添加的內容不能為空");
textBox1.Focus(); //獲取焦點
}
else
{
if (listView1.Items.Count > 0) //判斷列表框中是否有項
{
//循環比較是否有重復項,有則放棄添加 hovertree.com
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 = "";
}
}
}
附ListView用法:
//更改屬性
this.listViewHoverTree.GridLines = true; //顯示表格線
this.listViewHoverTree.View = View.Details;//顯示表格細節
this.listViewHoverTree.LabelEdit = true; //是否可編輯,ListView只可編輯第一列。
this.listViewHoverTree.Scrollable = true;//有滾動條
this.listViewHoverTree.HeaderStyle = ColumnHeaderStyle.Clickable;//對表頭進行設置
this.listViewHoverTree.FullRowSelect = true;//是否可以選擇行
//this.listViewHoverTree.HotTracking = true;// 當選擇此屬性時則HoverSelection自動為true和Activation屬性為oneClick
//this.listViewHoverTree.HoverSelection = true;
//this.listViewHoverTree.Activation = ItemActivation.Standard; //
//添加表頭
this.listViewHoverTree.Columns.Add("", 0);
this.listViewHoverTree.Columns.Add("列1",80);
this.listViewHoverTree.Columns.Add("列2", 160);
//添加各項
ListViewItem[] p = new ListViewItem[2];
p[0] = new ListViewItem(new string[] { "","aaaa","bbbb"});
p[1] = new ListViewItem(new string[] { "","cccc", "ggggg" });
//p[0].SubItems[0].BackColor = Color.Red; //用於設置某行的背景顏色
this.listViewHoverTree.Items.AddRange(p);
//也可以用this.listViewHoverTree.Items.Add();不過需要在使用的前後添加Begin... 和End...防止界面自動刷新
// 添加分組
this.listViewHoverTree.Groups.Add(new ListViewGroup("tou"));
this.listViewHoverTree.Groups.Add(new ListViewGroup("wei"));
this.listViewHoverTree.Items[0].Group = this.listViewHoverTree.Groups[0];
this.listViewHoverTree.Items[1].Group = this.listViewHoverTree.Groups[1];