Advertisement
MBrendecke

GiddleTestOptimized

Apr 2nd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         private Regex R;
  11.  
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.             InitRegex();
  16.         }
  17.  
  18.         private void InitRegex()
  19.         {
  20.             R = new Regex(String.Join("|", new[]{
  21.                 "IF", "THEN", "ELSE", "ENDIF"
  22.             }));
  23.         }
  24.  
  25.         private void rtbText_TextChanged(object sender, EventArgs e)
  26.         {
  27.             int index = rtbText.SelectionStart;
  28.             rtbText.SuspendLayout();
  29.  
  30.             foreach (Match m in R.Matches(rtbText.Text))
  31.             {
  32.                 rtbText.SelectionStart = m.Index;
  33.                 rtbText.SelectionLength = m.Length;
  34.                 rtbText.SelectionColor = Color.Blue;
  35.                 rtbText.SelectionStart = index;
  36.             }
  37.  
  38.             rtbText.SelectionColor = Color.Black;
  39.             rtbText.ResumeLayout(true);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement