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.
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.