Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
TitleSave and restore FlexGrid data in a file when a program starts and stops
DescriptionThis example shows how to save and restore FlexGrid data in a file when a program starts and stops in Visual Basic 6. It uses Write to save the data when the program ends. It uses Input to read the data when the program starts.
KeywordsFlexGrid, save, restore
CategoriesControls, Files and Directories
 
Subroutine LoadData opens the data file. It uses Input to read the number or rows and columns displayed by the FlexGrid and sets the control's Rows and Cols properties.
 
Private Sub LoadData()
Dim file_name As String
Dim fnum As Integer
Dim max_row As Integer
Dim max_col As Integer
Dim R As Integer
Dim C As Integer
Dim txt As String
Dim max_len As Single
Dim new_len As Single

    file_name = App.Path
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"
    file_name = file_name & "FlexGrid.dat"

    fnum = FreeFile
    Open file_name For Input As fnum

    ' Hide the control until it's loaded.
    MSFlexGrid1.Visible = False
    DoEvents

    ' Get the maximum row and column.
    Input #fnum, max_row, max_col

    MSFlexGrid1.FixedCols = 0
    MSFlexGrid1.Cols = max_col + 1
    MSFlexGrid1.FixedRows = 1
    MSFlexGrid1.Rows = max_row + 1

    ' Load the cell entries.
    For R = 0 To max_row
        For C = 0 To max_col
            Input #fnum, txt
            MSFlexGrid1.TextMatrix(R, C) = txt
        Next C

        ' Read the last blank entry.
        Input #fnum, txt
    Next R

    Close #fnum

    ' Size the columns.
    Font.Name = MSFlexGrid1.Font.Name
    Font.Size = MSFlexGrid1.Font.Size
    For C = 0 To max_col
        max_len = 0
        For R = 0 To max_row
            new_len = TextWidth(MSFlexGrid1.TextMatrix(R, _
                C))
            If max_len < new_len Then max_len = new_len
        Next R
        MSFlexGrid1.ColWidth(C) = max_len + 240
        MSFlexGrid1.ColAlignment(C) = flexAlignLeftCenter
    Next C

    ' Display the control.
    MSFlexGrid1.Visible = True
End Sub
 
Subroutine SaveData opens the data file and writes the number of rows and columns into it. It then loops through the FlexGrid's cells writing its values into the file.
 
' Save the FlexGrid data.
Private Sub SaveData()
Dim file_name As String
Dim fnum As Integer
Dim max_row As Integer
Dim max_col As Integer
Dim R As Integer
Dim C As Integer

    file_name = App.Path
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"
    file_name = file_name & "FlexGrid.dat"

    fnum = FreeFile
    Open file_name For Output As fnum

    ' Save the maximum row and column.
    max_row = MSFlexGrid1.Rows - 1
    max_col = MSFlexGrid1.Cols - 1
    Write #fnum, max_row, max_col

    For R = 0 To max_row
        For C = 0 To max_col
            Write #fnum, MSFlexGrid1.TextMatrix(R, C);
        Next C
        Write #fnum,
    Next R

    Close fnum
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated