.NET Framework Bookmark and Share   
 index > Regular Expressions > Having Problems With Regex For Rich Text Box
 

Having Problems With Regex For Rich Text Box

Ok, for some reason it is not telling me if it can't find the text entered in the dialog box was found or not in the rich text box. If it was found it needs to selected the word in the rich text box. For some reason it is not telling me anything or showing me any thing in short terms.

Here is a code snipit of the find dialog.

Code Block

public partial class FindDialog : Form
{
private Regex regex;
private Match match;
private bool isFirstFind = true;
//....


private Regex GetRegExpression()
{
Regex result;
String regExString;

// Get what the user entered
regExString = txtFind.Text;


// Is match case checkbox checked?
if (chkMatchCase.Checked)
{
result = new Regex(regExString);
}
else
{
result = new Regex(regExString, RegexOptions.IgnoreCase);
}

return result;
}

private void FindText()
{

RichTextBox newRtb2 = new RichTextBox();
window mMain = new window();
newRtb2 = mMain.tabs.SelectedTab.Controls[0] as RichTextBox;

// Is this the first time find is called?
// Then make instances of RegEx and Match
if (isFirstFind)
{
regex = GetRegExpression();
match = regex.Match(txtFind.Text);
isFirstFind = false;
}
else
{
// match.NextMatch() is also ok, except in Replace
// In replace as text is changing, it is necessary to
// find again
//match = match.NextMatch();
match = regex.Match(newRtb2.Text, match.Index + 1);
}

// found a match?
if (match.Success)
{
// then select it
newRtb2.SelectionStart = match.Index;
newRtb2.SelectionLength = match.Length;
}
else // didn't find? bad luck.
{
MessageBox.Show(String.Format("Cannot find '{0}'. ", txtFind.Text),
"Doom3 Scripter 2.0", MessageBoxButtons.OK, MessageBoxIcon.Information);
isFirstFind = true;
}
}

private void matchCaseCheckBox_CheckedChanged(object sender, EventArgs e)
{
isFirstFind = true;
}

private void searchTextBox_TextChanged(object sender, EventArgs e)
{
isFirstFind = true;
}


// Click event handler of find button
private void findButton_Click(object sender, EventArgs e)
{
FindText();
}


Sorry if it seems much to fix, but I am in head scratcher with this and its the only object for me to accomplish.


ajm113

ajm113,

reproducing a minimal scenario I could get your code to work without problems. The only difference is that in my case the RichTextBox resides on the same form and I fill it manually.

Theonly suspect at this point is this part of your code that I did not port in my tests:

...

RichTextBox newRtb2 = new RichTextBox();
window mMain = new window();
newRtb2 = mMain.tabs.SelectedTab.Controls[0] as RichTextBox;
...

You seem to be creating an object of type window and extract the RichTextBox control from there. Without the code I can't tell if that is correct. Could you provide the code (or a reasonable description)?

Thanks

--mc

Mario Cossi

Hello.

I made an application using similar method. I wonder if it helps you...

Code Block

private function findSearch(findRegex:Regex){
var findMatch:Match;

findMatch=findRegex.Match(rtfText.Text,rtfText.SelectionStart+(findRegex.RightToLeft?rtfText.SelectionLength:0));

if(findMatch.Success&&(findMatch.Index!=rtfText.SelectionStart||findMatch.Length!=rtfText.SelectionLength||(findMatch=findMatch.NextMatch(),findMatch.Success)))
rtfText.Select(findMatch.Index,findMatch.Length);
else
MessageBox.Show("not found");

}

// *This code has reverse-search feature.

//rtfText --- RichText Object

// findRegex --- RegEx Object

It's written in JScript code butessential part is same to C, I think.

にふてぃ

ajm113,

The reason why you don't get a message when the search text does not exist. In your code for FindText (comments removed):

if (isFirstFind) {
regex = GetRegExpression();
match = regex.Match(txtFind.Text);
isFirstFind = false;
}else{
match = regex.Match(newRtb2.Text, match.Index + 1);
}

For the first find, you are asking the code to look for the search text WITHIN the search text itself. This will always succeed with index 0. Simply replace with newRtb2.Text.

The second issue is the reason why you don't see anything happening. If you don't set HideSelection to false, the selection will be hidden when the RichTextBox does not have focus. This is obviously not the case when you press the search button. If you click back into the RichTextBox, the selection gets reset to the cursor position, so you don't see anything.

Either set the focus programmatically back to the newRtb2, or set HideSelection to false.

HTH

--mc

Mario Cossi
Ok, I did set HideSelection on the rich text box to false and it seemed to have no effect still. I did set the newRtb2 to focuse when the find event was called, but still...

Code Block

private void FindText()
{

RichTextBox newRtb2 = new RichTextBox();
window mMain = new window();
newRtb2 = mMain.tabs.SelectedTab.Controls[0] as RichTextBox;
newRtb2.Focus();

// Is this the first time find is called?
// Then make instances of RegEx and Match
if (isFirstFind)
{
regex = GetRegExpression();
match = regex.Match(newRtb2.Text);
isFirstFind = false;
}
else
{
// match.NextMatch() is also ok, except in Replace
// In replace as text is changing, it is necessary to
// find again
match = match.NextMatch();
match = regex.Match(newRtb2.Text, match.Index + 1);
}
//...



Sorry, if I misted something.
ajm113

ajm113,

reproducing a minimal scenario I could get your code to work without problems. The only difference is that in my case the RichTextBox resides on the same form and I fill it manually.

Theonly suspect at this point is this part of your code that I did not port in my tests:

...

RichTextBox newRtb2 = new RichTextBox();
window mMain = new window();
newRtb2 = mMain.tabs.SelectedTab.Controls[0] as RichTextBox;
...

You seem to be creating an object of type window and extract the RichTextBox control from there. Without the code I can't tell if that is correct. Could you provide the code (or a reasonable description)?

Thanks

--mc

Mario Cossi

Hello.

I made an application using similar method. I wonder if it helps you...

Code Block

private function findSearch(findRegex:Regex){
var findMatch:Match;

findMatch=findRegex.Match(rtfText.Text,rtfText.SelectionStart+(findRegex.RightToLeft?rtfText.SelectionLength:0));

if(findMatch.Success&&(findMatch.Index!=rtfText.SelectionStart||findMatch.Length!=rtfText.SelectionLength||(findMatch=findMatch.NextMatch(),findMatch.Success)))
rtfText.Select(findMatch.Index,findMatch.Length);
else
MessageBox.Show("not found");

}

// *This code has reverse-search feature.

//rtfText --- RichText Object

// findRegex --- RegEx Object

It's written in JScript code butessential part is same to C, I think.

にふてぃ

You can use google to search for other answers

Custom Search

More Threads

• Get all items, but ignore others
• Checking for blank line (*argh*)
• create component
• Complex Regular Expressin Please help
• regularexpressionvalidator question
• Help.. Regex.Replace Method...
• breaking a string based on commas and passsing the values into lists
• URL Matching
• .Net Fonts problem
• Find a perticular words value form a text document