0

我的程序中有以下代码。弹出一个消息框以显示字段表单的内容。点击 OK 后,会弹出另一个询问用户信息是否正确。我想将两者结合起来,弹出消息框,显示表单内容并询问信息是否正确,并带有一个是/否按钮。我已经尝试将两者结合起来无济于事。我相信我缺少一个句法概念。有任何想法吗?

   //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

4 回答 4

3
 //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);

Something like this? Of course this last text could also be appended to the StringBuilder.

If you only want Yes/No buttons then change MessageBoxButtons.YesNoCancel to MessageBoxButtons.YesNo.

Finally check the result like this:

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;
}

of course you have to add/remove the cancel option depending on if you include the button or not.

于 2012-08-07T12:04:20.490 回答
1

我假设您想在信息之后提出问题,在这种情况下,只需将您的问题附加到字符串生成器,然后再传递给相关的 MessageBox arg,就像您在第一个 MessageBox 中一样:

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);
于 2012-08-07T12:05:19.323 回答
1

做就是了:

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

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

于 2012-08-07T12:06:05.623 回答
1

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

    DialogResult result1 = MessageBox.Show(MessageText.ToString() + 
"\nInformation is correct?",
        "Double Check Form Information",
        MessageBoxButtons.YesNoCancel,
        MessageBoxIcon.Question);
于 2012-08-07T12:06:21.023 回答