2011年11月4日

C# ListView 使用方式

1.建立一個Windows Form專案
2.拉入ListView 控制項
3.填入ListView的Header

程式碼如下:

                this.listView1.Clear();//清除ListView所有資料

            this.listView1.Columns.Add("AAA", 60, HorizontalAlignment.Left);
            this.listView1.Columns.Add("BBB", 100, HorizontalAlignment.Left);
            this.listView1.Columns.Add("CCC", 80, HorizontalAlignment.Left);
4.將ListView中的MultiSelect設為True
設定為true後,則可利用ctrl 來多選
若要取得在ListView中被選取的Item方法如下:

利用foreach迴圈 ,並宣告ListViewItem,使用Selected判斷是否被選取!

                    foreach (ListViewItem tmpLstView in listView1.Items)
                    {
                        if (tmpLstView.Selected == true)==>被選取為true
                        {
                            strList1 = strList1 + tmpLstView.Text + ",";
                            strWorkID1 = strWorkID1 + tmpLstView.SubItems[1].Text + ",";
                        }
                    }      
取得ListView中特定一項Item中的細項資料方法為:
 tmpLstView.SubItems[0].Text ==>取得AAA對應的值
 tmpLstView.SubItems[1].Text ==>取得BBB對應的值
 tmpLstView.SubItems[1].Text ==>取得ccc對應的值

PLC WriteDeviceRandom方法

在MX component Programming 中Act元件
提供了WriteDeviceRandom方法
需要填入的資料如下:
1.Device List
2.Total Write Device count
3.Device Value
假設現在要寫入的Device有D200,D208,D201
在C#中DEVICE要寫入的資料為:
string strTmp = "D200" + '\n' + "D208" + '\n' + "D201" + '\n'
在VB.Net中則為:
dim strTmp as string = "D200" + vblf + "D208" + vblf + "D201" + vblf