4 Replies - 1187 Views - Last Post: 22 May 2012 - 02:29 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Using Finder To Find Text In RichTextBox Is My Code Sound?

Posted 19 May 2012 - 03:28 PM

So one of the things that gets me is you take the tutorials on like
This Tutorial REXPAD

He has his own algorithm for a find to find text in a textbox. Somehow though whenever I look at code like his since I didn't write it it DOES HELP GIVE ME IDEAS, but using the actual CODE CONFUSES ME MORE.

So I decided to write my own algorithm to try and find search strings in a textbox but I never know if my algorithm is inferior or superior, or if it's acceptable, or it it doesn't really matter that much. IT DOES WORK here is the code:


 public bool b_SearchNext = false;
        public int index = 0;
        private void tlstrp_btn_find_Click(object sender, EventArgs e)
        {
            if (b_SearchNext == false)
            {
                index = rchtxtbox_main_content.Find("hi", index, -1, RichTextBoxFinds.MatchCase);
                if (index == -1)
                {
                    switch (MessageBox.Show("There Are No More Instances Of hi Would You Like To Start Over Press Yes, Or Press No To Quit?", "Quit Or Start Over", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                    {
                        case DialogResult.Yes:
                            //set variables back
                            b_SearchNext = false;
                            index = 0;
                            break;
                        case DialogResult.No:
                            this.Close();
                            break;
                    }
                }
                b_SearchNext = true;
            }
            else
            {
                index += 1;
                index = rchtxtbox_main_content.Find("hi", index, -1, RichTextBoxFinds.MatchCase);

                if (index == -1)
                {
                    switch (MessageBox.Show("There Are No More Instances Of hi Would You Like To Start Over Press Yes, Or Press No To Quit?", "Quit Or Start Over", MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                    {
                        case DialogResult.Yes:
                            //set variables back
                            b_SearchNext = false;
                            index = 0;
                            break;
                        case DialogResult.No:
                            this.Close();
                            break;
                    }
                }
            }
        }






So just using this as an example to try and find the word "hi" THIS COULD BE SUBSTITUTED for a users input at runtime like textbox1.Text etc. You get that point.

Also To insure that the public variables index, and the boolian search next were set back to their defaults I would create an event with the textbox so as soon as the string was changed it would change the index back to zero and b_SearchNext back to false.

This algorithm would work fine at that point. (NOTE: I HAVE TESTED IT). Is this better than the RexPad algorithm though (which I have a hard time ever understanding), or is it EQUAL?

Is it better to write your own code/algorithm's for things like this? I feel like I learn more when I do. What should I do; should I feel like my algorithm's are inferior and therefore I am also an inferior programmer? Note: This code goes from top to bottom and highlights instances of the string.

This post has been edited by adn258: 19 May 2012 - 03:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using Finder To Find Text In RichTextBox Is My Code Sound?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,492
  • Joined: 05-May 12

Re: Using Finder To Find Text In RichTextBox Is My Code Sound?

Posted 21 May 2012 - 02:15 AM

To quote from Star Trek II: Wrath of Kahn "Your intellect is superior."

It is superior in that your Find Next at least attempts to find the next occurrence of the matching string rather than just always finding the first occurrence only. You can probably do some code folding to trim down your code. Notice that there is only a few lines different between your success case and your failure case in your if statement.

Anyway, despite the bug in RexPad, I would completely dismiss the code in RexPad because it tries very hard to separate UI code from logic code. It also tries to reuse code so that Find, FindNext, and Replace would all share the same bit of code. He can do this because he separates the program logic from the UI.

Anyway, what is it about the RexPad approach to searching that you do not understand? He is simply using String.IndexOf() ( http://msdn.microsof...ibrary/k8b1470s ) to do the searches. (The bug would be fixed if he used the variant that take a start index: http://msdn.microsof...ibrary/7cct0x33 ). You on the other hand are using the RichTextBox.Find() ( http://msdn.microsof...y/yab8wkhy.aspx ) to do the searches.

Personally, using the RichTextBox.Find() is more efficient because a string doesn't have to be marshalled from the unmanaged RichEdit control into a managed string so that String.IndexOf() can be run, so again your approach is superior. :-)

This post has been edited by Skydiver: 21 May 2012 - 02:16 AM

Was This Post Helpful? 2
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Using Finder To Find Text In RichTextBox Is My Code Sound?

Posted 21 May 2012 - 11:18 PM

View PostSkydiver, on 21 May 2012 - 02:15 AM, said:

To quote from Star Trek II: Wrath of Kahn "Your intellect is superior."

It is superior in that your Find Next at least attempts to find the next occurrence of the matching string rather than just always finding the first occurrence only. You can probably do some code folding to trim down your code. Notice that there is only a few lines different between your success case and your failure case in your if statement.

Anyway, despite the bug in RexPad, I would completely dismiss the code in RexPad because it tries very hard to separate UI code from logic code. It also tries to reuse code so that Find, FindNext, and Replace would all share the same bit of code. He can do this because he separates the program logic from the UI.

Anyway, what is it about the RexPad approach to searching that you do not understand? He is simply using String.IndexOf() ( http://msdn.microsof...ibrary/k8b1470s ) to do the searches. (The bug would be fixed if he used the variant that take a start index: http://msdn.microsof...ibrary/7cct0x33 ). You on the other hand are using the RichTextBox.Find() ( http://msdn.microsof...y/yab8wkhy.aspx ) to do the searches.

Personally, using the RichTextBox.Find() is more efficient because a string doesn't have to be marshalled from the unmanaged RichEdit control into a managed string so that String.IndexOf() can be run, so again your approach is superior. :-)


Thanks for your compliment man that feels good. I am getting better with programming and thinking of my own Algorithm's for things like this. I have only been programming for a couple years now when I have time and I try to write my own solutions now as much as possible with google and you amazing people here as help but yeah. I really appreciate your support.
Was This Post Helpful? 0
  • +
  • -

#4 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Using Finder To Find Text In RichTextBox Is My Code Sound?

Posted 22 May 2012 - 01:11 AM

Quote

I wouldn't completely dismiss the code in RexPad because it tries very hard to separate UI code from logic code
Small but quite important typo.

You should separate the program logic from the UI and Rexpad is a good example of this.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,492
  • Joined: 05-May 12

Re: Using Finder To Find Text In RichTextBox Is My Code Sound?

Posted 22 May 2012 - 02:29 AM

Sorry about that typo... You are correct that I wouldn't dismiss the RexPad code base because it does the right thing is separating UI from code logic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1