简体   繁体   English

新手-如何在C#中提示用户输入

[英]Newbie - How to prompt for user input in C#

I'm in the second week of C#, enjoying it. 我在C#的第二周,很开心。 I am doing an extra credit quiz, have one issue I can't seem to figure out. 我正在做一个额外的信用测验,有一个我似乎无法弄清的问题。

The user is supposed to enter their name into a textbox, when submit is pressed, it will display the name in a messagebox. 用户应该在文本框中输入其名称,当按下Submit时,它将在消息框中显示该名称。 Easy. 简单。 But my problem is, I am supposed to have it display a message box if the input field is blank, displaying "Please enter your name!" 但是我的问题是,如果输入字段为空,我应该让它显示一个消息框,显示“请输入您的名字!” I cannot find it anywhere in the book, or online. 我在书中或网上找不到它。 All I have (related to this button) so far is below, but I know it isn't right. 到目前为止,我所拥有的(与该按钮相关的信息)都在下面,但我知道这是不对的。 As it is, it simply opens both windows, lol. 实际上,它只是打开两个窗口,大声笑。 Any help for a newbie? 对新手有帮助吗? lol 大声笑

    private void Submit_Click(object sender, EventArgs e)
    {
        if (textBoxName.Text == "")
        {
            MessageBox.Show("Please Enter Your Name");
        }
        {
            MessageBox.Show("Hello, " + this.textBoxName.Text);
        }
    }

Just use an else: 只需使用其他:

   private void Submit_Click(object sender, EventArgs e)
    {
        if (textBoxName.Text == "")
        {
            MessageBox.Show("Please Enter Your Name");
        }
        else
        {
            MessageBox.Show("Hello, " + this.textBoxName.Text);
        }
    }

By the way, for single statement if/else block you don't need curly braces. 顺便说一下,对于单条语句if / else块,您不需要花括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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