none
C# RichTextBox Wortfarbe ändern RRS feed

  • Frage

  • Hallo, 

    ich möchte gerne eine Art minimalistischen Batch-Editor schreiben, ich habe verschiedene Schlüsselwörter, die in der Box farbig dargestellt werden sollen. Es werden nicht nur alle Keywords farbig dargestellt auch der Inhalt anderer Worte, und die Cursorposition ist immer hinten am Text, bzw. man kann nicht mitten im Text bearbeiten. Wie kann ich dieses Problem am besten lösen?

    	public partial class executeBatch : Form
    	{
    		internal string[] keywords = { "@echo", "off", "on", "color", "assoc", "attrib", "break", "bootcfg", "call", "cd", "chdir", "chkdsk", "cls", "cmd", "comp", "Copy", "date", "del", "dir", "wmic", "xcopy", "vol", "help", "ver", "start", "rmdir", "rem", "ren", "rename", "replace", "rd", "path", "cd..", "prompt", "move", "if", "goto", "for", "exit", "format", "diskpart" };
    		internal string[] apps = { "calc", "shutdown", "taskmgr" };
    		internal string[] special = { "kill" };
    
    		
    
    		private void richTextBox1_TextChanged(object sender, EventArgs e)
    		{
    			foreach (string s in keywords)
    			{
    				int index = richTextBox1.Text.IndexOf(s);
    				if ( index> -1)
    				{
    					richTextBox1.Select(index, s.Length);
    					richTextBox1.SelectionColor = Color.Orange;
    					
    				}
    			}
    			richTextBox1.DeselectAll();
    	
    			richTextBox1.SelectionColor = Color.Black;
    
    			foreach (string s in apps)
    			{
    				int index = richTextBox1.Text.IndexOf(s);
    				if (index > -1)
    				{
    					richTextBox1.Select(index, s.Length);
    					richTextBox1.SelectionColor = Color.Green;
    
    				}
    			}
    			richTextBox1.DeselectAll();
    			
    			richTextBox1.SelectionColor = Color.Black;
    
    			foreach (string s in special)
    			{
    				int index = richTextBox1.Text.IndexOf(s);
    				if (index > -1)
    				{
    					richTextBox1.Select(index, s.Length);
    					richTextBox1.SelectionColor = Color.Red;
    
    				}
    			}
    			richTextBox1.DeselectAll();
    		richTextBox1.Select(richTextBox1.Text.Length, 0);
    			richTextBox1.SelectionColor = Color.Black;
    			
    		}
    	
    			}
    
    

    Mittwoch, 8. Oktober 2014 16:45

Antworten

Alle Antworten