[英]How to get ListView.SubItems
我正在嘗試制作一個根據用戶輸入將文本輸入文檔的程序,並且我目前將其顯示為 ListView。
我不知道如何從項目中獲取 SubItem,因為這是我當前的代碼。
For Each item In ListView1.Items
Dim inputString72 As String = "@bot.command()" + vbNewLine
My.Computer.FileSystem.WriteAllText(
SaveFileDialog1.FileName, inputString72, True)
Dim inputString73 As String = "async def " + item.Text + "(ctx):" + vbNewLine
My.Computer.FileSystem.WriteAllText(
SaveFileDialog1.FileName, inputString73, True)
Dim inputString74 As String = " await ctx.send('" + THE SUBITEM OF THE ITEM GOES HERE + "')" + vbNewLine
My.Computer.FileSystem.WriteAllText(
SaveFileDialog1.FileName, inputString74, True)
Next
我認為使用 .net 文件 class 會更有效。 無需在每次迭代中多次調用該方法。 從文檔。 “WriteAllText 方法打開一個文件,寫入它,然后關閉它。” 那是很多打開和關閉。 使用StringBuilder
(可變)構建一個字符串,然后向文件寫入一次。
我使用了插值字符串(它前面有一個 $),它允許您將變量直接放在用大括號括起來的字符串中。
SubItem
索引從第一列的 0 開始。 SubItem
的.Text
屬性將返回其內容。
Private Sub WriteListViewToFile(FilePath As String)
Dim sb As New StringBuilder
For Each item As ListViewItem In ListView1.Items
sb.AppendLine("@bot.command()")
sb.AppendLine($"async def {item.Text}(ctx):")
sb.AppendLine($" await ctx.send('{item.SubItems(1).Text}')")
Next
File.WriteAllText(FilePath, sb.ToString)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
WriteListViewToFile(SaveFileDialog1.FileName)
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.