Results 1 to 11 of 11

Thread: [RESOLVED] Converting Text to a Long Value

Threaded View

  1. #10

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] Converting Text to a Long Value

    Quote Originally Posted by |2eM!x
    Can we get a small sample mark? For future reference..?
    I wanted to write a function that would format text in a RichText Box for a Calender control I was working on and I needed an easy way to indicate the color that I wanted to use other that using the Hex equivalent. I decided to use a collection to hold the values. I am using the String variable (ie. "vbRed") as the key, here is my code:

    VB Code:
    1. Option Explicit
    2. Dim Colors As New Collection
    3.  
    4. Private Sub Command1_Click()
    5.         With RichTextBox1
    6.             .Visible = False 'Hide RTB to Prevents Flickering
    7.             .Text = "" 'Clears previous text and formating
    8.            
    9.             .Text = "<B><U><FontName=Arial><FontSize=12>" & _
    10.                               "<Color=Red>The</U></B> </Color=Red></FontSize=12>" & _
    11.                               "<Color=Blue><B><FontSize=16>quick</FontSize=16> " & _
    12.                               "</Color=Blue><Color=Brown></B><I><FontSize=18> brown" & _
    13.                               "</FontSize=18></I></Color=Brown><Color=Purple> fox" & _
    14.                               "</Color=Purple></Color=Red></B></FontName=Arial>" & _
    15.                               vbNewLine & vbNewLine & vbNewLine & _
    16.                               "<Left><B>Left</B></Left>" & vbNewLine & _
    17.                               "<Center><B>Center</B></Center>" & vbNewLine & _
    18.                               "<Right><B>Right</B></Right>"
    19.            
    20.             Call RichTextBoxFormat(RichTextBox1)
    21.             .Visible = True
    22.         End With
    23. End Sub
    24.  
    25. Private Sub Form_Load()
    26.     With Colors 'Collection for Colors
    27.         .Add vbRed, "Red"
    28.         .Add vbBlue, "Blue"
    29.         .Add vbGreen, "Green"
    30.         .Add vbYellow, "Yellow"
    31.         .Add &H80FF&, "Orange"
    32.         .Add vbWhite, "White"
    33.         .Add vbBlack, "Black"
    34.         .Add &HC0C000, "LtBlue"
    35.         .Add &HFF00FF, "Purple"
    36.         .Add &H404080, "Brown"
    37.     End With
    38. End Sub
    39.  
    40. Private Sub RichTextBoxFormat(RTB As RichTextBox)
    41. Dim lngTag_Start As Long
    42. Dim lngTag_End As Integer
    43. Dim strTag_Start As String
    44. Dim strTag_End As String
    45. Dim strText As String
    46. Dim a As Long
    47.  
    48. For a = 1 To 31
    49.     strTag_Start = "<" & Choose(a, "B", "I", "U", "Center", "Left", _
    50.                                     "Right", "Color=Red", "Color=Blue", _
    51.                                     "Color=Green", "Color=Yellow", _
    52.                                     "Color=Orange", "Color=White", _
    53.                                     "Color=Black", "Color=LtBlue", _
    54.                                     "Color=Purple", "Color=Brown", _
    55.                                     "FontSize=8", "FontSize=10", "FontSize=12", _
    56.                                     "FontSize=14", "FontSize=16", "FontSize=18", _
    57.                                     "FontSize=20", "FontSize=22", "FontSize=24", _
    58.                                     "FontName=Arial", "FontName=Arial Black", _
    59.                                     "FontName=Comic Sans MS", _
    60.                                     "FontName=French Script MT", _
    61.                                     "FontName=Lucida Console", _
    62.                                     "FontName=Old English Text", _
    63.                                     "FontName=New Times Roman", _
    64.                                     "FontName=EAN-13", "FontName=UPCA", _
    65.                                     "FontName=UPCA", "FontName=UPC-E Short", _
    66.                                     "FontName=3 of 9 Barcode") & ">"
    67.                                        
    68.     strTag_End = "</" & Mid(strTag_Start, 2)
    69.    
    70.     lngTag_End = 1
    71.  
    72.     With RTB
    73.         strText = .Text
    74.        
    75.         Do While InStr(lngTag_End, strText, strTag_Start) > 0
    76.             'Loops through the text to find each tag
    77.            
    78.             .SelLength = 0
    79.             lngTag_Start = InStr(lngTag_End, strText, strTag_Start)
    80.             lngTag_End = InStr(lngTag_Start, strText, strTag_End)
    81.             .SelStart = lngTag_Start + Len(strTag_Start) - 1
    82.             .SelLength = ((lngTag_End - lngTag_Start) - Len(strTag_End)) + 1
    83.  
    84.             Select Case strTag_Start 'Formats the text based upon tag
    85.                 Case "<B>"  'Bold Text
    86.                     .SelBold = True
    87.                 Case "<I>"  'Italic Text
    88.                     .SelItalic = True
    89.                 Case "<U>"  'Underline Text
    90.                     .SelUnderline = True
    91.                 Case "<Center>" 'Center Align Text
    92.                     .SelAlignment = rtfCenter
    93.                 Case "<Left>"   'Left Align Text
    94.                     .SelAlignment = rtfLeft
    95.                 Case "<Right>"  'Right Align Text
    96.                     .SelAlignment = rtfRight
    97.                 Case Else
    98.                     If InStr(strTag_Start, "Color") > 0 Then 'Colors Text
    99.                         .SelColor = Colors.Item((Mid(strTag_Start, 8, Len(strTag_Start) - 8)))
    100.                     ElseIf InStr(strTag_Start, "FontSize") > 0 Then 'Font Size
    101.                         .SelFontSize = (Mid(strTag_Start, 11, Len(strTag_Start) - 11))
    102.                     ElseIf InStr(strTag_Start, "FontName") > 0 Then 'Font Name
    103.                         .SelFontName = (Mid(strTag_Start, 11, Len(strTag_Start) - 11))
    104.                     End If
    105.             End Select
    106.        
    107.             lngTag_End = lngTag_End + 1
    108.         Loop    'Check text for additional tags
    109.  
    110.         'Removes the Tags from the RichTextBox
    111.         Do While .Find(strTag_Start, 0) <> -1
    112.             .SelText = ""
    113.         Loop
    114.        
    115.         Do While .Find(strTag_End, 0) <> -1
    116.             .SelText = ""
    117.         Loop
    118.         .SelStart = 1
    119.     End With
    120. Next a
    121. End Sub

    Thanks to all that helped me with this code! Here is a link to my original post regarding RichTextBoxes and Tags.

    A picture of the Calendar Form:

    Last edited by Mark Gambo; Nov 30th, 2005 at 07:53 AM.
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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