Board index » Visual Studio » Textbox question
|
Spooled
|
|
Spooled
|
Textbox question
Visual Studio65
Hi all, In my form have more then 10 textbox. I would like to make all textbox when lostfocus backcolor is white, when gotfocus backcolor is yellow. But I don't want to write same code in the form. How to solve this problem. Is it can make a textbox class to control or some other method? Thanks - |
| KerryMoorman
Registered User |
Sun Apr 15 22:16:01 CDT 2007
Re:Textbox question
Localbar,
Use one event handler for all the textboxs's Gotfocus and Lostfocus events. For example: Private Sub Textbox_GotFocus(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles txtName.GotFocus, _ txtTest1.GotFocus, _ txtTest2.GotFocus, _ txtAverage.GotFocus Kerry Moorman "Localbar" wrote: QuoteHi all, |
| RobinS
Registered User |
Mon Apr 16 00:28:23 CDT 2007
Re:Textbox question
Create a base form that has no UI, all it does it loop through the controls
on the form and set the event handlers. I would recommend using Enter and Leave instead of the Focus events, by the way. Then have all of your forms inherit from the base form, and it will behave the same all over the place. Robin S. -------------------------------- "Localbar" <localbar.hk@gmail.com>wrote in message QuoteHi all, - |
| Localbar
Registered User |
Mon Apr 16 00:42:08 CDT 2007
Re:Textbox question
Hi Robin,
Can tell me more detail, After I create the form is no UI. and then how to do? ..No need to add textbox in baseform ? Thanks "RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó QuoteCreate a base form that has no UI, all it does it loop through the - |
| Localbar
Registered User |
Mon Apr 16 00:42:27 CDT 2007
Re:Textbox question
Thanks Kerry..Let me try now
"Kerry Moorman" <KerryMoorman@discussions.microsoft.com>¦b¶l¥ó QuoteLocalbar, - |
| RobinS
Registered User |
Mon Apr 16 02:34:14 CDT 2007
Re:Textbox question
Create a form and call it something like BaseForm. Don't put any controls
on it. Put this in the code behind the form: Private Sub AddEventHandlers(ByVal ctrlContainer As Control) For Each ctrl As Control In ctrlContainer.Controls If TypeOf ctrl Is TextBox _ OrElse TypeOf ctrl Is ComboBox Then AddHandler ctrl.Enter, AddressOf ProcessEnter AddHandler ctrl.Leave, AddressOf ProcessLeave End If 'If control has children, call this function recursively If ctrl.HasChildren Then AddEventHandlers(ctrl) End If Next End Sub Private Sub ProcessEnter(ByVal sender as Object, ByVal e as System.EventArgs) DirectCast(sender, Control).BackColor = Color.Lavender End Sub Private Sub ProcessLeave(ByVal sender as Object, ByVal e as System.EventArgs) DirectCast(sender, Control).BackColor = Color.FromKnownColor(KnownColor.Window) End Sub Add the BaseForm_Load event and put this in there: AddEventHandlers(me) Then create your new form, and either change it to inherit from BaseForm (this is probably in the designer-generated code), or use the Inheritance Picker to create your new form (which does the same thing). Put your textboxes on your new form, and run it, and voila! You get colors when you enter a textbox, and back to the original color when they leave. If you have every form inherit from the BaseForm, it will work the same on every form with no further effort on your part. Robin S. ----------------------------- "Localbar" <localbar.hk@gmail.com>wrote in message QuoteHi Robin, - |
| Localbar
Registered User |
Mon Apr 16 02:58:19 CDT 2007
Re:Textbox question
Thanks RobinS..It done.
"RobinS" <RobinS@NoSpam.yah.none>¦b¶l¥ó QuoteCreate a form and call it something like BaseForm. Don't put any controls - |
| RobinS
Registered User |
Tue Apr 17 02:15:07 CDT 2007
Re:Textbox question
Good. Glad I could help.
Robin S. -------------------------- "Localbar" <localbar.hk@gmail.com>wrote in message QuoteThanks RobinS..It done. - |
