C# richTextbox的 字型格式顏色等等

來源:互聯網
上載者:User

設定的是第一個字元的顏色,這時richtextBox的rtf中記錄下位置0的顏色,重設text時,在rtf中的位置從位置0開始,因此顏色還是Color.Red,第三次也應該同樣如此
richTextBox1.Text = "123";
richTextBox1.Select(0, 1);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.Clear();//清除文本,包括樣式
//或richTextBox1.Text="";
richTextBox1.Text = "abc";
richTextBox1.Text = "efg";
這樣重設後的文本顏色就會是黑色.

【關鍵字著色】

public partial class RichTextBox : Form
    ...{
        public RichTextBox()
        ...{
             InitializeComponent();
         }

        private void tSql_TextChanged(object sender, EventArgs e) //文字框改變事件
        ...{
            int index = this.tSql.SelectionStart;    //記錄修改的位置
            this.tSql.SelectAll();
            this.tSql.SelectionColor = Color.Black;

            string[] keystr =...{ "select ", "from ", "where ", " and ", " or ", " order ", " by ", " desc ", " when ", " case ",
   " then ", " end ", " on ", " in ", " is ", " else ", " left ", " join ", " not ", " null " };
            for (int i = 0; i < keystr.Length; i++)
                this.getbunch(keystr[i], this.tSql.Text);

            this.tSql.Select(index, 0);     //返回修改的位置
            this.tSql.SelectionColor = Color.Black;

         }
        public int getbunch(string p, string s) //給關鍵字上色
        ...{
            int cnt = 0; int M = p.Length; int N = s.Length;
            char[] ss = s.ToCharArray(), pp = p.ToCharArray();
            if (M > N) return 0;
            for (int i = 0; i < N - M + 1; i++)
            ...{
                int j;
                for (j = 0; j < M; j++)
                ...{
                    if (ss[i + j] != pp[j]) break;
                 }
                if (j == p.Length)
                ...{
                    this.tSql.Select(i, p.Length);
                    this.tSql.SelectionColor = Color.Blue;
                     cnt++;
                 }
             }
            return cnt;

         }

     }

【繪製顏色提議】

最好的做法是繼承RichTextBox,重載新類的Paint方法。

並且在設定SelectionLength的時候,禁止控制項的重繪過程,這樣才不會出現被文法高亮的文本有一個突然選中的過程。

以下2個方法將會對你解決這一問題有很大的協助.
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
private const int WM_SETREDRAW = 0xB;

//停止控制項的重繪
private void BeginPaint()
{
SendMessage(yourRichTextBox.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
}

//允許控制項重繪.
private void EndPaint()
{
SendMessage(yourRichTextBox.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
yourRichTextBox.Refresh();
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.