C# winform ListView 控件简单使用

ListView 控件简单使用。

C# winform ListView 控件简单使用

▲ 简单样式

listView控件名:listView_SerailNum

this.listView_SerailNum.FullRowSelect = true;
ColumnHeader cameraName = new ColumnHeader();
cameraName.Text = "相机";
cameraName.Width = 75;
cameraName.TextAlign = HorizontalAlignment.Center;
ColumnHeader cameraSerialNum = new ColumnHeader();
cameraSerialNum.Text = "序列号";
cameraSerialNum.Width = this.listView_SerailNum.Width - cameraName.Width - 4;
cameraSerialNum.TextAlign = HorizontalAlignment.Center;

listView_SerailNum.Columns.AddRange(new ColumnHeader[] { cameraName, cameraSerialNum });
listView_SerailNum.View = View.Details;
if (this.MachineHardware != null)
{
	ListViewItem lvItem1 = new ListViewItem(new string[] { "相机" + CameraIndex.A.ToString(), this.MachineHardware.CameraA.GetSerialNum() });
	ListViewItem lvItem2 = new ListViewItem(new string[] { "相机" + CameraIndex.B.ToString(), this.MachineHardware.CameraB.GetSerialNum() });
	ListViewItem lvItem3 = new ListViewItem(new string[] { "相机" + CameraIndex.C.ToString(), this.MachineHardware.CameraC.GetSerialNum() });
	this.listView_SerailNum.Items.Add(lvItem1);
	this.listView_SerailNum.Items.Add(lvItem2);
	this.listView_SerailNum.Items.Add(lvItem3);

	this.textBox1.Text = this.MachineHardware.CameraA.GetSerialNum();
	this.textBox2.Text = this.MachineHardware.CameraB.GetSerialNum();
	this.textBox3.Text = this.MachineHardware.CameraC.GetSerialNum();
}   

需要获取第二列的值是这样的,下标[1]直接去获取:

string serialNum = this.listView_SerailNum.FocusedItem.SubItems[1].Text;

FocusedItem是选中项。 可以对它进行判断,if (this.listView_SerailNum.FocusedItem == null) {},判断有没有选中的项。

控制表头显示与否的属性是:HeaderStyle = None; // 不显示表头

C# winform ListView 控件简单使用

上一篇:windows10配置jdk环境


下一篇:datagridview常用操作