2012-07-24 101 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