Click here to Skip to main content
13,141,429 members (26,187 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 3 Jan 2008

How to add Click Event on TextBox

, 3 Jan 2008
Rate this:
Please Sign up or sign in to vote.
How to add Click Event on TextBox


Few days back I was working on smart device application using VB.NET VS2005 and I required functionality of Click event on “TextBox” control. As by default TextBox control does not provide implementation of Click event so I was wondering how to get this, I am not that good in windows application development as in web application (That does not mean I am very good in Web application ;).

Well, as usual “uncle google” came to help me and I found few articles on MSDN and Microsoft forums suggesting solution for my problem.


Let me describe little more information about events on TextBox. Natively VS 2005 provides a long list of events on textbox which you can see when you are about to register some event with control. e.g: on TextBox

In C# something like

Textbox1.Change += Event_Handler();

Or in VB.NET

.NET does not provide all events implementation, provided are just few which you can see in control properties window and clicking event tab, these are implemented events rest of are declared as you can see in code snippet Click event exits in list of textbox events but if you register that event that will not work for you as that does not have implementation provided from .NET. You need to implement this functionality.

Using the code

1 ) Create a VB.NET windows application project and name it “ClickableTextBoxProj”.

2 ) Add a new module from solutions explorer into your project “ClickableTextBoxProj”.

ClickableTextBoxProj > add > Add new item> Module Name it what ever you like. I named it as “ctrl”.

3 ) Write a nested class which inherent TextBox class inside newly added module. I have added it to module but you can create it as new class too.

4 ) In nested class “SingleClickTextBox” Override “OnClick” event. Like code below.

Module ctrl
    Public Class SingleClickTextBox
        Inherits TextBox

        Protected Overrides Sub OnClick(ByVal e As EventArgs)
        End Sub
    End Class
End Module

6 ) Now open form1.designer.vb and search for InitializeComponent() and create an instance of newly created class and set properties of control in InitializeComponent() method.

7) Write Click event handler function in form1.vb and write some code like messagebox or somthing to check either event is raised and code is executed.

That’s it execute the project and click on Textbox to see result.Complete project with source code is included.

Points of Interest

This code will work only on Windows forms but this overridden Click event is not supported in PPC application on Compact Framework as Compact Framework gives very limited functionality to minimize its size.

So we need a workaround to get the same functionality on Pocket PC application. Microsoft has provided win32 and WndProcHooker classes which are used to implement events on controls which are available on MSDN website.


We will discuss “SubClassing TextBox using Native Callbacks” in next article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

imran afzal
Software Developer Adaptive solutions Inc.
Pakistan Pakistan
BS(CS) From SZABIST (Pakistan)
I have been working as
"Web Application Developer"
with Adaptive Solutions Inc.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Simon McKenzie29-Jun-09 16:35
memberSimon McKenzie29-Jun-09 16:35 
GeneralMy vote of 1 Pin
aaberg9-Feb-09 2:24
memberaaberg9-Feb-09 2:24 
Questionpardon? Pin
Mr.PoorEnglish5-Jan-08 7:20
memberMr.PoorEnglish5-Jan-08 7:20 
AnswerRe: pardon? Pin
ImranManni17-Mar-08 2:47
memberImranManni17-Mar-08 2:47 
GeneralRe: pardon? Pin
aaberg9-Feb-09 2:22
memberaaberg9-Feb-09 2:22 
GeneralRe: pardon? Pin
ImranManni9-Feb-09 3:52
memberImranManni9-Feb-09 3:52 
GeneralRe: pardon? Pin
aaberg9-Feb-09 6:58
memberaaberg9-Feb-09 6:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 4 Jan 2008
Article Copyright 2008 by imran afzal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid