1

我正在 Visual C#.Net 中创建一个工具。该工具的算法是检查括号之前/之后的所有空格,并为发现的错误创建错误消息。例如: input is ( Text ) 将引发错误,因为检测到括号前后的空格。
如果发现错误,代码将在 listview1.items() 中添加错误。

为了让我的问题更清楚,这是我的代码:

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 中发现的错误。

感谢您的所有帮助!

4

2 回答 2

0

Match 对象(如 firstMatch)在这里有两个有用的属性:索引和长度

它们为您提供原始文本中相关匹配的位置和长度。根据您的知识,您只需要在richTextBox 中实现高亮!

于 2012-07-24T10:14:20.377 回答
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;
    }
}
于 2012-07-24T11:39:31.330 回答