ListView 控件使用
时间:2011-06-17 来源:默默地
ListView
--------------------------------------------------------------------------------
//给ListView赋值
private void Form1_Load(object sender, System.EventArgs e)
{
//------ 创建 ------
//创建"列"实例
ColumnHeader ch = new ColumnHeader();
ColumnHeader ch1 = new ColumnHeader();
//列名,还可设置其它属性
ch.Text = "名字"; ch1.Text = "名字1";
//把列传给集合
listView1.Columns.Add(ch);
listView1.Columns.Add(ch1);
//------ 赋值 -------
string[] arr={"AA","BB"};
string[] ar1={"CC","DD"};
//创建行实例,用于控件的集合
ListViewItem lvi = new ListViewItem(arr);
ListViewItem lvi1 = new ListViewItem(ar1);
//把集合赋给listView
listView1.Items.Add(lvi);
listView1.Items.Add(lvi1);
}
//获取选中项的值
private void button1_Click(object sender, System.EventArgs e)
{
//构造
ListView.SelectedListViewItemCollection lvcItem = new ListView.SelectedListViewItemCollection(listView1);
//检查有无选中的项
if(lvcItem.Count>0) //第二列
textBox1.Text = lvcItem[0].SubItems[1].Text; //把选中项的第二列文本赋给textBox1
}
--------------------------------------------------------------------------------
//给ListView赋值
private void Form1_Load(object sender, System.EventArgs e)
{
//------ 创建 ------
//创建"列"实例
ColumnHeader ch = new ColumnHeader();
ColumnHeader ch1 = new ColumnHeader();
//列名,还可设置其它属性
ch.Text = "名字"; ch1.Text = "名字1";
//把列传给集合
listView1.Columns.Add(ch);
listView1.Columns.Add(ch1);
//------ 赋值 -------
string[] arr={"AA","BB"};
string[] ar1={"CC","DD"};
//创建行实例,用于控件的集合
ListViewItem lvi = new ListViewItem(arr);
ListViewItem lvi1 = new ListViewItem(ar1);
//把集合赋给listView
listView1.Items.Add(lvi);
listView1.Items.Add(lvi1);
}
//获取选中项的值
private void button1_Click(object sender, System.EventArgs e)
{
//构造
ListView.SelectedListViewItemCollection lvcItem = new ListView.SelectedListViewItemCollection(listView1);
//检查有无选中的项
if(lvcItem.Count>0) //第二列
textBox1.Text = lvcItem[0].SubItems[1].Text; //把选中项的第二列文本赋给textBox1
}
相关阅读 更多 +