CMA
CMA

Reputation: 2808

C# get string from textbox

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

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

  1. Username
  2. Password

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#?

Upvotes: 22

Views: 242904

Answers (6)

abdul rehman
abdul rehman

Reputation: 37

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";

Upvotes: 0

user8590116
user8590116

Reputation:

if in string:

string yourVar = yourTextBoxname.Text;

if in numbers:

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

Upvotes: 2

Suraj
Suraj

Reputation: 417

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

Upvotes: 4

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

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

Get

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

Set

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

Upvotes: 46

farzin parsa
farzin parsa

Reputation: 547

I show you this with an example:

string userName= textBox1.text;

and then use it as you wish

Upvotes: 3

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

The TextBox control has a Text property that you can use to get (or set) the text of the textbox.

Upvotes: 4

Related Questions