将两个C#消息框的内容合并为一个

时间:2012-08-07 12:01:01

标签: c# messagebox

我的程序中显示如下所示的代码。弹出一个消息框以显示字段表单的内容。点击OK后,会弹出另一个询问用户信息是否正确的信息。我想把弹出消息框的两个结合起来,显示表单内容并询问信息是否正确,并带有YES / NO按钮。我试过将两者结合起来无济于事。我相信我错过了一个句法概念。有什么想法吗?

   //shows contents of form fields
  StringBuilder MessageText = new StringBuilder();
        MessageText.AppendLine(string.Format("Coil#: {0}", coil_Num.Text));
        MessageText.AppendLine(string.Format("Location: {0}", location_box.Text));
        MessageText.AppendLine(string.Format("Sub-Area: {0}", sub_area_box.Text));
        MessageText.AppendLine(string.Format("Row: {0}", row_Num.Text));
        MessageBox.Show(MessageText.ToString());

  //asks if info is correct, with a YES/NO button and question mark
  DialogResult result1 = MessageBox.Show("Information is correct?",
        "Double Check Form Information",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);

4 个答案:

答案 0 :(得分:2)

 //shows contents of form fields
  StringBuilder MessageText = new StringBuilder();
        MessageText.AppendLine(string.Format("Coil#: {0}", coil_Num.Text));
        MessageText.AppendLine(string.Format("Location: {0}", location_box.Text));
        MessageText.AppendLine(string.Format("Sub-Area: {0}", sub_area_box.Text));
        MessageText.AppendLine(string.Format("Row: {0}", row_Num.Text));
        MessageText.AppendLine();
        MessageText.AppendLine();

  //asks if info is correct, with a YES/NO button and question mark
  DialogResult result1 = MessageBox.Show(MessageText.ToString() + "Information is correct?",
        "Double Check Form Information",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);

这样的东西?当然,最后一个文本也可以附加到StringBuilder。

如果您只想要是/否按钮,请将MessageBoxButtons.YesNoCancel更改为MessageBoxButtons.YesNo

最后检查结果如下:

switch (result1)
{
   case DialogResult.Yes:
      // ... Do stuff if Yes is choosen
      break;

   case DialogResult.No:
      // ... Do stuff if No is choosen
      break;

   case DialogResult.Cancel:
      // ... Do stuff if Cancel is choosen
      break;
}

当然,您必须添加/删除取消选项,具体取决于您是否包含该按钮。

答案 1 :(得分:1)

我假设您想在信息之后提出问题,在这种情况下,您需要将问题附加到stringbuilder,然后再传递给第一个MessageBox中的相关MessageBox arg:

StringBuilder MessageText = new StringBuilder();
MessageText.AppendLine(string.Format("Coil#: {0}", coil_Num.Text));
MessageText.AppendLine(string.Format("Location: {0}", location_box.Text));
MessageText.AppendLine(string.Format("Sub-Area: {0}", sub_area_box.Text));
MessageText.AppendLine(string.Format("Row: {0}", row_Num.Text));
MessageText.AppendLine("Is this information correct?");

DialogResult result1 = MessageBox.Show(MessageText.ToString(),
    "Double Check Form Information",
    MessageBoxButtons.YesNoCancel,
    MessageBoxIcon.Question);

答案 2 :(得分:1)

只是做:

MessageBox.Show(MessageText.ToString(), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

第二个参数(MessageBoxButtons.YesNoCancel)决定它将具有哪些按钮(在这种情况下,是,否和取消)

答案 3 :(得分:1)

为什么不简单地连接字符串?

    DialogResult result1 = MessageBox.Show(MessageText.ToString() + 
"\nInformation is correct?",
        "Double Check Form Information",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);