简体   繁体   中英

Message Box to Show Array and Text

What I am trying to achieve is for the program to record whats in Textbox1 and spit it back and say welcome "name". This is the code I have currently got. thank you!

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        string name = textBox1.Text;

        if (textBox1.Text == "Ryan" && textBox2.Text == "password")
        {
            MessageBox.Show("Welcome" + name);
        }

    }
}
}

由于没有定义name变量,因此必须使用+ concatenate字符串并使用textBox1.Text作为名称。

 MessageBox.Show("Welcome" + textBox1.Text);

在“消息框”行中为+更改&& (和运算符)。

MessageBox.Show("Welcome" + name);  // suppose "name" is the string you want to aggregate. 

Maybe you want to do this?:

private void button1_Click(object sender, EventArgs e)
{

    if (textBox1.Text == "Ryan" && textBox2.Text == "password")
    {
        MessageBox.Show("Welcome" + textBox1.Text);            

    }
}

TextBox1.Text and TextBox2.Text contains the value of the names.

In c# you have to use + for concatenation instead of &&

MessageBox.Show("Welcome" && name);

should be

MessageBox.Show("Welcome " + name);

I would update to

 MessageBox.Show("Welcome " + name);

Please note I've included a space after the "welcome", otherwise it will read WelcomeRyan instead of Welcome Ryan

You cannot concatenate with && -- use string.Format instead:

MessageBox.Show(string.Format("Welcome {0}", name));

With your edited code, you cannot use name in this event -- you'd need to use textBox1.Text. Or you can define your name variable as global. Depends on your needs.

I assume the "name" you want to output is the what is in the .Text property of textBox1 - then you want to change your code like:

    if (textBox1.Text == "Ryan" && textBox2.Text == "password")
    {
        MessageBox.Show("Welcome" + textBox1.Text);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM