2012-07-24 103 views
1

我在Visual C#.Net中創建一個工具。該工具的算法是檢查括號之前/之後的所有空間,併爲找到的錯誤創建錯誤消息。 例如:輸入爲(文本) 由於檢測到括號之前和之後的空間,所以會引發錯誤。
如果發現錯誤,代碼將在listview1.items()中添加錯誤。C#.Net:如何使我的listview項目可點擊

爲了使我的問題更清楚你這裏是我的代碼:

private void button1_Click(object sender, EventArgs e) 
     { 
      int error_counter = 0; 
      listView1.Items.Clear(); 

      //requirement 8c 
      //check for a space in open and close parenthesis 
      Regex test = new Regex(@"\(\s.+\s\)|\[\s.+\s\]|\{\s.+\s\}", RegexOptions.IgnoreCase); 
      MatchCollection matchlist = test.Matches(richTextbox1.Text); 
      if (matchlist.Count > 0) 
      { 
       for (int i = 0; i < matchlist.Count; i++) 
       { 
        Match firstMatch = matchlist[i]; 
        string firstMatch_string = firstMatch.ToString(); 
        string[] errors = new string[matchlist.Count]; 
        errors[i] = "Ommit Space between a bracket"; 
        listView1.Items.Add(errors[i]); 
        error_counter++; 
       } 
      } 
     } 

     private void listView1_ItemActivate(object sender, EventArgs e) 
     { 
      if (listView1.SelectedItems.Count > 0) 
      { 
       ListViewItem item = listView1.SelectedItems[0]; 
       MessageBox.Show(item.ToString()); 
      } 
     } 

我尋找的是我所有的ListView1的的項目可點擊和點擊是由用戶作出後工具將突出顯示richtextbox1中發現的錯誤。

感謝您的幫助!

+0

你可能想用jQuery來研究客戶端腳本來達到這個目的。 – ianaldo21 2012-07-24 09:54:34

+0

你的意思是我不能用C#創建那種類型的東西? – neo 2012-07-24 09:57:17

+0

不是關於你的問題,大多數情況下你應該使用靜態方法Regex.Matches(除非你知道與實例方法有什麼不同並且正在進行優化)。 – 2012-07-24 10:10:58

回答

0

正如有人已經告訴過你的,使用Match類的Index和Length屬性。這是一個簡單的例子,實現了一個奇怪的文本框選擇策略。但它有效地證明了這個概念:

public partial class Form1 : Form 
{ 
    List<Error> errors = new List<Error>(); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     errors = new List<Error>(); 
     listView1.Items.Clear();          
     foreach(Match m in Regex.Matches(richTextBox1.Text, @"(\(\s+|\s+\)|\[\s+|\s+\]|\{\s+|\s+\})", RegexOptions.IgnoreCase)) 
     {       
      //you may decide to differentiate the msg according to the specific problem 
      Error error = new Error(m, "Ommit Space between a bracket"); 
      this.errors.Add(error); 
      listView1.Items.Add(error.msg);     
     }    
    } 

    private void listView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (listView1.SelectedIndices.Count > 0) 
     { 
      Error error = errors[listView1.SelectedIndices[0]]; 
      Select(richTextBox1, error); 
     } 
    } 

    private static void Select(RichTextBox rtb, Error e) { 
     string o = rtb.Text; 
     rtb.Clear(); 
     for (int i = 0; i < o.Length; i++) 
     { 
      if (i >= e.index && i <= e.index + e.length) 
      { 
       rtb.SelectionColor = Color.White; 
       rtb.SelectionBackColor = Color.Red; 
      } 
      else 
      { 
       rtb.SelectionColor = Color.Black; 
       rtb.SelectionBackColor = Color.White; 
      } 
      rtb.AppendText(o[i].ToString()); 
     } 
    }    
} 

public class Error 
{ 

    public int index; 
    public int length; 
    public string value; 
    public string msg; 

    public Error(Match m, string msg) 
    { 
     this.index = m.Index; 
     this.length = m.Length; 
     this.value = m.Value; 
     this.msg = msg; 
    } 
} 
+0

這可能是我正在尋找的答案。我會嘗試這一個,並會更新你們,如果我找到了正確的答案,謝謝! – neo 2012-07-24 12:00:42

+0

我已經解決了我的項目問題!超級非常感謝!真的很大的幫助!迭戈再次感謝! – neo 2012-07-24 13:13:18

+0

這裏唯一的問題是,如果richTextbox1中的字符串輸入有許多行,我的意思是如果輸入太多,它會在您點擊列表視圖中的錯誤時變慢,因爲它搜索的字符太多。我怎樣才能減少這種事情?在這種情況下,感謝 – neo 2012-07-25 05:24:50

0

匹配對象(如firstMatch)在這裏有兩個有用的屬性:Index and Length

他們給你在原始文本中的位置和問題匹配的長度。 有了你的知識,你只需要在richTextBox中實現高亮!

+0

如果您在使listviewitem可點擊並檢測點擊項時遇到問題,請首先指定您正在使用的技術:WinForms? WPF? – 2012-07-24 10:29:49

+0

我正在使用WinForms。感謝你的回答! – neo 2012-07-24 11:59:16