خب کدهای بخش رنگ خیلی راحت هستند. فقط باید یک سری کد به صورت Keyword درست کنید و بعد با Select حله.
ولی بزرگ کردن حرف اول رو فعلا نمیدونم. روش یکم کار کردم اگه درست شد حتما قرار میدم.
این کدها برای RichTextBox هستند و تو TextBox جواب نمیدن.
کدهای VB.NET:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Namespace WindowsFormsApplication1
Public Partial Class Form1
Inherits Form
Public keyWords As New Regex("abstract|as|base|bool|break|byte|case|catch |char|checked|class|const|continue|decimal|default |delegate|do|double|else|enum|event|explicit|exter n|false|finally|fixed|float|for|" + "foreach|goto|if|implicit|in|int|interface|interna l|is|lock|long|namespace|new|null|object|operator| out|override|params|private|protected|public|reado nly|ref|return|sbyte|sealed|short|sizeof|stackallo c|static|" + "string|struct|switch|this|throw|true|try|typeof|u int|ulong|unchecked|unsafe|ushort|using|virtual|vo latile|void|while|")
Public Sub New()
InitializeComponent()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1_Tick
Dim selPos As Integer = richTextBox1.SelectionStart
For Each keyWordMatch As Match In keyWords.Matches(richTextBox1.Text)
richTextBox1.[Select](keyWordMatch.Index, keyWordMatch.Length)
richTextBox1.SelectionColor = Color.Blue
richTextBox1.SelectionStart = selPos
richTextBox1.SelectionColor = Color.Black
Next
End Sub
End Class
End Namespace
کدهای #C:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Regex keyWords = new Regex("abstract|as|base|bool|break|byte|case|catch |char|checked|class|const|continue|decimal|default |delegate|do|double|else|enum|event|explicit|exter n|false|finally|fixed|float|for|" +
"foreach|goto|if|implicit|in|int|interface|interna l|is|lock|long|namespace|new|null|object|operator| out|override|params|private|protected|public|reado nly|ref|return|sbyte|sealed|short|sizeof|stackallo c|static|" +
"string|struct|switch|this|throw|true|try|typeof|u int|ulong|unchecked|unsafe|ushort|using|virtual|vo latile|void|while|");
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
int selPos = richTextBox1.SelectionStart;
foreach (Match keyWordMatch in keyWords.Matches(richTextBox1.Text))
{
richTextBox1.Select(keyWordMatch.Index, keyWordMatch.Length);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectionStart = selPos;
richTextBox1.SelectionColor = Color.Black;
}
}
}
}
لینک سایت مرجع
تشکر یادتون نره