Tipp 0304 Daten aus Textdatei einlesen und speichern
Autor/Einsender:
Datum:
  Angie
18.01.2003
Entwicklungsumgebung:   VB 5
Mit nachfolgendem Beispiel lässt sich der Inhalt eines MSHFlexGrid-Steuerelements zeilenweise sowohl aus einer Textdatei einlesen als auch in eine Textdatei speichern. Das Trennzeichen für die einzelnen Spalteninhalte ist wählbar, sollte aber beim Einlesen der Textdatei bekannt sein. Optional kann angegeben werden, wie viele feststehende Zeilen/Spalten im Grid eingestellt werden sollen.
Code im Modul
 
Option Explicit

Public Sub LoadGridData(ByVal vGrid As MSHFlexGrid, ByVal _
             vstrFile As String, ByVal vstrSep As String, _
             Optional ByVal nFixedCols As Long = 1, _
             Optional ByVal nFixedRows As Long = 1)

  Dim Fn As Integer

  Dim astrData As Variant
  Dim intCols As Integer

  Dim strTemp As String

  Dim r As Long
  Dim c As Long

  Fn = FreeFile()
  Open vstrFile For Input As #Fn

  With vGrid
    .Redraw = False

    .Rows = 0
    .Cols = 0

    Do While Not EOF(Fn)
      Line Input #Fn, strTemp

      If Len(strTemp) <> 0 Then
        astrData = Split(strTemp, vstrSep)
        intCols = UBound(astrData)

        If intCols + 1 > .Cols Then .Cols = intCols + 1

        .Rows = .Rows + 1
        r = .Rows - 1

        For c = 0 To intCols
          .TextMatrix(r, c) = astrData©
        Next c

      Else
        .Rows = .Rows + 1
      End If
    Loop

    .Redraw = True
  End With

  Close #Fn

  With vGrid
    If .Rows >= nFixedRows + 1 Then
      .FixedRows = nFixedRows
    Else
      .FixedRows = .Rows - 1
    End If

    If .Cols >= nFixedCols + 1 Then
      .FixedCols = nFixedCols
    Else
      .FixedCols = .Cols - 1
    End If

    If .Cols > 1 Then
      For c = 0 To .Cols - 1
        .ColWidth© = 900
      Next c
    Else
      .ColWidth(0) = .Width * 0.99
    End If
  End With
End Sub

Public Sub SaveGridData(ByVal vGrid As MSHFlexGrid, ByVal _
               vstrFile As String, ByVal vstrSep As String)

  Dim Fn As Integer

  Dim nRows As Long
  Dim nCols As Long

  Dim strTemp As String

  Dim r As Long
  Dim c As Long

  With vGrid
    nRows = .Rows - 1
    nCols = .Cols - 1

    Fn = FreeFile()
    Open vstrFile For Output As #Fn
      For r = 0 To nRows
        strTemp = vbNullString
        For c = 0 To nCols - 1
          strTemp = strTemp & (.TextMatrix(r, c) & vstrSep)
        Next c
        strTemp = strTemp & .TextMatrix(r, c)

        Print #Fn, strTemp
      Next r
    Close #Fn
  End With
End Sub
 
Code im Codebereich der Form
 
Option Explicit

Private m_strFileName As String
Private m_strSep As String

Private Sub Form_Load()
  Dim strPath As String
  Dim strFile As String

  With Me.mshgData
    .Rows = 0
    .Cols = 0
  End With

  optSeparator_Click 3

  strPath = App.Path
  If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"

  strFile = strPath & "TestPipeZeichen.txt"
  If Len(Dir(strFile)) <> 0 Then
    LoadGridData Me.mshgData, strFile, m_strSep
  End If
End Sub

Private Sub optSeparator_Click(Index As Integer)
  Select Case Index
    Case 0:  m_strSep = vbTab
    Case 1:  m_strSep = ";"
    Case 2:  m_strSep = " "
    Case 3:  m_strSep = "|"
  End Select
End Sub

Private Sub cmdFileOpen_Click()
  On Error GoTo err_Handler
  With Me.dlgFileOpenSave
    .CancelError = True
    .DialogTitle = "Öffnen"
    .InitDir = App.Path
    .Filter = "Textdateien (*.txt;*.csv)|*.txt;*.csv"
    .Flags = cdlOFNFileMustExist
    .FileName = m_strFileName

    .ShowOpen

    m_strFileName = .FileName
    LoadGridData Me.mshgData, m_strFileName, m_strSep
  End With
  Exit Sub

err_Handler:
  Exit Sub
End Sub

Private Sub cmdFileSave_Click()
  With Me.mshgData
    If .Rows = 0 And .Cols = 0 Then
      MsgBox "Im MSHFlexGrid sind keine Daten zum Speichern " & _
             "enthalten..:-)", vbOKOnly + vbInformation, _
             Title:=Me.Caption
      cmdFileOpen_Click
      Exit Sub
    End If
  End With

  On Error GoTo err_Handler
  With Me.dlgFileOpenSave
    .CancelError = True
    .DialogTitle = "Speichern unter"
    .InitDir = CurDir$
    .Filter = "Textdatei (*.txt)|*.txt"
    .FileName = m_strFileName

    .ShowSave

    m_strFileName = .FileName
    SaveGridData Me.mshgData, m_strFileName, m_strSep
  End With
  Exit Sub

err_Handler:
  Exit Sub
End Sub
 
Hinweise
Das Download-Beispiel enthält einen Ersatz für die in VB 6 vorhandene Split-Funktion.
Um diesen Tipp ausführen zu können, muss das Microsoft Hierarchical FlexGrid Control als Komponente in das Projekt eingebunden werden.
Weitere Links zum Thema
ListView-Steuerelement laden und speichern
Programm-Einstellungen speichern (INI-Datei)
Programm-Einstellungen speichern (SaveSetting)

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (8,1 kB) Downloads bisher: [ 3515 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Samstag, 28. Mai 2011