آموزش ساخت برنامه search در RichTextBox و هایلایت کردن متن جستجو شده در #C
سلام …
در این مطلب ، یاد میگیرید که یک برنامه ای بسازین که کاربر یک فایل متنی رو باز کنه و متن مورد نظرش رو سرچ کنه و به صورت هایلایت شده بهش نمایش داده بشه.در پایان این آموزش می توانید برنامه ای شبیه برنامه ی بالا بسازید.
کارکرد برنامه به این صورت است که ابتدا کاربر متنی که میخواهد جستجو کند را در تکست باکس وارد میکند و سپس با کلیک کردن روی دکمه ی جستجو متن مورد نظر خودش اگر درون RichTextBox بود ، هایلایت شده می شود.
ابتدا کنترل هایی را که در برنامه تان میخواهید ، مثل دکمه و تکست باکس و RichTextBox و … را قرار دهید.سپس متغیر های start و indexOfSearch را به صورت سراسری تعریف کنید.
int start = 0;
int indexOfSearchText = 0;
int indexOfSearchText = 0;
سپس در رویداد کلیک دکمه ی جستجو کدهای زیر را وارد کنید:
int startindex = 0;
if(txtSearch.Text.Length > 0)
startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);
// If string was found in the RichTextBox, highlight it
if (startindex >= 0)
{
// Set the highlight color as red
rtb.SelectionColor = Color.Red;
// Find the end index. End Index = number of characters in textbox
int endindex = txtSearch.Text.Length;
// Highlight the search string
rtb.Select(startindex, endindex);
// mark the start position after the position of
// last search string
start = startindex + endindex;
}
if(txtSearch.Text.Length > 0)
startindex = FindMyText(txtSearch.Text.Trim(), start, rtb.Text.Length);
// If string was found in the RichTextBox, highlight it
if (startindex >= 0)
{
// Set the highlight color as red
rtb.SelectionColor = Color.Red;
// Find the end index. End Index = number of characters in textbox
int endindex = txtSearch.Text.Length;
// Highlight the search string
rtb.Select(startindex, endindex);
// mark the start position after the position of
// last search string
start = startindex + endindex;
}
سپس تابع FindMyText را به صورت زیر تعریف کنید:
public int FindMyText(string txtToSearch, int searchStart, int searchEnd)
{
// Unselect the previously searched string
if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
{
rtb.Undo();
}
// Set the return value to -1 by default.
int retVal = -1;
// A valid starting index should be specified.
// if indexOfSearchText = -1, the end of search
{
// Unselect the previously searched string
if (searchStart > 0 && searchEnd > 0 && indexOfSearchText >= 0)
{
rtb.Undo();
}
// Set the return value to -1 by default.
int retVal = -1;
// A valid starting index should be specified.
// if indexOfSearchText = -1, the end of search
// بقیه ی کدها مخصوص کاربران وی آی پی
if (searchStart >= 0 && indexOfSearchText >=0)
{
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
{
// Find the position of search string in RichTextBox
indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
// Determine whether the text was found in richTextBox1.
if (indexOfSearchText != -1)
{
// Return the index to the specified search text.
retVal = indexOfSearchText;
}
}
}
return retVal;
}
if (searchStart >= 0 && indexOfSearchText >=0)
{
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
{
// Find the position of search string in RichTextBox
indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
// Determine whether the text was found in richTextBox1.
if (indexOfSearchText != -1)
{
// Return the index to the specified search text.
retVal = indexOfSearchText;
}
}
}
return retVal;
}