1. 程式人生 > >C# ListView應用

C# ListView應用

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         }