Private Type jiancerecord
number As String * 1
name As String * 30
danwei As String * 10
shuxing As String * 8
liangchen As String * 10
End Type
Private Sub Cd1_Click() '写入数据
Dim fn As Integer
Dim rectmp As jiancerecord
Static n As Integer
n = n + 1
fn = FreeFile
Open "jiance.dat" For Random As #fn Len = Len(rectmp)
With rectmp
 .number = text1.Text
 .name = text2.Text
 .danwei = text3.Text
 .shuxing = Combo1.Text
 .liangchen = text4.Text
End With
Put #fn, n, rectmp
Close #fn
text1.Text = ""
text2.Text = ""
text3.Text = ""
text4.Text = ""
Combo1.Text = ""End Sub
Private Sub Command2_Click() '读取文件里所有数据
Dim fn As Integer
Dim rectmp As jiancerecord
 Static n As Integer
    
    n = n + 1
    
    If n > 1 Then
        MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
    End If
    MSHFlexGrid1.TopRow = MSHFlexGrid1.Rows - 1
fn = FreeFile
Open "jiance.dat" For Random As #fn Len = Len(rectmp)
Get #fn, n, rectmp
 MSHFlexGrid1.TextMatrix(1, 0) = rectmp.number
 MSHFlexGrid1.TextMatrix(1, 1) = rectmp.name
 MSHFlexGrid1.TextMatrix(1, 2) = rectmp.danwei
 MSHFlexGrid1.TextMatrix(1, 3) = rectmp.shuxing
 MSHFlexGrid1.TextMatrix(1, 4) = rectmp.liangchen
Close #fn
End Sub
Private Sub Form_Load()
Combo1.Clear
Combo1.AddItem ("模拟量")
Combo1.AddItem ("开关量")
Combo1.AddItem ("累计量")
Combo1.ListIndex = 0
With MSHFlexGrid1
   
    .Cols = 5
    .FixedCols = 0
    .FixedRows = 1
    .TextMatrix(0, 0) = "序号"
    .TextMatrix(0, 1) = "名称"
    .TextMatrix(0, 2) = "单位"
    .TextMatrix(0, 3) = "属性"
    .TextMatrix(0, 4) = "量程"
End With
End Sub我的程序是写入许多条数据,然后可以一次性读取出来并显示在MSHFlexGrid1中

解决方案 »

  1.   

    Private Sub Command2_Click() '读取文件里所有数据
    Dim fn As Integer
    Dim rectmp As jiancerecord
    Dim n As Integer
       
    fn = FreeFile
    Open "jiance.dat" For Random As #fn Len = Len(rectmp)
    Do Until EOF(fn)
    Get #fn, n, rectmpIf MSHFlexGrid1.TextMatrix(1, 0) > "" Then
        MSHFlexGrid1.Rows = MSHFlexGrid1.Rows + 1
    End If
    n = MSHFlexGrid1.Rows - 1 MSHFlexGrid1.TextMatrix(n, 0) = rectmp.number
     MSHFlexGrid1.TextMatrix(n, 1) = rectmp.name
     MSHFlexGrid1.TextMatrix(n, 2) = rectmp.danwei
     MSHFlexGrid1.TextMatrix(n, 3) = rectmp.shuxing
     MSHFlexGrid1.TextMatrix(n, 4) = rectmp.liangchen
    Loop
    Close #fn
    End Sub
      

  2.   

    不能一次性读取所有数据并显示在MSHFlexGrid1,按一下Command2显示一条数据,按一下Command2显示一条数据,我要求的是按一下数据能全显示出来