[英]Getting the text of a listview subitem
我正在嘗試獲取 vb.net 中的 ListView 子項的文本。 根據這個答案,下面的代碼應該可以工作。
Function gen() As String
Dim Log = New Logging.Log("gen")
Dim Code As String = getTemplateCode()
For Each i As ListViewItem In ListView1.Items
Log.WriteEntry("Item: " + i.Text)
If i.SubItems.Count > 0 Then
Log.WriteEntry("Item: " + i.SubItems(0).Text)
Code = Code.Replace(i.Text, i.SubItems(0).Text)
End If
Next
Return Code
End Function
但是,我沒有在ListViewItem.SubItem.Text
中獲取字符串,而是得到了似乎是ListViewItem.Text
的內容,但無法弄清楚原因。
這是來自上述代碼的實際日志 output:
gen Information: 0 : Item: $Parshah$
gen Information: 0 : Item: $Parshah$
gen Information: 0 : Item: $Year$
gen Information: 0 : Item: $Year$
gen Information: 0 : Item: $EnglishId$
gen Information: 0 : Item: $EnglishId$
gen Information: 0 : Item: $books$
gen Information: 0 : Item: $books$
gen Information: 0 : Item: $title$
gen Information: 0 : Item: $title$
和列表視圖的屏幕截圖
日志 output 應該是
gen Information: 0 : Item: $Parshah$
gen Information: 0 : Item: nothing
gen Information: 0 : Item: $Year$
gen Information: 0 : Item: nothing
gen Information: 0 : Item: $EnglishId$
gen Information: 0 : Item: nothing
gen Information: 0 : Item: $books$
gen Information: 0 : Item: nothing
gen Information: 0 : Item: $title$
gen Information: 0 : Item: nothing
我究竟做錯了什么?
好的。 愚蠢的錯誤。 一項研究告訴我,ListViewItem 在其第一個子項中包含自身,並且第一個動態添加的子項以 1 而不是 0 的索引開始。 (哎呀。)想想批處理 Arguments 其中 %~0 是文件本身......哈哈。
所以,我們改變這樣的代碼:
Function gen() As String
Dim Log = New Logging.Log("gen")
Dim Code As String = getTemplateCode()
For Each i As ListViewItem In ListView1.Items
Log.WriteEntry("Item: " + i.Text)
If i.SubItems.Count > 0 Then
Log.WriteEntry("Item: " + i.SubItems(1).Text) 'changed 0 to 1
Code = Code.Replace(i.Text, i.SubItems(1).Text) 'changed 0 to 1
End If
Next
Return Code
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.