按顺序读取Listview行 [英] Read Listview rows sequentially

查看:68
本文介绍了按顺序读取Listview行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



我有一个包含多列的Listview - 逐行添加行。

在给定的点上,按钮是单击并且Listview的内容必须写入文本文件。

Listview的每一行都是Text文件中的一行,每一列都用|分隔。在文本文件中。



使用下面的代码我得到:



ListViewSubItem:{03 / 09/2013} | ListViewSubItem:{USD / GBP} | ......等



而不是



03/09/2013 | USD / GBP | ..... etc



为什么要返回ListViewSubItem:{}?



任何人都可以建议我做错了吗?







 私有  Sub  UpdateJournal()
< span class =code-keyword> Dim JournalFileName As String = EtxJournal.dat
Dim JournalLine < span class =code-keyword> As String
Dim i < span class =code-keyword>作为 整数
Dim aryText( 4 作为 字符串


' ---创建日记
Dim objWriter < span class =code-keyword> As System.IO.StreamWriter(JournalFileName, False


' ---写入期刊

对于 x = 0 lsv_Journal.Items.Count - 1

JournalLine = lsv_Journal.Items(x).SubItems( 0 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 1 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 2 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 3 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 4 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 5 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 6 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 7 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 8 )。ToString& |& _
lsv_Journal.Items(x).SubItems( 9 )。ToString

objWriter.Write(JournalLine)

下一步 x

objWriter.Close()

结束 Sub

解决方案

你需要使用Text属性。

请注意,我使用了StreamWriter方法 WriteLine 而不是



 私人  Sub  UpdateJournal()
Dim JournalFileName As String = EtxJournal.dat
Dim JournalLine As String
Dim x 作为 整数
Dim aryText( 4 as 字符串
' ---创建日记
Dim objWriter As System.IO.StreamWriter(JournalFileName, False
' ---写入日记
对于 x = 0 lsv_Journal.Items.Count - 1
JournalLine = lsv_Journal.Items(x).SubItems( 0 )。文本& |& _
lsv_Journal.Items(x).SubItems( 1 )。文字& |& _
lsv_Journal.Items(x).SubItems( 2 )。文字& |& _
lsv_Journal.Items(x).SubItems( 3 )。文字& |& _
lsv_Journal.Items(x).SubItems( 4 )。文字& |& _
lsv_Journal.Items(x).SubItems( 5 )。文字& |& _
lsv_Journal.Items(x).SubItems( 6 )。文字& |& _
lsv_Journal.Items(x).SubItems( 7 )。文字& |& _
lsv_Journal.Items(x).SubItems( 8 )。文字& |& _
lsv_Journal.Items(x).SubItems( 9 )。文本
objWriter.WriteLine(JournalLine)
下一步 x
objWriter.Close()
结束 Sub





这是使用StringBuilder的替代方案:

  Imports  System.Text 

...


私有 Sub UpdateJournal()
Dim JournalFileName As String = EtxJournal.dat
Dim JournalLine As StringBuilder( 4096
Dim x As 整数
Dim y As 整数
Dim aryText( 4 As 字符串
' ---创建日记
Dim objWriter 作为 System.IO.StreamWriter(JournalFileName, False
' ---写入日记
对于 x = 0 lsv_journal.Items.Count - 1
对于 y = 0 9
JournalLine.Append(lsv_journal.Items(x).SubItems(y).Text)
如果 y<> 9 然后 JournalLine.Append( < span class =code-string> |)
下一页
objWriter.WriteLine(JournalLine.ToString)
下一步 x
objWriter.Close()
结束


Hi everyone.

I have a Listview with multiple columns - rows are progressively added.
At a given point a button is clicked and the contents of the Listview must be written to a text file.
Each row of the Listview is a line in the Text file and each column is delimited with | in the text file.

Using the code below I am getting :

ListViewSubItem: {03/09/2013}|ListViewSubItem: {USD/GBP}|......etc

instead of

03/09/2013|USD/GBP|.....etc

Why is "ListViewSubItem: {} being returned?

Can anyone please suggest what I am doing wrong?



Private Sub UpdateJournal()
     Dim JournalFileName As String = "EtxJournal.dat"
     Dim JournalLine As String
     Dim i As Integer
     Dim aryText(4) As String


     '---Create the Journal
     Dim objWriter As New System.IO.StreamWriter(JournalFileName, False)


     '---Write to Journal

     For x = 0 To lsv_Journal.Items.Count - 1

         JournalLine = lsv_Journal.Items(x).SubItems(0).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(1).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(2).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(3).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(4).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(5).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(6).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(7).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(8).ToString & "|" & _
                       lsv_Journal.Items(x).SubItems(9).ToString

         objWriter.Write(JournalLine)

     Next x

     objWriter.Close()

 End Sub

解决方案

You need to use the Text property.
Note that I used StreamWriter method WriteLine instead of Write.

Private Sub UpdateJournal()
     Dim JournalFileName As String = "EtxJournal.dat"
     Dim JournalLine As String
     Dim x As Integer
     Dim aryText(4) As String
     '---Create the Journal
     Dim objWriter As New System.IO.StreamWriter(JournalFileName, False)
     '---Write to Journal
     For x = 0 To lsv_Journal.Items.Count - 1
         JournalLine = lsv_Journal.Items(x).SubItems(0).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(1).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(2).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(3).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(4).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(5).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(6).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(7).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(8).Text & "|" & _
                       lsv_Journal.Items(x).SubItems(9).Text
         objWriter.WriteLine(JournalLine)
     Next x
     objWriter.Close()
 End Sub



Here is an alternative using StringBuilder:

Imports System.Text

...


Private Sub UpdateJournal()
    Dim JournalFileName As String = "EtxJournal.dat"
    Dim JournalLine As New StringBuilder(4096)
    Dim x As Integer
    Dim y As Integer
    Dim aryText(4) As String
    '---Create the Journal
    Dim objWriter As New System.IO.StreamWriter(JournalFileName, False)
    '---Write to Journal
    For x = 0 To lsv_journal.Items.Count - 1
        For y = 0 To 9
            JournalLine.Append(lsv_journal.Items(x).SubItems(y).Text)
            If y <> 9 Then JournalLine.Append("|")
        Next
        objWriter.WriteLine(JournalLine.ToString)
    Next x
    objWriter.Close()
End Sub


这篇关于按顺序读取Listview行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