如何获取用户输入并在 C# 中打印它

本文关键字:打印 输入 用户 何获取 获取 | 更新日期: 2023-09-27 18:37:05

我是编程新手,正在玩C#应用程序...
我的代码有什么问题?

private void button1_Click(object sender, EventArgs e)
{
    String text = textBox1.Text;
    text = Console.ReadLine();
    Console.WriteLine(text);
    MessageBox.Show("hi"+ text);
}

我只是想获取用户输入并在 c# 应用程序中打印它。

如何获取用户输入并在 C# 中打印它

问题是您使用的是控制台应用程序还是用户界面应用程序(如WPF,WinForms)?

如果您使用的是控制台应用程序,则应:

string input = Console.ReadLine(); //Getting an input from the user.
Console.WriteLine(input); //Print the input.

如果您使用的是用户界面应用程序(如 WPF),则应:

string input = textBox1.Text; //You should have a textbox in the view with the name textBox1
MessageBox.Show("hi" + input); //Shows a message box.