richTextBox problem 2
-
This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?
int posTxt = 0;
private void button2_Click(object sender, EventArgs e)
{string searchText = textBox2.Text.ToLower(); string alltxt = richTextBox1.Text.ToLower(); if (alltxt.Contains(searchText)) { richTextBox1.Text = alltxt; while (posTxt != -1) { posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase); if (richTextBox1.SelectedText == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase); } } posTxt = 0; }
-
This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?
int posTxt = 0;
private void button2_Click(object sender, EventArgs e)
{string searchText = textBox2.Text.ToLower(); string alltxt = richTextBox1.Text.ToLower(); if (alltxt.Contains(searchText)) { richTextBox1.Text = alltxt; while (posTxt != -1) { posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase); if (richTextBox1.SelectedText == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase); } } posTxt = 0; }
You could use the lower-case versions of the search string and the text to locate the match (or matches); then remove whatever exists at the match position (you know position and length), and then insert the new text with its original casing. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
You could use the lower-case versions of the search string and the text to locate the match (or matches); then remove whatever exists at the match position (you know position and length), and then insert the new text with its original casing. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3this was my original thought but I have problems putting in practice. The search string do not have a lower-case version. http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextboxfinds.aspx[^] I must unfortunately bring down all text ToLower. This is my problem-how to avoid that? Can you give me an example, please? thanks.
-
this was my original thought but I have problems putting in practice. The search string do not have a lower-case version. http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextboxfinds.aspx[^] I must unfortunately bring down all text ToLower. This is my problem-how to avoid that? Can you give me an example, please? thanks.
please read my reply again: you search using the lower-case versions, only to find the location. you modify using the actual text, not the lower-case one (the positions are the same for both!) :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
please read my reply again: you search using the lower-case versions, only to find the location. you modify using the actual text, not the lower-case one (the positions are the same for both!) :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3this is what i have done so far... but with the same result. The text in richTextBox1 is changing to lower and I dont see from where.
int posTxt = 0;
private void button2_Click(object sender, EventArgs e)
{
string searchText = textBox2.Text.ToLower();
//string alltxt = richTextBox1.Text.ToLower();
string alltxt = richTextBox1.Text.ToLower();
RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;if (alltxt.Contains(searchText)) { //richTextBox1.Text = alltxt; rtb.Text = alltxt; while (posTxt != -1) { posTxt = rtb.Find(searchText, posTxt, RichTextBoxFinds.MatchCase); if (rtb.SelectedText == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = rtb.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase); } } posTxt = 0; }
-
this is what i have done so far... but with the same result. The text in richTextBox1 is changing to lower and I dont see from where.
int posTxt = 0;
private void button2_Click(object sender, EventArgs e)
{
string searchText = textBox2.Text.ToLower();
//string alltxt = richTextBox1.Text.ToLower();
string alltxt = richTextBox1.Text.ToLower();
RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;if (alltxt.Contains(searchText)) { //richTextBox1.Text = alltxt; rtb.Text = alltxt; while (posTxt != -1) { posTxt = rtb.Find(searchText, posTxt, RichTextBoxFinds.MatchCase); if (rtb.SelectedText == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = rtb.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase); } } posTxt = 0; }
_q12_ wrote:
The text in richTextBox1 is changing to lower and I dont see from where.
_q12_ wrote:
string alltxt = richTextBox1.Text.ToLower();
_q12_ wrote:
rtb.Text = alltxt;
X|
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
this is what i have done so far... but with the same result. The text in richTextBox1 is changing to lower and I dont see from where.
int posTxt = 0;
private void button2_Click(object sender, EventArgs e)
{
string searchText = textBox2.Text.ToLower();
//string alltxt = richTextBox1.Text.ToLower();
string alltxt = richTextBox1.Text.ToLower();
RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;if (alltxt.Contains(searchText)) { //richTextBox1.Text = alltxt; rtb.Text = alltxt; while (posTxt != -1) { posTxt = rtb.Find(searchText, posTxt, RichTextBoxFinds.MatchCase); if (rtb.SelectedText == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = rtb.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase); } } posTxt = 0; }
As Luc has already suggested: Do not assign alltxt to rtb.Text nor richTextBox1.Text; Actually, remove rtb variable, you don't need it; Use alltxt.IndexOf method instead of rtb.Find method to search for the text you want. The indexes will be the same. Use these indexes to make the changes on your rich text box control. The lines:
RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;
are useless becouse RichTextBox is a reference type, so your local rtb variable and your richTextBox1 will both be pointing to the same object.
-
This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?
int posTxt = 0;
private void button2_Click(object sender, EventArgs e)
{string searchText = textBox2.Text.ToLower(); string alltxt = richTextBox1.Text.ToLower(); if (alltxt.Contains(searchText)) { richTextBox1.Text = alltxt; while (posTxt != -1) { posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase); if (richTextBox1.SelectedText == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase); } } posTxt = 0; }
richTextBox1.Text = alltxt;
Why are you doing that? You are explicitly setting the text to the lower case version in that line. The only difference between the text property and alltxt is that you ran ToLower on alltxt! Also, RichTextBox.Find, if you want to use it, already searches case-insensitively by default. Did you read the documentation for RichTextBoxFinds[^]? Passing RichTextBoxFinds.None will cause the search to be case-insensitive – you explicitly asked for a case sensitive one, for some reason.
-
_q12_ wrote:
The text in richTextBox1 is changing to lower and I dont see from where.
_q12_ wrote:
string alltxt = richTextBox1.Text.ToLower();
_q12_ wrote:
rtb.Text = alltxt;
X|
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
yes... but... only rtb.Text = richTextBox1.Text; not inverse.
RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;
meaning: richTextBox1.Text is not equal with rtb.Text. :confused: anyway... its not about the new rtb.
_q12_ wrote:
rtb = richTextBox1;
WRONG! you have copied the reference, so both rtb and richTextBox1 point to the same object. the new RichTextBox you also created is dead right away, it is never used (it also is not needed!). BTW: Eric already told you all that. I suggest you choose, buy and thoroughly study an introductory book on C#. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?
int posTxt = 0;
private void button2_Click(object sender, EventArgs e)
{string searchText = textBox2.Text.ToLower(); string alltxt = richTextBox1.Text.ToLower(); if (alltxt.Contains(searchText)) { richTextBox1.Text = alltxt; while (posTxt != -1) { posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase); if (richTextBox1.SelectedText == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase); } } posTxt = 0; }
Problems Solved: Sincerely, I did changed RichTextBoxFinds.None before posting here on forum, but for some strange reason, it did not work back then... Now it works fine. I have another problem...see it beneath code. Here is the functional code:
int posTxt = 0; string tempText = "";
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text = tempText;//somewhere in the page load : tempText = richTextBox1.Text;string searchText = textBox2.Text.ToLower(); string alltxt = richTextBox1.Text.ToLower(); if (alltxt.Contains(searchText)) { //problems at start if (posTxt <= 0) { richTextBox1.Text = richTextBox1.Text.Insert(0, " "); posTxt = 1; } //colorization at found text while (posTxt > 0) { posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.None); if (richTextBox1.SelectedText.ToLower() == searchText) { richTextBox1.SelectionLength = searchText.Length; richTextBox1.SelectionColor = Color.Red; } posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.None); } } posTxt = 0; }
But i have a little problem now... If I load a *.rtf file with formatting text and search a word in that richTextBox1 that show that file, the text is re-formatting, loosing its original format. How to maintain its original format, and select as usual(with red marking)?
-
As Luc has already suggested: Do not assign alltxt to rtb.Text nor richTextBox1.Text; Actually, remove rtb variable, you don't need it; Use alltxt.IndexOf method instead of rtb.Find method to search for the text you want. The indexes will be the same. Use these indexes to make the changes on your rich text box control. The lines:
RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;
are useless becouse RichTextBox is a reference type, so your local rtb variable and your richTextBox1 will both be pointing to the same object.
-
yes... but... only rtb.Text = richTextBox1.Text; not inverse.
RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;
meaning: richTextBox1.Text is not equal with rtb.Text. :confused: anyway... its not about the new rtb.
As told earlier by Luc, rtb and richtextBox1 refer(point to the same object). Think of it this way: rtb and richtextbox1 are just some names that help you deal the the real thing. A guick fix for what you are trying to do with this line of code would be:
RichTextBox rtb = new RichTextBox(); rtb**.Text** = richTextBox1**.Text**;
Now rtb does not point to the same object but it just copied the text from richtexbox1. Clearer than that I don't know how to explain.
All the best, Dan
modified on Friday, May 27, 2011 3:35 PM