Results 1 to 8 of 8

Thread: Word Wrap

  1. #1

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hi!

    I'm doing this User Control called Colored Label -- it's a regular label, but you can modify the colors in it. First, initialize the colors you wanna use using this function:

    ColoredLabel1.SetColor AscW("G"), RGB(0, 255, 0)

    This will initialize the G as Green (any color not initialized will stay as black). Now, to use this color, in the middle of the text in the Caption property, you type a special character (defined in the ColorModifier property) and the character in front of it will modify the color to the one defined for it (for example, "Testing #GGreen #0Testing" will display "Testing" in the default color, then "Green" in green and "Testing" in black, since 0 was not initialized).

    So, it was going fine until it came the time to word wrap (use multiple lines). This is the only clue I have, but I can't put it to work:

    1. Write the characters 1 by 1.
    2. When the total width acheived in the text is bigger than the width of the User Control, erase the previous letters 1 by 1 (also decreasing the counter if used in a For Next loop), until a Space character is found.
    3. Then, increase the distance from the top to te start of the text (a variable).
    4. Continue the loop until all characters are done.

    Well, that's it. If you think it's not enough, I can send you the source code (please don't do anything bad with it, like solving the problem and saying it's yours!).

    Thanks for your help!
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  2. #2
    Addicted Member Mih_Flyer's Avatar
    Join Date
    Mar 2000
    Location
    some place there
    Posts
    241
    send me copy, [email protected]

  3. #3
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Post

    hi, Jotaf98 Send me a copy too.

  4. #4
    Hyperactive Member
    Join Date
    Dec 1999
    Posts
    321
    Send me an ex too, please!
    Signed, Rodik ([email protected])
    Programmer,usesVB6ED
    ===========================
    Copyright©RodikCo,2002.

    Dont mind this signature ;] Its old

  5. #5

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Instead of sending it to you all...

    Instead of sending it to you all, I uploaded the file so you can download it. Please don't distribute saying it was you

    You'll find it at:
    http://freepages.ugo.com/jotaf98scie...l/files/CL.zip

    Thanks for your help

    Bye,

    -Jotaf98

    "Cyberspace forever"

    [email protected] - ICQ#60784495 - http://jotaf98.cjb.net
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  6. #6

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Signature

    Sorry, forgot that I already had a signature, so it appeared twice! =)
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  7. #7
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    You're in luck my boy. Fresh after writing a printing algorithim for someone, i am now an expert at word wrapping.

    This function will break up a string of text, and print it to a label called Label1. It should give you an idea of what to do.

    It will accept multi-line strings and words that are too long to fit in the label (It breaks these up).

    Have Fun


    This example requires :

    TextBox called Text1
    Label called Label1
    CommandButton called Command1
    Code:
    Option Explicit
    
    Private Sub printString(strText As String)
        Dim ipCrlf1 As Long, ipCrLf2 As Long    'line feed pointers
        Dim ipSp1 As Long, ipSp2 As Long        'space pointers
        
        Dim iCurX As Long   'the curent x position of the label
        
        Dim iWidth  As Long  'the width of the label
        Dim strWord As String, strLine As String    'Holds words and Lines
        Dim blLongWord As Boolean   'True if the word won't fit in the margins
        
        
        'get the width of the label
        iWidth = Label1.Width
        
        'set the font to that of the label box
        Me.Font = Label1.Font
        
        'initialse the line pointers
        ipCrlf1 = 1: ipCrLf2 = 1
        
        'break up text on line feeds
        Do
          ipSp1 = 1: ipSp2 = 1
          
          'find a line feed
          ipCrLf2 = InStr(ipCrlf1, strText, vbCrLf)
          If ipCrLf2 = 0 Then
            'if there isn't one then set the length to the string length
            ipCrLf2 = Len(strText) + 1
          End If
          
          'get the line of text from the string
          strLine = Mid$(strText, ipCrlf1, ipCrLf2 - ipCrlf1)
          
          Do
            'find a space
            ipSp2 = InStr(ipSp1, strLine, " ")
            If ipSp2 = 0 Then
              'if there isn't one then set the length to th string length
              ipSp2 = Len(strLine)
            End If
            
            'get the word
            strWord = Mid$(strLine, ipSp1, ipSp2 - ipSp1 + 1)
                
            blLongWord = False
            'Make sure the word will fit between the margins
            'if not we will have to chop the string
            If Me.TextWidth(strWord) > (iWidth) Then
              'find out where to chop string
              ipSp2 = chopString(strWord, (iWidth - iCurX)) + ipSp1
              blLongWord = True
              'get the chopped String
              strWord = Mid$(strLine, ipSp1, ipSp2 - ipSp1 + 1)
            End If
            
            'make sure the word won't pass the edge of the margin
            'if it will then drop down a line
            If iCurX + Me.TextWidth(strWord) > iWidth Then
              iCurX = 0
              Label1.Caption = Label1.Caption & vbCrLf
            End If
                    
            'print the word
            Label1.Caption = Label1.Caption & strWord
            
            'set the current X position
            iCurX = iCurX + Me.TextWidth(strWord)
            If blLongWord Then
              'set iCurX to the margin
              iCurX = 0
              Label1.Caption = Label1.Caption & vbCrLf
            End If
            
            'set the pointer
            ipSp1 = ipSp2 + 1
          Loop Until ipSp2 >= Len(strLine)
          
          'set the pointer.  +2 to miss the vbCrLf characters
          ipCrlf1 = ipCrLf2 + 2
          
          'finished a line so move curX to the margin
          iCurX = 0
          'and go down a line
          Label1.Caption = Label1.Caption & vbCrLf
        
        Loop Until ipCrLf2 >= Len(strText)
        
    End Sub
    
    
    Private Function chopString(stWord As String, iLength As Long) As Long
        Dim i As Integer
    
        '-----------------------------------------------'
        'This function chops a long word in half until  '
        'it is small enough that it will fit into the   '
        'Page margins.  It then loops to find the exact '
        'size that will fit to the edge of the margin.  '
        '-----------------------------------------------'
    
    
        'if the text is too wide for the margin, then chop it
        If TextWidth(stWord) > (iLength) Then
          'chop it in half
          chopString = chopString(Mid$(stWord, 1, Len(stWord) \ 2), iLength)
          If chopString = -1 Then
            'means we have a string that is small enough to work with
            'loop to find the exact righ size
            For i = Len(stWord) To 1 Step -1
              If TextWidth(Mid$(stWord, 1, i)) <= iLength Then
                chopString = i - 1
                Exit For
              End If
            Next i
          End If
        Else
          'the string has been choped small enough
          'to loop through to find the size to use
          chopString = -1
        End If
    
    End Function
    
    
    Private Sub Command1_Click()
        Label1.Caption = ""
        printString (Text1.Text)
    End Sub
    Iain, thats with an i by the way!

  8. #8

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Talking Thsnks, man!

    Thanks a lot! I'll include you in the "Thanks to" =)

    Bye!

    -Jotaf98

    "Cyberspace forever"

    [email protected] - ICQ#60784495 - http://jotaf98.cjb.net

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