0

我需要验证RichTextBox我在按下按钮时动态创建的 x 数量的 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;
    }
}
4

1 回答 1

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();
}
于 2013-04-23T16:42:55.187 回答