2012-08-06 66 views
1

我試圖在用戶單擊「提交」作爲驗證正確輸入的方法時在消息框中顯示3個值。在消息框中顯示錶單數據

3個值來自comboBoxes的形式:年齡,身高,體重。

在我目前的設置中,該框只會在頂部邊框中顯示「age:」的實際數值。

如何獲取3個組合框數據項出現在具有適當標題的消息框內?

像這樣: 年齡:27身高 :62 重量:180

數據存儲在變量age_Num.Text,height_Num.Text和weight_Num.Text

MessageBox.Show("Age:", age_Num.Text); //just shows "Age:". Value is in titlebar of mb 

回答

2

您必須將這些值連接成單個字符串。試試這個,使用StringBuilder

 StringBuilder MessageText = new StringBuilder(); 
     MessageText.AppendLine(string.Format("Age: {0}", age_Num.Text)); 
     MessageText.AppendLine(string.Format("Height: {0}", height_Num.Text)); 
     MessageText.AppendLine(string.Format("Weight: {0}", weight_Num.Text)); 
     MessageBox.Show(MessageText.ToString()); 
+0

有沒有辦法顯示每個值後的回車項目,所以它不是一個巨大的字符串? – 2012-08-06 17:22:01

+0

@ J.C.Morris是的,使用'MessageText.AppendLine',它會發揮魔力。但是,應該將內部字符串修改爲'string.Format(「... {0}」,...)'。得到它了? – 2012-08-06 17:23:11

1

你可以直接使用組合的值,

MessageBox.Show("Sometext 1:" + cbo1.SelectedValue.ToString() + " Sometext 2:" + cbo2.SelectedValue.ToString() + " Sometext 3:" + cbo3.SelectedValue.ToString()); 

或者你已經有了變量。

MessageBox.Show("Age: " + age_num.Text + " Height: " + age_num.Text + " Sometext 3: " + weight_Num.Text); 
4

ComboBox的情況下,你可以通過ComboBox.SelectedText財產選定的文本。

要從多個值構建字符串,您可以使用String.Format()

string age = age_num.SelectedText; 
string height = height_Num.SelectedText; 
string weight = weight_Num.SelectedText 
string text = String.Format(
        "Age: {0}, Height: {1}, Weight: {2}", age, height, weight); 
MessageBox.Show(text); 
1

如果您不喜歡上述任何一種,只需根據需要創建一個對話框窗體,使用靜態功能以模態方式顯示它。

+0

嗯我從來沒有想過這個!好主意。 – 2012-08-06 17:42:13