1

こんにちはマスター/プログラマーです。
Split() を使用しようとしています。使用後、RTB の入力 == 私のポイントかどうかを確認してから、RTB のフォントの色を変更します。この例のように。

RTBの入力:Chelsea is my favorite football club. I like playing football
私のポイント:football.

次に、入力を分割し、各インデックスで分割の結果を確認します。
最後に、例を見つけました:arr[4] and [9] = football

では、RTB画面で
「チェルシーが好きfootball。クラブで遊ぶのが好き」のようにフォントの色を変えるにはfootball

この私のコード例:

 ArrayList arrInput = new ArrayList();
 //if the input Chelsea is my favorite football club. I like playing football
 string allInput = rtbInput.Text; 

 string[] splitString = allInput.Split(new char[] { ' ', '\t', ',', '.'});

 foreach (string s in splitString)
 {
     if (s.Trim() != "")
     {      
          int selectionStart = s.ToLower().IndexOf("football");

          if (selectionStart != -1)
          {
              rtbInput.SelectionStart = selectionStart;
              rtbInput.SelectionLength = ("football").Length;
              rtbInput.SelectionColor = Color.Red;
          }
 }
 //What Next?? Im confused. We know that football on arrInput[4].ToString() and [9]
 //How to change font color on RTB screen when the input == football
4

1 に答える 1

0

プロパティを選択footballして設定する必要がありますSelectionColor

foreach (string s in splitString)
{
    string trimmedS = s.Trim();

    if (trimmedS != "")
    {
        int selectionStart = -1;
        if (trimmedS.ToLower == "football") // Find the string you want to color
            selectionStart = allInput.Count;

        allInput.Add(s);

        if (selectionStart != -1)
        {
            rtbInput.SelectionStart = selectionStart; // Select that string on your RTB
            rtbInput.SelectionLength = trimmedS.Length;
            rtbInput.SelectionColor = myCustomColor; // Set your color here
        }
    }
}

編集:
代替

// create your allInput first

int selectionStart = allInput.ToLower().IndexOf("football");
if (selectionStart != -1)
{
    rtbInput.SelectionStart = selectionStart;
    rtbInput.SelectionLength = ("football").Length;
    rtbInput.SelectionColor = myCustomColor;
}

色が残るかどうかわからないので、この2番目の答えをお勧めします。RichTextBox.Text

Edit2:
さらに別の選択肢

// create your allInput first

Regex regex = new Regex("football", RegexOptions.IgnoreCase); // using System.Text.RegularExpressions;

foreach (Match match in regex.Matches(allInput))
{
    rtbInput.SelectionStart = match.Index;
    rtbInput.SelectionLength = match.Length;
    rtbInput.SelectionColor = myCustomColor;
}
于 2013-03-11T11:47:07.377 に答える