CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60

    Question Writing data from txt file..Help!

    Hi,

    I have a text file with data similar to this:

    [List1]
    Item1
    Item2
    Item3

    [List2]
    Item1

    [List3]
    Item 1
    Item 2
    Item 3
    Item 4
    Item 5

    Etc...

    I have a command button and a number of Labels on my form form, Lbl1, Lbl2,etc.

    I want to click button and input box to come up, then if you type list1 all items under list 1 in txt file go to labels, but stops at last item in list.

    If there is only a few items in a list, i dont want all labels to be used, only as required.

    Any info on this?

    Thanks

    Mark.

  2. #2
    Join Date
    Aug 2003
    Location
    Buenos Aires, Argentina
    Posts
    513
    You can dinamically load labels. Create an only invisible label with its index property to 0. Then, for each line in the txt:

    Dim intCount as Integer
    intCount = Label1.Count
    Load Label1(intCount)
    Label1(intCount-1).Left = Label1(intCount-2).Left
    Label1(intCount-1).Top = Label1(intCount-2).Top + 200
    Label1(intCount-1).Visible = True
    Label1(intCount-1).Caption = line data


    Before you unload the form, you do:
    Dim intIndex as Integer
    For intIndex = 1 To intCount-1
    Unload Label1(intIndex)
    Next

  3. #3
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60
    Hi,

    Thanks for the reply. But how do i get the data in from the text file, for the list which is selected in input box?

    Mark

  4. #4
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Seeing as your text file is formatted like an ini file, you can use "GetPrivateProfileString" as follows:

    Code:
    Option Explicit
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    ' Globals - we might be able to re-use some of this stuff
    Public gsRetFile As String
    Public gsDBServer As String
    Public gsDatabaseName As String
    
    Public Function GetIniSettings() As Boolean
        ' 
        On Error GoTo ErrTrap
    
        Dim ret As Long
        Dim nSize As Integer
        Dim tmpString As String * 128
    
        ' Retrieve all the ini settings from the ini file
        gsRetFile = App.Path & "\" & App.EXEName & ".ini"
        tmpString = ""
    
        ' DatabaseName
        gsDatabaseName = ""
        lpAppName = "Database Settings"
        lpKeyName = "DatabaseName"
        lpDefault = "Unknown"
        lpFileName = gsRetFile
        ret = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, tmpString, Len(tmpString), lpFileName)
        gsDatabaseName = Left(tmpString, ret)
        tmpString = ""
    
       GetIniSettings = True
    End Function
    Be nice to Harley riders...

  5. #5
    Join Date
    Mar 2002
    Location
    United Kingdom
    Posts
    60
    Hi, thanks for the reply.

    I am not using database though, this code is for a game i am making :-)

    Also, how would I include the InputBox bit to say which list I want?

    Thanks

    Mark

  6. #6
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Doesn't matter whether you're using a database or not. That's just the section I wanted to get out of the text file.

    Here's the format of my text file - putting this and my last post together, you should be able to figure out how to read from a text file in the .ini format

    As you will see, the following line corresponds to the Section in the ini file that I'm after
    Code:
    lpAppName = "Database Settings"
    and the following line corresponds to the LINE in the above Section that I'm after
    Code:
    lpKeyName = "DatabaseName"

    [Database Settings]
    ODBC=FALSE
    DBServer=DevSQL
    DatabaseName=Sales2000Test
    UserName=Sa
    Password=
    ProgUser=bairdn
    DeleteOnCompletion=True
    TableName=tblContainerActivityNow
    DBTimeOut=45
    Be nice to Harley riders...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured