Hello,
Is it possible to extend the TextBox class to include an event where the TextBox changes it's background color on focus and returns it to the default on lost focus
Kind regards,
Hello,
Is it possible to extend the TextBox class to include an event where the TextBox changes it's background color on focus and returns it to the default on lost focus
Kind regards,
TextBox BackColor change OnFocus and LostFocus
PDX_Catalyst
Hello,
Appreciate your answer. It works perfectly.
Would the same solution apply with a form that has, say ten TextBoxes- I mean, repeating the same code for each one of the TextBoxes (TextBox1,....2,...3. etc)
Kind regards,
--------------------------------------------------------------------------------------------------------------------------
This newbie can't talk and can't walk. Embarrassing.
Francesco75
Yes, that looks great.
Kirk Brote
Hello,
Thanks.
1. Please review this code. Is this the 'best" option The code included here (although it maybe not what you did suggest) works: it changes any TextBox's backcolor when the TextBox get focus (Enter) and changes it back to default (or any other color) when the TextBox looses focus (Leave).
When you said: derive - did you mean:Inherits
Public
Class ColorTBoxOnFocus End SubEnd Class
====================================================================================================
2. You wrote: Otherwise, you can hook up all your textboxes to call the same event. In this case, cast the sender parameter to be of type TextBox, and it will point to the textbox that fired the message.Could you give a more detailed instruction how to do it
This is my interpretation ( but certainly I do not know what exactly 'cast', 'point to', 'fired' means) so please bear with me.
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
End Sub
Kind regards,
----------------------------------------------------------------------------------------------------------------
This newbie can't talk and can't walk. Embarrassing.
CJWalsh
Hello and thank you,
You said: "I would do to this is add properties to set the two colors, so you can reuse the class where-ever you like." Is this below the correct interpretation
Public Class txtBoxBackColor
Public
Sub txtBoxBackColor_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EnterEnd Sub
Public
Sub txtBoxBackColor_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LeaveBackColor = ColorOff
End SubEnd
Class'Instructions to make the class:
File> New Project> WindowsApplication-Name:Test -OK> in the Solution Explorer: -highlight Test, - go to Project,- click on it. Add Class> Name it: txtBoxBackColor - in the Solution Explorer now you can see txtBoxBackColor.vb - Copy code above from 1. until 2. - Paste it between Public Class txtBoxBackColor and End Class> File-Save All > Hit F5 >A Window appears. Close it> Under Test Components in the ToolBox Window on the left side of the Design Window there is an icon with the label: txtBoxBackColor> Drag it into the Form1>Hit F5 to test.>Highlight txtBoxBackColor.vb in the Solution Explorer > File> Save txtBoxBackColor.vb As....(make yourself a Folder:My Classes and copy this class into it for future use)
Please correct.
Kind regards,
-------------------------------------------------------------------------------------------------------------------------------------
This newbie can't talk and can't walk. Embarrassing.
P.S. There is a new tread: ComboBox/DateTimePickerBox dropdown arrow visible/invisible. Could you please have a look at it.
Rémi P.
There are Enter and Leave events, you could try setting the backcolor in those.
Robert Shurbet
The *best*" option is to derive a class from the textbox and put this code inside that class. Otherwise, you can hook up all your textboxes to call the same event. In this case, cast the sender parameter to be of type TextBox, and it will point to the textbox that fired the message.
Dave987654321
1 - yes, I think this is the best option. Inherits is a VB keyword, and derive is the proper name for what it does. Sorry if that was confusing, but obviously not too confusing, you've done exactly what I was recommending.
The only thing I would do to this is add properties to set the two colors, so you can reuse the class where-ever you like.
2 - Sorry about my confusing terminology.
I think CType is the function used to cast in VB. Casting means to take a base object and turn it into an instance of a derived object. The parameter 'sender' is an object ( the base for all .NET reference types ) and it is the object that caused the event to occur, which is what I mean by 'fired'. So, insteasd of using TExtBox1, do something like
Dim tb as TextBox = CType(sender, TextBox)
tb.BackColor = .....
However, the class you wrote in response to part one is a better option, IMO. The technique I'm describing here is great tho when you have a number of controls that you want to respond to one event.