Results 1 to 9 of 9

Thread: Prevent Focus on textbox in code

  1. #1

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Question 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.

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Prevent Focus on textbox in code

    Use a label. If the text can't be changed or selected, then there isn't much use to a textbox... so a label would be appropriate.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Prevent Focus on textbox in code

    Hi techgnome


    some users have the right to edit tekst others don't
    sometimes entry's are locked by other users
    sometimes the object is locked due to it's status.

    and it's normal to show data normally is editable by users in a textbox.
    So a label is not an option to me. Just as you'll see many disabled textboxes in applications.
    Last edited by Dnereb; Jan 26th, 2015 at 05:39 AM. Reason: spelling
    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.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Prevent Focus on textbox in code

    True... and normally that's what I'd argue... but if your'e disabling it, changing the color, and want to prevent focus and selection (not sure why) ... then a label fits all that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Prevent Focus on textbox in code

    I want that because it's standard behavior for a textbox.
    You can't focus on a disabled textbox. But I want to keep the forecolor black for readabilty reasons.
    normal disabled textboxes are hard to read (for some users)
    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.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Prevent Focus on textbox in code

    Sorry... I had things mixed up... I was thinking about Readonly, which is what I normally use instead of enabled.

    I was going to suggest something along these lines to simply pass focus along, but for some reason I can't get it to work even with a couple of simple text boxes.
    Code:
        Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
            Dim ctl As Control
            ctl = CType(sender, Control)
            ctl.SelectNextControl(ActiveControl, True, True, True, True)
        End Sub
    Got it from here
    https://msdn.microsoft.com/en-us/lib...vs.110%29.aspx

    but it's not working.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Re: Prevent Focus on textbox in code

    Was running along the same line. and as Usual things are a bit more complicated then MSDN let's you believe

    The problem I'm not able to solve is if a form has zero controls that can recieve focus, in that scenario the usercontrole still will accept focus.
    Normally you could concider to ignore that, but a form showing data that's locked by another user is typically a form with all controls set to Enabled = False

    Code:
     Private Sub TextboxPlus_Enter(sender As Object, e As EventArgs) Handles Me.Enter
            If Not _enabled Then
                SelectNext(Me)
            End If
    
        End Sub
    
        Private Function SelectNext(Ctrl As Control) As Boolean
    
            Dim container As Control
            Dim result As Boolean
    
            container = Ctrl.Parent
    
            If container IsNot Nothing AndAlso Ctrl.Parent.Controls.Count > 1 Then
    
                'search on parent control
                For Each itm As Control In container.Controls
    
                    If itm IsNot Ctrl Then
                        'don't bother to check already searched part of the tree
                        If itm.CanFocus Then
                            'ther is another control the paren that can focus
                            result = True
                            container.SelectNextControl(Ctrl, True, True, True, True)
                            Exit For
                        ElseIf itm.Controls.Count > 0 Then
                            'this control hold ome other controls... to investigate
                            If SelectNext(itm.Controls(0)) Then
                                result = True
                                Exit For
                            End If
                        End If
                    End If
                Next
            End If
    
            If Not result AndAlso container.Parent IsNot Nothing Then
                result = SelectNext(container)
            End If
    
    
            Return result
    
        End Function
    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.

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,748

    Re: Prevent Focus on textbox in code

    Create an invisible label on the form. If there are zero controls that can receive focus select the invisible label.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Fanatic Member Dnereb's Avatar
    Join Date
    Aug 2005
    Location
    Netherlands
    Posts
    863

    Arrow Re: Prevent Focus on textbox in code

    Hi dbasnett,

    Thx for your thoughts

    I'm in a user control to be used on many forms.
    In essence this control must be usable just like a normal textbox drop on the form and bind to data or whatever you normally do with a textbox
    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
  •  



Click Here to Expand Forum to Full Width