CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2001
    Location
    Barcelona , Spain
    Posts
    54

    Read from a File


    Hi ,

    I would like to read from a file ( text file or ...) some strings , like this :

    Font:"Arial"
    Font style:"Italic"
    Size:"12"
    Alignement:"Center"
    string:"hello "

    , and then write the string in a label or textbox with the font settings specified.

    I should do it with several strings , so may be I should create a control to manage it .

    Thanks so much .



  2. #2
    Join Date
    Jul 2001
    Location
    Trivandrum, Kerala, India
    Posts
    21

    Re: Read from a File


    Hai,

    Here is the code, which i hope meet your requirements. To run this code place a label in a form and paste the code to declaration section of the form.

    option Explicit

    private Sub Form_Load()
    SetFont
    End Sub

    private Sub SetFont()
    Dim strInfo as string

    ' open the file
    ' edit this line to suit your file name
    Open "c:\FontInfo.txt" for input as #1

    ' read line by line from the file
    Do While Not EOF(1) ' Loop until end of file.

    ' Read line into variable.
    Line input #1, strInfo

    ' set the label properties as per the read values
    Select Case LCase(LeftString(strInfo))
    Case "string"
    Label1 = RightString(strInfo)
    Case "font"
    Label1.FontName = RightString(strInfo)
    Case "size"
    Label1.FontSize = RightString(strInfo)
    End Select
    Loop
    Close #1 ' Close file.
    End Sub

    ' returns the left side of : of the line
    private Function LeftString(byval strLine as string)
    Dim iColPos as Integer

    ' assume there is always : in the line
    iColPos = InStr(1, strLine, ":", 1)

    ' return the left side portion
    LeftString = Left(strLine, iColPos - 1)
    End Function

    ' returns the left side of : of the line
    private Function RightString(byval strLine as string)
    Dim iColPos as Integer
    Dim strWithQuotes as string

    ' assume there is always : in the line
    iColPos = InStr(1, strLine, ":", 1)

    ' the right side portion is in double quotes.
    ' as a first step, get the string with quotes
    strWithQuotes = Right(strLine, len(strLine) - iColPos)

    ' return the string, stripped off "
    RightString = mid(strWithQuotes, 2, len(strWithQuotes) - 2)
    End Function




    I have skipped the font style and alignment. I hope they can be incorporated very easily.

    All the best.

    Kishore.


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