Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Basic Textbox Control Events in C#

Author: DevASP
Download Source Code : 673_TextboxEvents.zip

In this article I will try to explain how the event occurs of Textbox control. Here I will discuss the very basic events that are very helpful while developing an Application.

To begin with this application create a windows form and drop two textbox control and listbox control. Define the following events for the textbox controls:

 

  • Enter Event.
  • GotFocus Event.
  • KeyDown Event.
  • KeyUp Event.
  • KeyPress Event.
  • Leave Event.
  • LostFocus Event.

 

To check when which occurs I hav used listbox control in which the message is displayed when the respective event will occur. Code for these Events is as follows:

 

Events for the Textbox control1:

 

void textBox1_LostFocus(object sender, System.EventArgs e)

{

   listBox1.Items.Add("LostFocus event called of TextBox1");

}

 

void textBox1_GotFocus(object sender, System.EventArgs e)

{

    listBox1.Items.Add("GotFocus event called of TextBox1");

}

 

void textBox1_Leave(object sender, System.EventArgs e)

{

    listBox1.Items.Add("Leave event called of TextBox1");

}

 

void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)

{

    listBox1.Items.Add("KeyUp event called of TextBox1");

}

 

void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

    listBox1.Items.Add("KeyPress event called of TextBox1");

}

 

void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

{

   listBox1.Items.Add("KeyDown event called of TextBox1");

}

 

void textBox1_Enter(object sender, System.EventArgs e)

{

    listBox1.Items.Add("Enter event called of TextBox1");

}

 

  • Events for the Textbox control2:

 

void textBox2_LostFocus(object sender, System.EventArgs e)

{

    listBox1.Items.Add("LostFocus event called of TextBox2");

}

 

void textBox2_GotFocus(object sender, System.EventArgs e)

{

    listBox1.Items.Add("GotFocus event called of TextBox2");

}

 

void textBox2_Leave(object sender, System.EventArgs e)

{

    listBox1.Items.Add("Leave event called of TextBox2");

}

 

void textBox2_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)

{

    listBox1.Items.Add("KeyUp event called of TextBox2");

}

 

void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)

{

    listBox1.Items.Add("KeyPress event called of TextBox2");

}

 

void textBox2_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)

{

   listBox1.Items.Add("KeyDown event called of TextBox2");

}

 

void textBox2_Enter(object sender, System.EventArgs e)

{

   listBox1.Items.Add("Enter event called of TextBox2");

}

 

Note: To check the exact working of controls use Tab button for GotFocus and LostFocus event and keyboard key stroks for KeyDown, KeyUp, KeyPress events.

Article Comments
Much appreciated!!!

Posted on 2/12/2007 3:04:47 PM by Alex C. Barberi

Need to go from One textbox to another using keypress event.
---------------------------
In VB
Private Sub txtFirst_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
txtSecond.SetFocus
End If
End Sub
-----------------------------------
What will be In C#

Posted on 4/7/2007 6:09:55 AM by Shimul Dey

events in ASP.Net code behind C# Web application

Posted on 5/17/2007 3:04:57 AM by natarajan

events in ASP.Net code behind C# Web application

Posted on 5/17/2007 3:05:07 AM by natarajan

i have seen your program i will try to solve my problem

thank u

Posted on 8/1/2007 9:41:47 AM by sanjy

want to read about Web Application TextBox Events especially Lost Focus

Posted on 8/28/2007 6:55:59 AM by Tarak Shah

Your article is very useful. Can you please email how to validate keyascii in C#.
In VB
Private Sub txtFirst_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
txtSecond.SetFocus
End If
End Sub
-----------------------------------
What will be In C#

Please contiue this.

Regards,
Rahman

Posted on 9/17/2007 2:29:45 AM by Rahman

Nice Articla.
Thank u very much. I hope u will gige me a lot tips in near future.

Posted on 11/3/2007 2:21:07 AM by krishna

Hi,
Its nice but if i have to trap F2 key, i.e. i want to fire some code when the user press F2 in the text box. Thwn how will I do that in C#?
Plz help.
Rahul
www.shopperixmall.com

