Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > .NET Frameworks (точка нет фреймворки) > C# (си шарп)
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 07.02.2012, 00:17   #1
mike_ua
Новичок
Джуниор
 
Регистрация: 06.02.2012
Сообщений: 1
Сообщение RichTextBox C# поиск

Доброго времени суток! Нужно осуществить поиск введенного в TextBox слова по ранее загруженому тексту из *.rtf в RichTexBox. У кого какие есть предложения.
mike_ua вне форума Ответить с цитированием
Старый 07.02.2012, 13:03   #2
Hollander
Участник клуба
 
Аватар для Hollander
 
Регистрация: 03.05.2007
Сообщений: 1,189
По умолчанию

Код:
/// <summary>
/// method for searching for specified text in a RichTextBox
/// </summary>
/// <param name="text">text we're looking for</param>
/// <param name="matchCase">match case or not?</param>
/// <param name="rtb">RichTextBox we're searching for</param>
public static void Find(string text, bool matchCase, System.Windows.Forms.RichTextBox rtb)
{
    try
    {
        //variable to hold the start position (start of the found text)
        int startPos;

        //what kind of search are we doing
        StringComparison type;

        //determine if it's a match case search or not
        type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

        //look for the search text
        startPos = rtb.Text.IndexOf(text, type);

        //check the position
        if (!(startPos > 0))
        {
            //text doesn't exist so let the user know
            MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            return;
        }
        else
        {
            //Yureka! Select the found text
            rtb.Select(startPos, text.Length);
            //scroll to the found text
            rtb.ScrollToCaret();
            //add focus so the highlighting shows up
            rtb.Focus();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Search Error");
    }
}
Hollander вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
RichTextBox msemenikhin Visual C++ 1 02.02.2012 16:51
отступы в RichTextBox DarkMage Общие вопросы .NET 1 07.05.2011 00:11
richtextbox Рыжик чик чик Microsoft Office Word 1 04.05.2011 11:30
richTextBox Cpluser Общие вопросы .NET 3 28.04.2009 02:39
richtextbox sergei64_89 Общие вопросы .NET 1 08.12.2008 13:53