PDA

View Full Version : سوال: رنگی کردن دو زیررشته یکسان در یک رشته



قله بلند
شنبه 31 خرداد 1393, 16:30 عصر
سلام دوستان
من برای رنگی کردن زیر رشته "رنگ" در رشته "این رنگ و این رنگ نشان می دهند" از روش زیر استفاده کردم چون Find فقط اولین زیر رشته را پیدا می کتد و دومین زیر رشته را پیدا نمی کند. آیا راه دیگه ای هم وجود داره؟



richTextBox_Label2.SelectionStart = 4;
richTextBox_Label2.SelectionLength = 3;
richTextBox_Label2.SelectionColor = Color.Magenta;
richTextBox_Label2.SelectionStart = 15;
richTextBox_Label2.SelectionLength = 3;
richTextBox_Label2.SelectionColor = Color.Violet;

Amir4317
شنبه 31 خرداد 1393, 18:30 عصر
string keyword = txtSearch.Text.Trim();//getting given searching string
int startPosition = 0; //initializing starting position to search
int endPosition = 0;
int endArticle = txtText.Text.Length;//finding total number of character into the article
for (int i = 0; i < endArticle; i = startPosition)//creating a loop until ending the article
{
if (i == -1) //if the loop goes to end of the article then stop
{
break;
}
startPosition = txtText.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);//using find method get the begining position when find the searching string
if (startPosition >= 0) //if match the string //if don't match the string then it return -1
{
count++;//conunting the number of occuerence or match the search string
txtText.SelectionBackColor = Color.Orange;//coloring the matching string in the article
endPosition = txtSearch.Text.Length;
startPosition = startPosition + endPosition;//place the starting position at the next word of previously matching string to continue searching.
}
}
if (count == 0)//if the givn search string don't match at any time
{
MessageBox.Show("No Match Found!!!");
}
MessageBox.Show("تعداد : "+count+" یافت شد"); //show the number of occurence into the text box




منبع (http://barnamenevisan.org/Articles/Article2260.html)

Amir4317
شنبه 31 خرداد 1393, 18:32 عصر
تکه کد زیر خیلی بدرد بخوره




string keyword = txtSearch.Text.Trim();//getting given searching string
int startPosition = 0; //initializing starting position to search
int endPosition = 0;
int endArticle = txtText.Text.Length;//finding total number of character into the article
for (int i = 0; i < endArticle; i = startPosition)//creating a loop until ending the article
{
if (i == -1) //if the loop goes to end of the article then stop
{
break;
}
startPosition = txtText.Find(keyword, startPosition, endArticle, RichTextBoxFinds.WholeWord);//using find method get the begining position when find the searching string
if (startPosition >= 0) //if match the string //if don't match the string then it return -1
{
count++;//conunting the number of occuerence or match the search string
txtText.SelectionBackColor = Color.Orange;//coloring the matching string in the article
endPosition = txtSearch.Text.Length;
startPosition = startPosition + endPosition;//place the starting position at the next word of previously matching string to continue searching.
}

}
if (count == 0)//if the givn search string don't match at any time
{
MessageBox.Show("No Match Found!!!");
}
MessageBox.Show("تعداد : "+count+" یافت شد"); //show the number of occurence into the text box

//Amir4317



منبع (http://barnamenevisan.org/Articles/Article2260.html)

golestan1
شنبه 31 خرداد 1393, 18:37 عصر
یک تکست باکس روی فرم قرار بده و رشته مورد نظر برای جستجو رو وارد کن (رنگ)
حالا چندین سطر رشته مورد نظر رو در richTextBox وارد کن(این رنگ و این رنگ نشان می دهند)
و برای کلیک دکمه نیز کد زیر رو بزار
this.richTextBox1.SelectAll();
this.richTextBox1.SelectionColor = Color.Black;
this.richTextBox1.Select(0, 0);
string s = this.textBox1.Text;
int num = 0;
for (int i = 0; i < richTextBox1.Text.Length - s.Length + 1; ++i)
{
if (richTextBox1.Text.Substring(i, s.Length) == s)
{
this.richTextBox1.SelectionStart = i;
this.richTextBox1.SelectionLength = s.Length;
this.richTextBox1.SelectionColor = Color.Red;
num++;
}
}
this.richTextBox1.Select(richTextBox1.Text.Length, 0);

قله بلند
شنبه 31 خرداد 1393, 23:35 عصر
سلام
ممنون از دو بزرگوار

اون وقت یک سوال دارم. وقتی زیر رشته مورد نظر، رنگی می شه و بخواهیم از همین richtextbox برای یافتن زیر رشته ای دیگر استفاده کنیم و همین عملیات رو انجام بدیم چه کار کنیم که همه چیز به وضع اولش برگرده تا عملیات بعدی، رنگ عملیات قبلی رو به خودش نگیره. آخه برای من اینجوری می شه و عملیات بعدی، رنگ های عملیات قبلی رو می گیرند و نتیجه رنگی درستی رو نشون نمی دهند.