c#使用richtextbox中的项目填充列表视图(每行分为列,空行并转到下一行)

时间:2017-10-06 14:47:44

标签: c# listview richtextbox readline

我想阅读一个richtextbox,将每个段落连续放入段落中的每一行。

段落用空行分隔

以下richtextbox中的信息示例:

richtextbox example

我想要3列。 然后段落中的前两行在前两列中,然后所有剩余的行都在第三列中。

我现在所拥有的就是让它在列表视图中读取每个3列,如下所示,这显然无法达到我想要的效果。

for (int i = 0; i < richTextBox1.Lines.Count(); i += 3)
   {
       listView1.Items.Add(new ListViewItem(new[]{
            richTextBox1.Lines[i],
            richTextBox1.Lines[i+1], 
            richTextBox1.Lines[i+2]]}));
    }

1 个答案:

答案 0 :(得分:0)

//Add this method


  public static HashSet<Tuple<int, string, string, string>>  GetRichTextBoxSections(string[] Lines)
        {
            HashSet<Tuple<int, string, string, string>> Sections = new HashSet<Tuple<int, string, string, string>>();
            int SectionNumber = -1;
            int SectionIndexLineNumber = 0;
            bool FoundNewSectionNumber = false;
            string Col1 = string.Empty;
            string Col2 = string.Empty;
            string Col3 = string.Empty;
            int LinesCount = Lines.Length;
            for (int i = 0; i < LinesCount; i++)
            {

                string NoSpaces = System.Text.RegularExpressions.Regex.Replace(Lines[i], @"\s+", "");
                if (FoundNewSectionNumber == false)
                {
                    SectionIndexLineNumber = i;
                }



                if (FoundNewSectionNumber)
                {
                    if (string.IsNullOrWhiteSpace(NoSpaces) || string.IsNullOrEmpty(NoSpaces) & FoundNewSectionNumber == true)
                    {

                        Sections.Add(new Tuple<int, string, string, string>(SectionNumber, Col1, Col2, Col3));
                        SectionNumber = -1;
                        FoundNewSectionNumber = false;
                        Col1 = string.Empty;
                        Col2 = string.Empty;
                        Col3 = string.Empty;
                        SectionIndexLineNumber = 0;
                        continue;

                    }
                    else
                    {
                        switch (i - SectionIndexLineNumber)
                        {
                            case 1:
                                Col1 = Lines[i];
                                break;
                            case 2:
                                Col2 = Lines[i];
                                break;
                            default:
                                Col3 += Lines[i];
                                break;
                        }
                    }
                }
                if (FoundNewSectionNumber == false)
                {
                    FoundNewSectionNumber = int.TryParse(NoSpaces, out SectionNumber);
                }
                if (i == (LinesCount - 1))
                {
                    Sections.Add(new Tuple<int, string, string, string>(SectionNumber, Col1, Col2, Col3));
                }
            }
            return Sections;
        }

请在下面调用

foreach(Tuple<int,string,string,string> SectionData in GetRichTextBoxSections(richTextBox1.Lines))
            {

 listView1.Items.Add(new ListViewItem(new[]{
//Item1 is the section index if you need it
            SectionData.Item2,
            SectionData.Item3, 
            SectionData.Item4}));
}