Results 1 to 6 of 6

Thread: [RESOLVED] improve my code

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    [RESOLVED] improve my code

    Hello
    Ive only been learning VB for a half year now so my knowledge is very ...basic ..., and I wrote this code to color all html tags (all words within < and >) within a rich text box in red...but I know its quite bad and I knew it wouldnt work when having ALOT of code...so can some of you pro's please improve it?

    Thanks

    VB Code:
    1. Dim start_p As Long
    2.     Dim end_p As Long
    3.     For a = 1 To Len(RT.Text)
    4.         If Mid(RT.Text, a, 1) = "<" Then
    5.             start_p = a
    6.         End If
    7.         If Mid(RT.Text, a, 1) = ">" Then
    8.             end_p = a
    9.             RT.SelStart = start_p - 1
    10.             RT.SelLength = (end_p - start_p)
    11.             RT.SelColor = RGB(255, 0, 0)
    12.         End If
    13.     Next a
    Last edited by Atheist; Nov 15th, 2005 at 05:18 PM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: improve my code

    its a bit faster using FIND (Built into rtb).. but also.. using the LockWindowUpdate API
    will keep it from flickering or scrolling like crazy
    VB Code:
    1. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    2.  
    3.  
    4. Private Sub Command1_Click()
    5. LockWindowUpdate RTB1.hWnd
    6. Dim Loc As Long
    7. Loc = 0
    8. Loc = RTB1.Find("<", Loc) + 1
    9. Do
    10.     RTB1.SelStart = Loc
    11.     RTB1.SelLength = InStr(Loc, RTB1.Text, ">") - Loc - 1
    12.     RTB1.SelColor = vbRed
    13.     Loc = RTB1.Find("<", Loc) + 1
    14. Loop While Loc > 0
    15. RTB1.SelStart = 0
    16. LockWindowUpdate 0&
    17. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: improve my code

    make a bool called "DoRed"

    Then in your keypress event for your rich text box do this;

    VB Code:
    1. Private Sub RT_KeyPress(KeyAscii As Integer)
    2.     If Chr(KeyAscii) = "<" Then DoRed = True
    3.     If DoRed Then
    4.         RT.SelColor = RGB(255, 0, 0)
    5.     Else
    6.         RT.SelColor = RGB(0, 0, 0)
    7.     End If
    8.    
    9.     If Chr(KeyAscii) = ">" Then DoRed = False
    10.    
    11. End Sub

  4. #4

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: improve my code

    Thanks static, works like a charm..expect the LockWindowUpdate API...it says "object required" each time i try to run...any idea why?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: improve my code

    Moved to code it better...

  6. #6
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: improve my code

    Quote Originally Posted by Atheist
    Thanks static, works like a charm..expect the LockWindowUpdate API...it says "object required" each time i try to run...any idea why?
    Did you change this line "RTB1.hWnd" to the name of your rich text box? Maybe you forgot to add the .hWnd after?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

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