Posted on 12/23/2007 11:34:17 PM by Rahul

Your article is very useful.
Can you please email how to change a first letter as uppercase in C#.

Posted on 12/24/2007 12:13:14 AM by Natarajan

Hi ,
Nice article.

But We want definition.
What happening there in each and every event............
am not able to under stand

Posted on 12/26/2007 6:41:48 AM by Ganesh

textbox keylostfocus

Posted on 1/22/2008 9:22:47 AM by km

Hi,
Pls send me the code of simple add values of two textbox in asp.net using c#.....and some more code of textbox and pannel

Posted on 2/1/2008 3:26:17 PM by saurabh

Hi,
Pls send me the code of simple add values of two textbox in asp.net using c#.....and some more code of textbox and pannel

Posted on 2/1/2008 3:26:22 PM by saurabh

it is very useful for .net freshers

Posted on 2/4/2008 12:30:38 AM by Nagaraju

I learn more in C# please continue
Thanks

Posted on 4/16/2008 12:49:37 AM by Nagarajanr

i am interested in C# coding

Posted on 5/9/2008 5:19:20 AM by mukesh Technopoint

i like very much. it was good.

Posted on 7/10/2008 1:17:00 AM by kingston

but how cud we use all these text box events in web page using c#

Posted on 9/17/2008 6:01:11 AM by shoaib

Need to go from One textbox to another using keypress event.
---------------------------
In VB
Private Sub txtFirst_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
txtSecond.SetFocus
End If
End Sub
-----------------------------------
What will be In C#

Posted on 9/27/2008 2:11:42 AM by Sathesh

i will be so happy if i could get the program for the same in c#?

Posted on 12/16/2008 5:25:23 AM by rakesh lal

hi

i tried all the c# codings buts its not working.
what to do?
any other code?

Posted on 1/7/2010 5:24:58 AM by deepthy

Hi deehthy,

What type of error you are getting?

Posted on 1/7/2010 6:07:06 AM by zunair

i take only colour in textbox with the help of leave and enter event

Posted on 1/29/2010 3:41:47 AM by pooja

tell me how to enter into the events of the textbox control?

for example: private void txtsample_KeyPress(...) to use keypress event we need to write the code like above or is there any other process like double clicking on the textbox and changing properties etc....

Posted on 3/11/2010 6:29:41 AM by venkat

Hi
i think this code can help you
private void Login_KeyPress(object sender, KeyPressEventArgs e)
{
char Test =e.KeyChar;
if (Test==char.Parse("\r"))//\r replace keyascii=13 in vb
{
MessageBox.Show("ok");
}
else
MessageBox.Show("no");
}

Posted on 3/12/2010 6:57:30 PM by Mohamed Ghodbane

send basic and more examples with timer control in c#.net

Posted on 3/16/2010 3:19:09 PM by venkat

when i am trying to install sql server2008 in my computer it is giving
invalid login or unauthorized use errors

how to solve

Posted on 3/16/2010 3:29:38 PM by venkat

hi
i need code of textbox when we enter value on runtime then on the selected of textbox without delete ,previous value has deleted automatically means that if we want enter the rupees in textbox and by using property of textbox we write on the text 0, without use delete button.on running of page when we enter rupees then o hasbeen deleted automatically pls send me code and pls solve this query.
thanks


thanks

Posted on 4/10/2010 3:05:39 PM by sonam purwar

thank u very much this is very useful to me................

Posted on 4/11/2010 8:09:47 AM by prithivi

Thank you for good lesson for understanding
Thank you.

Muneer
Atcom.Pvt .Ltd
Calicut,Riyadh

Posted on 4/27/2010 11:30:50 AM by muneer

I want something more..........

Posted on 6/12/2010 9:02:34 AM by Rajesh

i want c# code

Posted on 7/16/2010 6:39:35 AM by sandy

good and it's helpful for me

Posted on 9/8/2010 4:17:51 AM by sachin

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Scrolling tool tip in a web page.

Disclaimer - Privacy
© 2002-2017 DevASP.net