我需要验证按下按钮动态创建的x RichTextBox
es的数量。在将内容复制到剪贴板并调用下一个表单之前,我需要确保没有一个RTB为空。
我尝试添加一个布尔变量,但如果空的RTB位于中间位置,则会跳过这个。
这是我目前的代码。非常感谢任何帮助。
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;
}
}
答案 0 :(得分:1)
可以将复制方面移到循环外部,以确保只有在所有RichTextBoxes
都为空时它才会启动。
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();
}