|
-
Jan 26th, 2015, 02:42 AM
#1
Thread Starter
Fanatic Member
Prevent Focus on textbox in code
Hi all,
I'm inheriting from a textbox, to amongst many other things alter the enabled behavior for a textbox in my application because "vissually impared" users can't read the grayed out text (i.o.w. sloppy users).
So I want the forecolor to stay black on the gray back color when a textbox is disabled, that's the easy part.
This is the class I'm using, but this way disabled textbox can get the focus and it's possible to select text.
how to avoid the textbox to accept focus?
Code:
Public Class TextboxPlus
Inherits System.Windows.Forms.TextBox
Private _enabled As Boolean = True
Private _foreColor As System.Drawing.Color = System.Drawing.SystemColors.ControlText
Private _backcolor As System.Drawing.Color = System.Drawing.SystemColors.Control
Private _disabledBackcolor As System.Drawing.Color = System.Drawing.SystemColors.InactiveCaption
Public Property DisabledBackColor As System.Drawing.Color
Get
Return _disabledBackcolor
End Get
Set(value As System.Drawing.Color)
_disabledBackcolor = value
End Set
End Property
Public Shadows Property BackColor As System.Drawing.Color
Get
Return If(_enabled, _backcolor, _disabledBackcolor)
End Get
Set(value As System.Drawing.Color)
_backcolor = value
End Set
End Property
Public Shadows Property Forecolor As System.Drawing.Color
Get
Return _foreColor
End Get
Set(value As System.Drawing.Color)
_foreColor = value
End Set
End Property
Public Shadows Property Enabled As Boolean
Get
Return _enabled
End Get
Set(value As Boolean)
_enabled = value
MyBase.BackColor = If(_enabled, _backcolor, _disabledBackcolor)
End Set
End Property
Private Sub TextboxPlus_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not _enabled Then
e.Handled = True
Exit Sub
End If
'---------other code---------------
End Sub
Private Sub TextboxPlus_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If Not _enabled Then
e.Handled = True
Exit Sub
End If
'---------other code---------------
End Sub
'-------- other overrides and extra methods
End Class
 why can't programmers keep and 31 Oct and 25 dec apart. Why Rating is Useful
for every question you ask provide an answer on another thread.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|