在消息框中显示表单数据

时间:2012-08-06 17:06:37

标签: c# combobox messagebox

当用户点击“提交”作为验证输入正确的方法时,我试图在消息框中显示3个值。

3个值来自组合框,其形式为:年龄,身高,体重。

在我目前的设置中,该框仅显示“年龄:”,顶部边框中的实际数值。

如何让3个comboBox数据项显示在带有相应标题的消息框中?

像这样:  年龄: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

4 个答案:

答案 0 :(得分: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 :(得分: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());

答案 2 :(得分:1)

您可以直接使用combo的值,

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

答案 3 :(得分:1)

如果你不喜欢上述任何一种,只需创建一个你想要的对话框表格,静态功能就可以模态显示。

相关问题