首页 > Windows开发 > 详细

C# winform ListView 控件简单使用

时间:2021-06-21 11:37:23      阅读:12      评论:0      收藏:0      [点我收藏+]

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 控件简单使用

原文:https://www.cnblogs.com/huvjie/p/14910984.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!