Textbox question

  • Thread starter Thread starter Localbar
  • Start date Start date
L

Localbar

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
 
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
 
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.
 
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
 
Thanks Kerry..Let me try now

Kerry Moorman said:
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
 
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.
-----------------------------
 
Thanks RobinS..It done.

RobinS said:
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.
 
Back
Top