无网不进  
软硬件开发

C#  ListView应用

1. 添加表头标题的方法

a. 直接在ListView控件上编写

b. 通过代码编写

 1             //动态添加lstv_ReaderList列表头
 2             /*
 3                 lstv_ReaderList.Columns.Add("序号", 50, HorizontalAlignment.Center);
 4                 lstv_ReaderList.Columns.Add("读写器IP", 90, HorizontalAlignment.Center);
 5                 lstv_ReaderList.Columns.Add("读写器Port", 75, HorizontalAlignment.Center);
 6                 lstv_ReaderList.Columns.Add("本机Port", 75, HorizontalAlignment.Center);
 7                 lstv_ReaderList.Columns.Add("通讯方式", 75, HorizontalAlignment.Center);
 8                 lstv_ReaderList.Columns.Add("参数配置", 80, HorizontalAlignment.Center);
 9                 lstv_ReaderList.Columns.Add("连接操作", 80, HorizontalAlignment.Center);
10                 lstv_ReaderList.Columns.Add("运行操作", 80, HorizontalAlignment.Center);
11             */

 

2. 在表格中插入控件的方法

    a. 直接通过代码进行添加

 1                 //参数配置
 2                 myitem.SubItems.Add("更新参数");
 3                 Button btn_SetParam = new Button();
 4                 btn_SetParam.Text = "更新参数";
 5                 btn_SetParam.Enabled = false;
 6                 btn_SetParam.BackColor = Color.Bisque;
 7                 btn_SetParam.Click += lstv_ReaderList_SetParam_Click;
 8                 lstv_ReaderList.Controls.Add(btn_SetParam);
 9                 btn_SetParam.Size = new Size(lstv_ReaderList.Items[row].SubItems[5].Bounds.Width,
10                 lstv_ReaderList.Items[row].SubItems[5].Bounds.Height);
11                 btn_SetParam.Location = new Point(lstv_ReaderList.Items[row].SubItems[5].Bounds.Left, lstv_ReaderList.Items[row].SubItems[5].Bounds.Top);
12 
13                 //连接操作
14                 myitem.SubItems.Add("打开连接");
15                 Button btn_Connect = new Button();
16                 btn_Connect.Text = "打开连接";
17                 btn_Connect.BackColor = Color.Bisque;
18                 btn_Connect.Click += lstv_ReaderList_btnConnect_Click;
19                 lstv_ReaderList.Controls.Add(btn_Connect);
20                 btn_Connect.Size = new Size(lstv_ReaderList.Items[row].SubItems[6].Bounds.Width,
21                 lstv_ReaderList.Items[row].SubItems[6].Bounds.Height);
22                 btn_Connect.Location = new Point(lstv_ReaderList.Items[row].SubItems[6].Bounds.Left, lstv_ReaderList.Items[row].SubItems[6].Bounds.Top);
23 
24                 //运行操作
25                 myitem.SubItems.Add("开启运行");
26                 Button btn_Run = new Button();
27                 btn_Run.Text = "开启运行";
28                 btn_Run.BackColor = Color.Bisque;
29                 btn_Run.Click += lstv_ReaderList_btnRun_Click;
30                 lstv_ReaderList.Controls.Add(btn_Run);
31                 btn_Run.Size = new Size(lstv_ReaderList.Items[row].SubItems[7].Bounds.Width,
32                 lstv_ReaderList.Items[row].SubItems[7].Bounds.Height);
33                 btn_Run.Location = new Point(lstv_ReaderList.Items[row].SubItems[7].Bounds.Left, lstv_ReaderList.Items[row].SubItems[7].Bounds.Top);

    b. 通过构建List<类>,在类中构建控件:

 1 类:
 2         class ReaderControl
 3         {
 4             public Button btn_ReaderParam;                                      //设置读写器参数按钮
 5             public Button btn_ReaderConect;                                     //设置读写器连接按钮
 6             public Button btn_ReaderRun;                                        //设置读写器运行按钮
 7 
 8         }  
 9                 //参数配置
