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

New Topic/Question
Reply



MultiQuote





|