0

I am in need of validating x amount of RichTextBoxes I created dynamically at the press of a button. I need to make sure there isn't a single RTB empty before copying the contents to the clipboard and calling the next form.

I tried adding a boolean variable but this just gets skipped if an empty RTB is somewhere in the middle.

Here's the current code I have. Any help is greatly appreciated.

List<RichTextBox> rtbs = scrlPanel.Children.OfType<RichTextBox>().ToList();
List<TextBlock> texts = scrlPanel.Children.OfType<TextBlock>().ToList();
StringBuilder raTemplate = new StringBuilder();
//bool flag = true; // True as in It is empty

foreach (RichTextBox rtb in scrlPanel.Children.OfType<RichTextBox>())
{
    TextRange txtRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
    if (txtRange.Text.Trim() == string.Empty)
    {
        MessageBox.Show("Empty fields.");
        break;
    }
    else
    {
        foreach (TextBlock txtb in texts)
        {
            //RichTextBox rtb = rtbs[texts.IndexOf(txtb)];
            //TextRange txtRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
            raTemplate.Append(txtb.Text + " " + "::" + Environment.NewLine + txtRange.Text.Trim() + Environment.NewLine);
        }
        Clipboard.SetText(raTemplate.ToString());
        RA_Email ra = new RA_Email();
        ra.raEmail();
        //flag = true;
    }
}
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
hectormtnezg
  • 132
  • 5
  • 18
  • If the third RTB is empty, for example, do you want to skip the rest or just skip the third one? Or do you also want to undo the first two? – keyboardP Apr 23 '13 at 16:48
  • Pretty much your first post (code) fixed my issue :) but yea, I want to discard everything as soon as the code encounters an empty RTB be it at the start, at the middle or at the end. – hectormtnezg Apr 23 '13 at 16:57
  • I removed that post because I realised that `txtRange` won't exist outside the first loop. Reposted an updated version that should work with the `txtRange` variable :) – keyboardP Apr 23 '13 at 17:07

1 Answers1

1

The copying aspect can be moved outside the loop to ensure that it only starts if all the RichTextBoxes are empty.

bool doCopy = true;
foreach (RichTextBox rtb in scrlPanel.Children.OfType<RichTextBox>())
{
     TextRange txtRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
     if (txtRange.Text.Trim() == string.Empty)
     {
         MessageBox.Show("Empty fields.");
         doCopy = false;
         break;
     }
}

if(doCopy)
{
     foreach (TextBlock txtb in texts)
     {
         //RichTextBox rtb = rtbs[texts.IndexOf(txtb)];
         //TextRange txtRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
         raTemplate.Append(txtb.Text + " " + "::" + Environment.NewLine + txtRange.Text.Trim() + Environment.NewLine);
     }

     Clipboard.SetText(raTemplate.ToString());
     RA_Email ra = new RA_Email();
     ra.raEmail();
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • I bet you're going to remove this one too :) This latest code you posted actually copies each of the TextBlock + RTB for every RTB in scrlPanel. Basically, it copies a a very large appended string. Your first code however, worked flawlessly, I just added a second txtRange (although it's not best programming practices) :) – hectormtnezg Apr 23 '13 at 17:16
  • @hectormtnezg - Ah okay. In that case, you could loop through the RTB collection twice. Once to ensure that none are empty and the next to actually do the copying stuff. (Updated answer. Hope it works!). If not, I'll repost the first one :) – keyboardP Apr 23 '13 at 17:23
  • Thank you very much for your help keyboardP, I suggest you re-post the first one though so I can mark it as the answer and up you a point because this one does the same thing as the previous one and the txtRange also needs to be re-declared. :) – hectormtnezg Apr 23 '13 at 17:29
  • You're welcome! If I remember correctly, this was the first suggestion :D – keyboardP Apr 23 '13 at 17:33