Results 1 to 4 of 4

Thread: is there a better way of doing this code...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    is there a better way of doing this code...

    I have this code:

    Code:
    		private void markError(string errorString, System.Drawing.Color color)
    		{
    			int lastStrLoc = 0;
    			int strLoc = 0;
    			int errorStrLen = errorString.Length;
    			while((strLoc = rtbResult.Find(errorString, lastStrLoc, RichTextBoxFinds.None)) != -1)
    			{
    				lastStrLoc = strLoc + 1;
    				rtbResult.Select(strLoc, errorStrLen);
    				rtbResult.SelectionColor = color;
    			}
    		}
    It basically looks for the errorString and changes its text to red. Is there a better way of doing this? When there is over 1000 of the target string, it takes quite a long time.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: is there a better way of doing this code...

    Code:
    		private void markError(string errorString, System.Drawing.Color color)
    		{
    			rtbResult.Select(rtbResult.Text.IndexOf(errorString), errorString.Length);
    			rtbResult.SelectionColor = color;
    		}

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: is there a better way of doing this code...

    thanks. I also just found out about rtf codes that change font color. I'll try both and see which way would be faster.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: is there a better way of doing this code...

    A lot of the time will come from the text box being redrawn after each change. If you can lock the control from updating before doing the changes, and then unlock it, you should see an improvement.

    Usually you would do this with BeginUpdate() and EndUpdate() functions, but the RichTextBox doesn't seem to have those .

    Another solution using the Win32 API would be to import SendMessage, and send a WM_SETREDRAW with wParam = 1 to the handle of your richtextbox. Then, when you're done updating, send it again with wParam = 0.

    HTH!
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

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