Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / Visual Basic
Article

How to add Click Event on TextBox

Rate me:
Please Sign up or sign in to vote.
1.32/5 (10 votes)
3 Jan 2008CPOL2 min read 97.8K   1K   23   7
How to add Click Event on TextBox

Introduction

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.

Background

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.

VB.NET
Module ctrl
    Public Class SingleClickTextBox
        Inherits TextBox

        Protected Overrides Sub OnClick(ByVal e As EventArgs)
            Me.SelectAll()
            MyBase.OnClick(e)
        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.

History

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

License

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


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


Comments and Discussions

 
GeneralMy vote of 1 Pin
Simon McKenzie29-Jun-09 16:35
Simon McKenzie29-Jun-09 16:35 
GeneralMy vote of 1 Pin
aaberg9-Feb-09 2:24
aaberg9-Feb-09 2:24 
Questionpardon? Pin
Mr.PoorEnglish5-Jan-08 7:20
Mr.PoorEnglish5-Jan-08 7:20 
AnswerRe: pardon? Pin
ImranManni17-Mar-08 2:47
ImranManni17-Mar-08 2:47 
GeneralRe: pardon? Pin
aaberg9-Feb-09 2:22
aaberg9-Feb-09 2:22 
GeneralRe: pardon? Pin
ImranManni9-Feb-09 3:52
ImranManni9-Feb-09 3:52 
GeneralRe: pardon? Pin
aaberg9-Feb-09 6:58
aaberg9-Feb-09 6:58 
Sorry, I didn't read the article thorough. I didn't realise it was for a smart device.

Thanks for the quick reply.

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.