0

How would you check multiple RichTextboxes containing multiple lines for unique and or duplicate lines. I've figured out how to loop through the richTextboxes and get the data, but I'm struggling on the logic to see if it's unique. My apologies if code is as clear as mud.

List<string> DistinctItems = new List<string>();
List<string> DupilcatedItems = new List<string>();
List<string> FirstItemsList = new List<string>();
List<string> CompareItemsList = new List<string>();
int ElementIndex = 0;

foreach (RichTextBox c in tableLayoutPanel1.Controls)
{
    if (c.Text != null)
    {
        FirstItemsList.Add(c.Text.Replace("\n", Environment.NewLine).ToString());

        if (CompareItemsList.Count == 0)
        {
            //Have to add the first batch
            foreach (string str in FirstItemsList)
            {
                txtMixerTextBox.AppendText(str);
                txtDistinctItems.AppendText(str);
                DistinctItems.Add(str);
                ElementIndex++;
            }

            CompareItemsList.Add(c.Text.Replace("\n", Environment.NewLine).ToString());

            if (CompareItemsList.Count() > 0)
            {
                //OK we've gone through the first set
                foreach (string s in CompareItemsList)
                {
                    if (DistinctItems.Contains(s))
                    {
                        //It's a duplicate see if it's in the duplicate list
                        if (DupilcatedItems.Contains(s))
                        {
                            //OK it's in the list we don't have to add it
                            //See if it's in the textbox
                            if (!txtDuplicateItems.Text.Contains(s))
                            {
                                //OK it's not in the textbox let's add it
                                txtDuplicateItems.AppendText(s);
                            }
                        }
                    }
                    else
                    {
                        //It's not there so add it
                        DupilcatedItems.Add(s);
                        //now see if it's in the Distinct Textbox
                        if (!txtDistinctItems.Text.Contains(s))
                        {
                            //add it
                            txtDistinctItems.AppendText(s);
                        }
                        txtMixerTextBox.AppendText(s);
                    }
                }
            }
        }
    }
}
SuperOli
  • 1,784
  • 1
  • 11
  • 23
Ron H
  • 75
  • 1
  • 5

2 Answers2

0

Use String.Split. For example:

foreach (RichTextBox c in tableLayoutPanel1.Controls)
{
    if (!c.Text.IsNullOrWhiteSpace)
    {
        string[] lines = c.Text.Split('\n');
        string[] uniqueLines = GetUniqueLines(lines);//Some line-uniqueness checking algorithm
        c.Text = String.Join('\n',uniqueLines)
    }
}
SuperOli
  • 1,784
  • 1
  • 11
  • 23
Pharap
  • 3,826
  • 5
  • 37
  • 51
  • .IsNullOrWhiteSpace only good for .Net 4.0 or higher. Sorry I didn't mention that I was using 3.5 (my mistake). I think this might do it. [link]http://stackoverflow.com/questions/47752/remove-duplicates-from-a-listt-in-c-sharp – Ron H Jul 09 '13 at 21:57
  • That would have been good to know beforehand. If you are going with a solution from here, be sure to flag your question for removal for being a duplicate or at least show more clearly how you got your solution so that future people who read this can see how your problem was solved. – Pharap Jul 09 '13 at 22:03
  • Found it after I posted the question. I will endeavour to include the version of .Net I'm using in the future. Thanks for the heads up. As this is the first question I've asked, I'm not very clear on the nuance(s). I think that the List.Distinct().ToList() mentioned in the article above might be helpful to someone with the same or similar question. It hasn't yet solved my problem, but seemed like a good idea. Linked answer refers to List not specifically RichTextBoxes. Someone else may be adverse to using List (for whatever reason). – Ron H Jul 09 '13 at 22:22
0

This is what I did to get the results I was after. Looping through the RichTextboxes as noted above, I wrote the list to a file, stripped out the blank lines (where they came from I haven't the foggiest), read the file into a new list and then got the distinct list from there. I think the blank lines may have been messing me up, or it might have been the fact that I was looping through the strings in the list (again) thus giving myself duplicates. I'll likely get hammered for it, but it worked for me.

        List<string> SortingList = new List<string>();

    using (StreamReader r = new   StreamReader("DistinctItemsNoBlankLines.txt"))
                        {
                        string line;
                        while ((line = r.ReadLine()) != null)
                            {
                                SortingList.Add(line);
                            }

                        }



   List<string>DistinctSortingList = SortingList.Distinct().ToList();

        foreach (string str in DistinctSortingList)
        {

        int index = 0;
            while ( index < DistinctSortingList.Count() -1)
            {
            if (DistinctSortingList[index] == DistinctSortingList[index + 1])
                DistinctSortingList.RemoveAt(index);
            else
                index++;
            }
        }
         txtDistinctItems.Lines = DistinctSortingList.ToArray();
Ron H
  • 75
  • 1
  • 5