Advertisement
MBrendecke

RTB Highlight

Apr 1st, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Text.RegularExpressions;
  4. using System.Windows.Forms;
  5.  
  6. namespace GiddleTest1
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         public Form1()
  11.         {
  12.             InitializeComponent();
  13.         }
  14.  
  15.         private void rtbText_TextChanged(object sender, EventArgs e)
  16.         {
  17.             string[] befehle =
  18.             {
  19.                 "IF", "THEN", "ELSE", "ENDIF" // Befehle vervollständigen...
  20.             };
  21.  
  22.             string pattern = String.Join("|", befehle);
  23.  
  24.             Regex R = new Regex(pattern);
  25.             int index = rtbText.SelectionStart;
  26.             rtbText.SuspendLayout();
  27.  
  28.             foreach (Match m in R.Matches(rtbText.Text))
  29.             {
  30.                 rtbText.SelectionStart = m.Index;
  31.                 rtbText.SelectionLength = m.Length;
  32.                 rtbText.SelectionColor = Color.Blue;
  33.                 rtbText.SelectionStart = index;
  34.             }
  35.  
  36.             rtbText.SelectionColor = Color.Black;
  37.             rtbText.ResumeLayout(true);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement