C#从文本框中获取字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10696941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:51:48  来源:igfitidea点击:

C# get string from textbox

c#winformsforms

提问by CMA

I am just a noob in C#, and I've got this question to ask you.

我只是一个 C# 菜鸟,我有这个问题要问你。

I have here a form that asks for login details. It has two textfields:

我这里有一个要求登录详细信息的表格。它有两个文本字段:

  1. Username
  2. Password
  1. 用户名
  2. 密码

What I want is to get the strings entered in that textfields.

我想要的是获取在该文本字段中输入的字符串。

I am not yet familiar with the methods in C#..(in java, getString method is used). What could be the "equivalent" method here in C#?

我对C#中的方法还不熟悉..(在java中,使用的是getString方法)。C# 中的“等效”方法是什么?

采纳答案by Nikhil Agrawal

In C#, unlike java we do not have to use any method. TextBox property Textis used to get or set its text.

在 C# 中,与 java 不同,我们不必使用任何方法。TextBox 属性Text用于获取或设置其文本。

Get

得到

string username = txtusername.Text;
string password = txtpassword.Text;

Set

txtusername.Text = "my_username";
txtpassword.Text = "12345";

回答by Fredrik M?rk

The TextBoxcontrol has a Textproperty that you can use to get (or set) the text of the textbox.

TextBox控件具有Text可用于获取(或设置)文本框文本的属性。

回答by farzin parsa

I show you this with an example:

我用一个例子向你展示这一点:

string userName= textBox1.text;

and then use it as you wish

然后随心所欲地使用它

回答by Suraj

When using MVC, try using ViewBag. The best way to take input from textbox and displaying in View.

使用MVC时,尝试使用ViewBag。从文本框中获取输入并在视图中显示的最佳方式。

回答by Suraj

if in string:

如果在字符串中:

string yourVar = yourTextBoxname.Text;

if in numbers:

如果是数字:

int yourVar = int.Parse(yourTextBoxname.Text);

回答by abdul rehman

to get value of textbox

获取文本框的值

   string username = TextBox1.Text;
   string password = TextBox2.Text;

to set value of textbox

设置文本框的值

  TextBox1.Text = "my_username";
   TextBox2.Text = "12345";