代码之家  ›  专栏  ›  技术社区  ›  user770022

如何显示包含某些标签内容的MessageBox消息?

  •  4
  • user770022  · 技术社区  · 15 年前

    我想让messagebox.show说(“对不起,您输了,号码是”,“label1.Text”);但是在它显示label.Text的地方,我想让它显示生成的号码。

    private void button1_Click(object sender, EventArgs e)
        {
            RandomNumber(0,99);
            button2.Enabled = true ;
            button1.Enabled = false;
            if (textBox1.Text == label1.Text)
                MessageBox.Show("Winner");
            if (textBox1.Text != label1.Text)
                MessageBox.Show("Sorry - You Lose, The number is{0}",label1.Text);            
        }
    
    4 回复  |  直到 14 年前
        1
  •  1
  •   mint    15 年前
    MessageBox.Show("Sorry - You Lose, The number is " + label1.Text);
    
        2
  •  2
  •   Jakub Konecki    15 年前
    MessageBox.Show(string.Format("Sorry - You Lose, The number is {0}",label1.Text));
    
        3
  •  2
  •   Gabriel Magana    15 年前
    MessageBox.Show(string.Format("Sorry - You Lose, The number is{0}",label1.Text)); 
    
        4
  •  2
  •   Muad'Dib    15 年前

    将String.Format添加到对MessageBox.Show…的调用中。。。。这是你的代码,显示了我的意思。

    private void button1_Click(object sender, EventArgs e)
        {
            RandomNumber(0,99);
            button2.Enabled = true ;
            button1.Enabled = false;
            if (textBox1.Text == label1.Text)
                MessageBox.Show("Winner");
            if (textBox1.Text != label1.Text)
                MessageBox.Show( String.Format("Sorry - You Lose, The number is{0}",label1.Text));            
        }