Reputation: 132
I am in need of validating x amount of RichTextBox
es 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;
}
}
Upvotes: 0
Views: 239
Reputation: 69372
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();
}
Upvotes: 1