Click here to Skip to main content
Click here to Skip to main content

How to add Click Event on TextBox

, 3 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;) .

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.

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)

Share

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.
 

Comments and Discussions

 
GeneralMy vote of 1 PinmemberSimon McKenzie29-Jun-09 17:35 
GeneralMy vote of 1 Pinmemberaaberg9-Feb-09 3:24 
Questionpardon? PinmemberMr.PoorEnglish5-Jan-08 8:20 
AnswerRe: pardon? PinmemberImranManni17-Mar-08 3:47 
GeneralRe: pardon? Pinmemberaaberg9-Feb-09 3:22 
GeneralRe: pardon? PinmemberImranManni9-Feb-09 4:52 
GeneralRe: pardon? Pinmemberaaberg9-Feb-09 7:58 

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

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

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