10                 myitem.SubItems.Add("更新参数");
11                 readercontrol.btn_ReaderParam  = new Button();
12                 readercontrol.btn_ReaderParam.Text = "更新参数";
13                 readercontrol.btn_ReaderParam.Enabled = false;
14                 readercontrol.btn_ReaderParam.BackColor = Color.Bisque;
15                 readercontrol.btn_ReaderParam.Click += lstv_ReaderList_SetParam_Click;
16                 lstv_ReaderList.Controls.Add(readercontrol.btn_ReaderParam);
17                 readercontrol.btn_ReaderParam.Size = new Size(lstv_ReaderList.Items[row].SubItems[5].Bounds.Width,
18                 lstv_ReaderList.Items[row].SubItems[5].Bounds.Height);
19                 readercontrol.btn_ReaderParam.Location = new Point(lstv_ReaderList.Items[row].SubItems[5].Bounds.Left, lstv_ReaderList.Items[row].SubItems[5].Bounds.Top);
20 
21                 //连接操作
22                 myitem.SubItems.Add("打开连接");
23                 readercontrol.btn_ReaderConect = new Button();
24                 readercontrol.btn_ReaderConect.Text = "打开连接";
25                 readercontrol.btn_ReaderConect.BackColor = Color.Bisque;
26                 readercontrol.btn_ReaderConect.Click += lstv_ReaderList_btnConnect_Click;
27                 lstv_ReaderList.Controls.Add(readercontrol.btn_ReaderConect);
28                 readercontrol.btn_ReaderConect.Size = new Size(lstv_ReaderList.Items[row].SubItems[6].Bounds.Width,
29                 lstv_ReaderList.Items[row].SubItems[6].Bounds.Height);
30                 readercontrol.btn_ReaderConect.Location = new Point(lstv_ReaderList.Items[row].SubItems[6].Bounds.Left, lstv_ReaderList.Items[row].SubItems[6].Bounds.Top);
31 
32                 //运行操作
33                 myitem.SubItems.Add("开启运行");
34                 readercontrol.btn_ReaderRun = new Button();
35                 readercontrol.btn_ReaderRun.Text = "开启运行";
36                 readercontrol.btn_ReaderRun.BackColor = Color.Bisque;
37                 readercontrol.btn_ReaderRun.Click += lstv_ReaderList_btnRun_Click;
38                 lstv_ReaderList.Controls.Add(readercontrol.btn_ReaderRun);
39                 readercontrol.btn_ReaderRun.Size = new Size(lstv_ReaderList.Items[row].SubItems[7].Bounds.Width,
40                 lstv_ReaderList.Items[row].SubItems[7].Bounds.Height);
41                 readercontrol.btn_ReaderRun.Location = new Point(lstv_ReaderList.Items[row].SubItems[7].Bounds.Left, lstv_ReaderList.Items[row].SubItems[7].Bounds.Top);

2. 在表格中插入控件调用方法

   a.  直接通过代码的调用方法,通过控件位置进行匹配:

 1         private void lstv_ReaderList_btnConnect_Click(object sender, EventArgs e)
 2         {
 3             int btn_num = lstv_ReaderList.Controls.Count;
 4             for (int i = 0; i < btn_num; i++)
 5             {
 6                 Button btn = (Button)lstv_ReaderList.Controls[i];
 7                 if (((Button)sender).Location == btn.Location)
 8                 {
 9                     int btn_index = i / 3;
10                     //连接操作
11                     if (list_ReaderControl[btn_index].byte_ConnectState == 0)
12                     {
13                         Reader_Connect(btn, btn_index, 1);
14                     }
15                     else
16                     {
17                         Reader_Disconnect(btn, btn_index, 1);
18                     }
19                 }
20             }
21         }

 

  b.  直接通过List<类>调用方法:

 1         private void lstv_ReaderList_btnConnect_Click(object sender, EventArgs e)
 2         {
 3             for (int i = 0; i < lstv_ReaderList.Items.Count; i++)
 4             {
 5                 if (((Button)sender).Location == list_ReaderControl[i].btn_ReaderConect.Location)
 6                 {
 7                     //连接操作
 8                     if (list_ReaderControl[i].byte_ConnectState == 0)
 9                     {
10                         Reader_Connect(i);
11                     }
12                     else
13                     {
14                         Reader_Disconnect(i);
15                     }
16                 }
17             }
18         }

 

posted on 2018-12-24 18:13  无网不进  阅读(344)  评论(0编辑  收藏  举报