When a control has focus there is a very subtle change in it's colour, that experienced windows users know.
However we develop software that is often the 1st windows programme the users have experienced.
To help, with this we sometimes set the background colour to yellow on focus and white on no focus. I've also modified the code, to allow you to set it up for making the border colour (color) go yellow on focus, and form colour on lost focus (bit like google chrome text boxes).
Here is some easy code to allow this:
#Region "ChangeColourOnHasFocus"
Private intFocusType As FocusType = FocusType.BackgroundColor
Private Enum FocusType
BackgroundColor = 1
BorderColor = 2
End Enum
Private Sub SetControlBackgroundToWhiteOnLostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim ctl1 As Control
ctl1 = sender
Debug.WriteLine(ctl1.Text)
Dim blChangeColour As Boolean
blChangeColour = Me.needToChangeControlColourOnFocusChange(ctl1)
If blChangeColour Then
Select Case intFocusType
Case FocusType.BackgroundColor
ctl1.BackColor = Color.White
Case FocusType.BorderColor
Dim g As Graphics = Me.CreateGraphics
Dim pen As New Pen(Me.BackColor, 5.0)
g.DrawRectangle(pen, New _
Rectangle(ctl1.Location, ctl1.Size))
End Select
End If
End Sub
Private Sub SetControlBackgroundToYellowOnFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim ctl1 As Control
ctl1 = sender
Dim blChangeColour As Boolean
blChangeColour = Me.needToChangeControlColourOnFocusChange(ctl1)
If blChangeColour Then
'ctl1.BackColor = Color.Yellow
Select Case intFocusType
Case FocusType.BackgroundColor
ctl1.BackColor = Color.Yellow
Case FocusType.BorderColor
Dim g As Graphics = Me.CreateGraphics
Dim pen As New Pen(Color.Yellow, 5.0)
g.DrawRectangle(pen, New _
Rectangle(ctl1.Location, ctl1.Size))
End Select
End If
End Sub
Function needToChangeControlColourOnFocusChange(ByVal control1 As Control) As Boolean
' Don't change colour if control is not enabled or read only
Dim blSetColour As Boolean = True
If control1.Enabled = False Then
Return False
End If
If TypeOf (control1) Is TextBox Then
Dim txt1 As TextBox
txt1 = control1
If txt1.ReadOnly Then
Return False
End If
End If
Return True
End Function
Sub AddChangeColourOnFocusLostFocusHandlers()
' Can call this on form load on whenever you want to enable the colour changing
For Each control1 As Control In Me.Controls
Me.AddHandlersForLostFocusOnControlAndChildControls(control1)
Next
End Sub
Private Sub AddHandlersForLostFocusOnControlAndChildControls(ByVal control1 As Control)
For Each ctrlChild As Control In control1.Controls
Me.AddHandlersForLostFocusOnControlAndChildControls(ctrlChild)
Next
If TypeOf (control1) Is TextBox Or TypeOf (control1) Is ComboBox Then
AddHandler control1.GotFocus, AddressOf Me.SetControlBackgroundToYellowOnFocus
AddHandler control1.LostFocus, AddressOf Me.SetControlBackgroundToWhiteOnLostFocus
End If
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AddChangeColourOnFocusLostFocusHandlers()
End Sub
No comments:
Post a Comment