سؤال

وكيف أرسم في أحمر في كل مرة أقابل حرف "A" في RichTextBox؟

هل كانت مفيدة؟

المحلول

وجرب هذا:

static void HighlightPhrase(RichTextBox box, string phrase, Color color) {
  int pos = box.SelectionStart;
  string s = box.Text;
  for (int ix = 0; ; ) {
    int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
    if (jx < 0) break;
    box.SelectionStart = jx;
    box.SelectionLength = phrase.Length;
    box.SelectionColor = color;
    ix = jx + 1;
  }
  box.SelectionStart = pos;
  box.SelectionLength = 0;
}

...

private void button1_Click(object sender, EventArgs e) {
  richTextBox1.Text = "Aardvarks are strange animals";
  HighlightPhrase(richTextBox1, "a", Color.Red);
}

نصائح أخرى

وهنا مقتطف من بلدي فئة مجمع للقيام بهذه المهمة:

    private delegate void AddMessageCallback(string message, Color color);

    public void AddMessage(string message)
    {
        Color color = Color.Empty;

        string searchedString = message.ToLowerInvariant();

        if (searchedString.Contains("failed")
            || searchedString.Contains("error")
            || searchedString.Contains("warning"))
        {
            color = Color.Red;
        }
        else if (searchedString.Contains("success"))
        {
            color = Color.Green;
        }

        AddMessage(message, color);
    }

    public void AddMessage(string message, Color color)
    {
        if (_richTextBox.InvokeRequired)
        {
            AddMessageCallback cb = new AddMessageCallback(AddMessageInternal);
            _richTextBox.BeginInvoke(cb, message, color);
        }
        else
        {
            AddMessageInternal(message, color);
        }
    }

    private void AddMessageInternal(string message, Color color)
    {
        string formattedMessage = String.Format("{0:G}   {1}{2}", DateTime.Now, message, Environment.NewLine);

        if (color != Color.Empty)
        {
            _richTextBox.SelectionColor = color;
        }
        _richTextBox.SelectedText = formattedMessage;

        _richTextBox.SelectionStart = _richTextBox.Text.Length;
        _richTextBox.ScrollToCaret();
    }

والآن يمكنك الاتصال مع AddMessage("The command failed") للحصول عليه تسليط الضوء تلقائيا باللون الأحمر. أو يمكنك الاتصال مع AddMessage("Just a special message", Color.Purple) لتحديد لون خاص (مثل مفيدة داخل كتل الصيد لتحديد لون معين، بغض النظر عن محتوى الرسالة)

وهذا لن ينجح بينما كنت تكتب إذا كان هذا هو ما كنت أبحث عنه، ولكن أنا استخدم هذا لتسليط الضوء على سلاسل فرعية:

Function Highlight(ByVal Search_Str As Object, ByVal InputTxt As String, ByVal StartTag As String, ByVal EndTag As String) As String
    Highlight = Regex.Replace(InputTxt, "(" & Regex.Escape(Search_Str) & ")", StartTag & "$1" & EndTag, RegexOptions.IgnoreCase)
End Function

ووالذي يطلق عليه بهذه الطريقة:

Highlight("A", "Color All my A's red", [span class=highlight]', '[/span]')

وأين الطبقة "تسليط الضوء" لديها أي لون الترميز / التنسيق الذي تريد:

.highlight {text-decoration: none;color:black;background:red;}

وراجع للشغل: كنت بحاجة إلى تغيير تلك الأقواس المربعة إلى تلك الزاوية ... أنها لن تأتي من خلال عندما كنت كتبته لهم ...

وهذا هو C # رمز للإجابة EJ برينان:

public string Highlight(object Search_Str, string InputTxt, string StartTag, string EndTag) 
{
    return Regex.Replace(InputTxt, "(" + Regex.Escape(Search_Str) + ")", StartTag + "$1" + EndTag, RegexOptions.IgnoreCase);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top