简体   繁体   English

C#.Net:如何使我的listview项目可点击

[英]C#.Net : How to make my listview items clickable

I am creating a tool in Visual C#.Net. 我正在Visual C#.Net中创建一个工具。 The algorithm of the tool is to check for all space/s before/after a parenthesis and create an error message for the found errors. 该工具的算法是检查括号前后的所有空间,并为找到的错误创建错误消息。 For example: input is ( Text ) Error will be raise because space before and after the parenthesis is detected. 例如:input is(Text)由于检测到括号前后的空格,将引发错误。
If errors are found the code will add the errors in listview1.items(). 如果发现错误,则代码会将错误添加到listview1.items()中。

To make my question much clearer for you here's my code: 为了让我的问题更清楚,这是我的代码:

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());
            }
        }

What I looking for is that all of the items of my listview1 will be clickable, and after a click was made by the user the tool will highlight the error found in the richtextbox1. 我要寻找的是listview1的所有项目都是可单击的,并且用户单击后,该工具将突出显示在richtextbox1中发现的错误。

Thanks for all your help guys! 感谢您的帮助!

The Match object (like firstMatch) has two usefull properties here : Index and Length . Match对象(如firstMatch)在此处具有两个有用的属性: Index和Length

They give you the position and the length of the match in question in the original text. 他们在原始文本中为您提供了相关比赛的位置和时间。 With that in your knowledge, you just have to implement the highlight in the richTextBox ! 据此,您只需要在richTextBox中实现突出显示!

As someone already told you, use the Index and Length properties of the Match class. 正如有人已经告诉您的那样,使用Match类的Index和Length属性。 Here's a short example implementing a weird textbox selection strategy. 这是一个实现怪异的文本框选择策略的简短示例。 But it works effectively demonstrating the concept: 但这有效地证明了这一概念:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM