LYHZM 发表于 2004-2-9 10:55:00

请问如何解决"当随机文件中的字段包含有汉字,则此字段之后的字段内容无法显

请问如何解决"当随机文件中的字段包含有汉字,则此字段之后的字段内容无法显示在列表框中"这一问题?请看下面的程序段落
----------------------------------------------
'定义用户自定义类型
Private Type SubstationDataGather_Type
                       Substation_Name As String * 20
                       Substation_Alias As String * 20
                       HighTensionCable_SegmentNumber As Integer
                       SumRb As Double
                       SumXb As Double
End Type
Dim SubstationDataGather_DataArray() As SubstationDataGather_Type
'在窗体初始化过程中把随机文件中的每一记录的各字段连接成一字符串,显示在列表框中
For RecordNumber = 1 To Lastrec
                               
                                                               Get #1, RecordNumber, SubstationDataGather_DataArray(RecordNumber)
                                                               With SubstationDataGather_DataArray(RecordNumber)
                                                                               
                                                                                                                        SubstationDataGatherList = Format(.Substation_Name, "@@@@@@@@@@@@@@@@@@@@") & "                       " & _
                                                                                                                                                                                                                                                                                                                                       Format(.Substation_Alias, "@@@@@@@@@@@@@@@@@@@@") & _
                                                                                                                                                                                                                                                                                                                                       Format(.HighTensionCable_SegmentNumber, "##") & "                                                                                                               " & _
                                                                                                                                                                                                                                                                                                                                       Format(Val(.SumRb), "##.#####") & "                       " & _
                                                                                                                                                                                                                                                                                                                                       Format(Val(.SumXb), "##.#####")
                                                                                                                                                                                                                                                                                               
                                                                                        End With
                                                                               
                                                                       '向列表框1中添加数据项, 即把数据显示在列表框1中
                                                                       ListBox1.AddItem SubstationDataGatherList
                                       Next RecordNumber
-----------------------------------------------------------------------------------------------
对于上面的程序段落,当随机文件中的某一记录的字段包含有汉字,则此字段之后的字段内容无法显示在列表框中,在列表框中显示如:
“2631变电所                                                                                                                                                                                                                                                                                                                                               ”
而实际应显示为:
“2631变电所                       2631                               1                      2.3333                               0.33333                                        ”
请问如何解决?

mccad 发表于 2004-2-9 13:02:00

你文本文件中的行文字是怎样转换到自定义数据类型SubstationDataGather_Type中的呢?

LYHZM 发表于 2004-2-9 19:49:00

谢谢mccad 老师的关注。文本文件中的行文字是通过以下程序转换到自定义数据类型SubstationDataGather_Type中的,恳请mccad 老师指正。
Dim Lastrec As Integer
                                       Dim RecordNumber As Integer
                                       Dim SubstationDataGatherList As String
                                                On Error Resume Next
                                               
                                       ReDim Preserve SubstationDataGather_DataArray(0)
                                       
                                       '以随机访问方式打开文件
                                       Open "f:\MyAutoCAD\MyVBA\SubstationDataGather.dat" For Random As #1 Len = Len(SubstationDataGather_DataArray(0))
                                                                                               
                                       Lastrec = LOF(1) / Len(SubstationDataGather_DataArray(0))
                                               
                                       ReDim Preserve SubstationDataGather_DataArray(Lastrec)
                                       '清空列表框1,为在其中显示全部记录做准备
                                       ListBox1.Clear
                                       
                                       
                                       '通过循环,把打开的硬盘上的文件"f:\MyAutoCAD\MyVBA\SubstationDataGather.dat"中的记录
                                       '一一赋予SubstationDataGather_Array()数组,再把数组中的数据一一加入到列表框中1显示出来
                                       
For RecordNumber = 1 To Lastrec
                               
                                                               Get #1, RecordNumber, SubstationDataGather_DataArray(RecordNumber)
                                                               With SubstationDataGather_DataArray(RecordNumber)
                                                                               
                                                                                                                        SubstationDataGatherList = Format(.Substation_Name, "@@@@@@@@@@@@@@@@@@@@") & "                       " & _
                                                                                                                                                                                                                                                                                                                                       Format(.Substation_Alias, "@@@@@@@@@@@@@@@@@@@@") & _
                                                                                                                                                                                                                                                                                                                                       Format(.HighTensionCable_SegmentNumber, "##") & "                                                                                                               " & _
                                                                                                                                                                                                                                                                                                                                       Format(Val(.SumRb), "##.#####") & "                       " & _
                                                                                                                                                                                                                                                                                                                                       Format(Val(.SumXb), "##.#####")
                                                                                                                                                                                                                                                                                               
                                                                                        End With
                                                                               
                                                                       '向列表框1中添加数据项, 即把数据显示在列表框1中
                                                                       ListBox1.AddItem SubstationDataGatherList
                                       Next RecordNumber
'关闭文件
Close #1

mccad 发表于 2004-2-9 20:36:00

我这里没有你所说的问题,以下是我试验的代码:
Dim SubstationDataGather_DataArray As SubstationDataGather_Type
Private Sub CommandButton1_Click()
       Open "d:\Gather.dat" For Random As #1 Len = Len(SubstationDataGather_DataArray)
       Get #1, 1, SubstationDataGather_DataArray
       Close #1
       With SubstationDataGather_DataArray
               SubstationDataGatherList = Format(.Substation_Name, "@@@@@@@@@@@@@@@@@@@@") & "       " & _
                                                                     Format(.Substation_Alias, "@@@@@@@@@@@@@@@@@@@@") & _
                                                                     Format(.HighTensionCable_SegmentNumber, "##") & "                           " & _
                                                                     Format(Val(.SumRb), "##.#####") & "       " & _
                                                                     Format(Val(.SumXb), "##.#####")
       End With
       '向列表框1中添加数据项, 即把数据显示在列表框1中
       ListBox1.AddItem SubstationDataGatherList
End SubPrivate Sub UserForm_Initialize()
       Open "d:\Gather.dat" For Random As #1 Len = Len(SubstationDataGather_DataArray)
       With SubstationDataGather_DataArray
               .Substation_Name = "2631变电所"
               .Substation_Alias = "2631"
               .HighTensionCable_SegmentNumber = 1
               .SumRb = 2.3333
               .SumXb = 2.3333
       End With
       Put #1, 1, SubstationDataGather_DataArray
       Close #1
End Sub

LYHZM 发表于 2004-2-10 12:02:00

mccad 老师,当我使用“逐语句”的方法运行我上面列出的程序时,在执行到包含有汉字的记录时,我把光标放在“ListBox1.AddItem SubstationDataGatherList”语句行的“SubstationDataGatherList”字符上,则此时能够出现如“2631变电所                       2631                               1                      2.3333                               0.33333                                        ”这样的正确提示值,但在列表框中就出现上面所述的错误显示,请问mccad 老师如何解决?
页: [1]
查看完整版本: 请问如何解决"当随机文件中的字段包含有汉字,则此字段之后的字段内容无法显