簡體   English   中英

將兩個C#消息框的內容合並為一個

[英]Combining the contents of two C# message boxes into one

我的程序中下面顯示了以下代碼。 彈出一個消息框以顯示字段表單的內容。 點擊確定后,彈出另一個詢問用戶信息是否正確。 我想將兩者結合起來,在彈出的消息框中顯示表單內容並詢問信息是否正確,並帶有“是/否”按鈕。 我嘗試將兩者結合都無濟於事。 我相信我缺少語法概念。 有任何想法嗎?

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

當然,您必須根據是否包含按鈕來添加/刪除取消選項。

我假設您想在信息后問這個問題,在這種情況下,可以像在第一個MessageBox中那樣,在將您的問題附加到stringbuilder之前,再傳遞給相關的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);

做就是了:

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

第二個參數(MessageBoxButtons.YesNoCancel)是確定它將具有哪些按鈕的(在這種情況下,是,否和取消)

您為什么不簡單地串聯字符串?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM