2013-07-11 55 views
0

IM上的代碼編輯器工作,我想出了這組驗證碼:避免掃描EntireRichtextbox突出

public class Test2 : Form { 

    RichTextBox m_rtb = null; 

    public static void Main() { 
    Application.Run(new Test2()); 
    } 

    public Test2() { 
    Text = "Test2"; 
    ClientSize = new Size(400, 400); 
    m_rtb = new RichTextBox(); 
    m_rtb.Multiline = true; 
    m_rtb.WordWrap = false; 
    m_rtb.AcceptsTab = true; 
     m_rtb.ScrollBars = RichTextBoxScrollBars.ForcedBoth; 
    m_rtb.Dock = DockStyle.Fill; 
    m_rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular); 
    m_rtb.SelectionColor = Color.Black; 
    Controls.Add(m_rtb); 
    Parse(); 
    m_rtb.TextChanged += new EventHandler(this.TextChangedEvent); 
    } 

    void Parse() { 
    String inputLanguage = 
     "// Comment.\n" + 
     "using System;\n" + "\n" + 
     "public class Stuff : Form { \n" + 
     " public static void Main(String args) {\n" + 
     " }\n" + 
     "}\n" ; 

    // Foreach line in input, 
    // identify key words and format them when adding to the rich text box. 
    Regex r = new Regex("\\n"); 
    String [] lines = r.Split(inputLanguage); 
    foreach (string l in lines) { 
     ParseLine(l); 
    }  
    } 

    void ParseLine(string line) { 
    Regex r = new Regex("([ \\t{}();])"); 
    String [] tokens = r.Split(line); 

    foreach (string token in tokens) { 
     // Set the token's default color and font. 
     m_rtb.SelectionColor = Color.Black; 
     m_rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular); 

     // Check for a comment. 
     if (token == "//" || token.StartsWith("//")) { 
     // Find the start of the comment and then extract the whole comment. 
     int index = line.IndexOf("//"); 
     string comment = line.Substring(index, line.Length - index); 
     m_rtb.SelectionColor = Color.LightGreen; 
     m_rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular); 
     m_rtb.SelectedText = comment; 
     break; 
     } 

     // Check whether the token is a keyword. 
     String [] keywords = { "public", "void", "using", "static", "class" }; 
     for (int i = 0; i < keywords.Length; i++) { 
     if (keywords[i] == token) { 
      // Apply alternative color and font to highlight keyword. 
      m_rtb.SelectionColor = Color.Blue; 
      m_rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold); 
      break; 
     } 
     } 
     m_rtb.SelectedText = token; 
    }  
    m_rtb.SelectedText = "\n"; 
    } 

    private void TextChangedEvent(object sender, EventArgs e) { 
    // Calculate the starting position of the current line. 
    int start = 0, end = 0; 
    for (start = m_rtb.SelectionStart - 1; start > 0; start--) { 
     if (m_rtb.Text[start] == '\n') { start++; break; } 
    } 

    // Calculate the end position of the current line. 
    for (end = m_rtb.SelectionStart; end < m_rtb.Text.Length; end++) { 
     if (m_rtb.Text[end] == '\n') break; 
    } 

    // Extract the current line that is being edited. 
    String line = m_rtb.Text.Substring(start, end - start); 

    // Backup the users current selection point. 
    int selectionStart = m_rtb.SelectionStart; 
    int selectionLength = m_rtb.SelectionLength; 

    // Split the line into tokens. 
    Regex r = new Regex("([ \\t{}();])"); 
    string [] tokens = r.Split(line); 
    int index = start; 
    foreach (string token in tokens) { 

     // Set the token's default color and font. 
     m_rtb.SelectionStart = index; 
     m_rtb.SelectionLength = token.Length; 
     m_rtb.SelectionColor = Color.Black; 
     m_rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular); 

     // Check for a comment. 
     if (token == "//" || token.StartsWith("//")) { 
     // Find the start of the comment and then extract the whole comment. 
     int length = line.Length - (index - start); 
     string commentText = m_rtb.Text.Substring(index, length); 
     m_rtb.SelectionStart = index; 
     m_rtb.SelectionLength = length; 
     m_rtb.SelectionColor = Color.LightGreen; 
     m_rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular); 
     break; 
     } 

     // Check whether the token is a keyword. 
     String [] keywords = { "public", "void", "using", "static", "class" }; 
     for (int i = 0; i < keywords.Length; i++) { 
     if (keywords[i] == token) { 
      // Apply alternative color and font to highlight keyword.   
      m_rtb.SelectionColor = Color.Blue; 
      m_rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold); 
      break; 
     } 
     } 
     index += token.Length; 
    } 
    // Restore the users current selection point.  
    m_rtb.SelectionStart = selectionStart; 
    m_rtb.SelectionLength = selectionLength; 
    } 
} 

的問題是,每次我按下空格鍵或輸入完整的代碼編輯器不斷掃描像上搜索的內容要突出我覺得有點討厭...

,所以我只是想詢問一下這個......可能的解決方案,以避免整體的RichTextBox的高亮像掃描下一個高亮的。

感謝很多提前的幫助!更多的權力!

回答

0

我在你最近的問題回答了這一點,但如果有人在讀這篇文章,沒有找到它,我將它張貼在這裏(因爲這是專門關於性能):

您可以使用一對夫婦的事情,以改善性能:

1)你可以得到用戶通過從當前選擇獲取文本範圍編輯線。我建議使用WPF的richtextbox,因爲它包含更多的功能,並且有可用於獲取當前行的有用的TextPointer類。好像你正在使用不過的WinForms,所以這可以用這幾行代碼(加的一些瑣碎的代碼)來完成:

int start_index = RTB.GetFirstCharIndexOfCurrentLine(); 
int line = RTB.GetLineFromCharIndex(index); 
int last_index = RTB.GetFirstCharIndexFromLine(line+1); 
RTB.Select(start_index, last_index); 

然後,您可以用當前的選擇工作。

2)如果你不希望它如此頻繁更新,您可以創建計算從最後一次編輯延時定時器,復位定時器如果其他編輯之前計時器經過製作。