C# ListView應用spa
1. 添加表頭標題的方法code
a. 直接在ListView控件上編寫blog
b. 經過代碼編寫it
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. 在表格中插入控件的方法io
a. 直接經過代碼進行添加class
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. 在表格中插入控件調用方法object
a. 直接經過代碼的調用方法,經過控件位置進行匹配:List
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 }