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

时间:2022-11-04 16:03:25

I would like to read through a richtextbox, have each paragraph in a row and each line in the paragraph into its column.

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

Paragraphs are separated by an empty line

段落用空行分隔

An example of the information in the richtextbox below:

以下richtextbox中的信息示例:

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

I'd like to have 3 columns. Then each first 2 lines in the paragraph be in the first two columns, then all the remaining lines into the 3rd column.

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

All i have right now is have it read each 3 columns into the listview as below, and this clearly does not achieve what i want.

我现在所拥有的就是让它在列表视图中读取每个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 个解决方案

#1


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

Call it like below

如下所示

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

#1


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

Call it like below

如下所示

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