Results 1 to 10 of 10

Thread: [RESOLVED] Combobox - disable drop-down

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Resolved [RESOLVED] Combobox - disable drop-down

    Hi,

    I want to disable drop down of combobox and make It appear exactly like textbox. You can set DropdownHeight to 1, but that still draws a line under It. Another option is to set It to simple, and that is what I want, but then combobox appears with visual style border so It doesn't look like other textboxes. I tried simple paint events of border, but no affect at all.

    How could I override visual style border in simple dropdown list, or set dropdown height to 0 ?

  2. #2
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    477

    Re: Combobox - disable drop-down

    Just curious why you want a combobox to look like a textbox and not dropdown.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Combobox - disable drop-down

    I have a form where I edit records and I have comboboxes,textboxes and datetimepickers there. When form loads all controls are disabled for editing. I handled this by setting all textboxes Readonly, preventing combobox to load items and disabled changing dates in datetimepicker until I hit edit button. All controls are bounded to DB and I want them to look same as possible. Another option I tried was setting them enabled=false, but then issues accure in Datetimepickers - they don't have same disabled colour as other controls. It's just a visual style I want to achieve, nothing else.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: Combobox - disable drop-down

    That isn't going to help because you can still use the arrow keys to change the selection or type the first character in the desired item. I would recommend setting Enabled to False if you don't want a control enabled but if you really want those controls to look exactly like TextBoxes then use TextBoxes and just hide and show as required when editing.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Combobox - disable drop-down

    arrow keys doesn't bother me, and I have set e.handled=true to disable typing in them. I would set them to disabled, but then Datetimepicker is a problem - It's disabled colour is more white then gray, so It's a visual problem again. Yes, I could hide and show, but I'm kind of concerned about having so much controls on form (we are talking about +20 controls now) - only If I made them dynamically ?
    Last edited by LuckyLuke82; Mar 8th, 2017 at 03:53 AM.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: Combobox - disable drop-down

    Quote Originally Posted by LuckyLuke82 View Post
    I'm kind of concerned about having so much controls on form (we are talking about +20 controls now)
    And have you tested to see whether that concern is warranted?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Combobox - disable drop-down

    And have you tested to see whether that concern is warranted?
    Not yet, I'm stil looking for some different solutions, but nothing so far. I've found custom combobox classes, but that visual style border is still not overrided. How would you make this show/hide feature - place +20 textboxes next to comboboxes in design mode ?

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,623

    Re: Combobox - disable drop-down

    Well this may not be the preferred solution but in the past what I have did is make the bound combo invisible and place a text box on the form. Then place code to update the combo or text box as needed when something changes.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Combobox - disable drop-down

    Well this may not be the preferred solution but in the past what I have did is make the bound combo invisible and place a text box on the form. Then place code to update the combo or text box as needed when something changes.
    Yes, I've read a lot about that approach. But It's not something that I would be satisfied with.... So I did some research and ended up with creating a custom combobox that would suit my needs. What I created is a combobox with 3 properties:

    - LockCombo (to lock combobox entirely - no dropdown, no keypress - simmilar to Textbox ReadOnly);
    - BorderColor (change combobox border colour to whatever you like);
    - CustomBorder (option to enable or disable custom border, but to make things easier I excluded Dropdown Simple from this);

    I believe that someone will need something like this in future as It might get handy in apps where you have forms for edit , so I decided to share my custom combobox with community

    Here is the code:

    Code:
    Public Class LockCombobox
        Inherits ComboBox
    
        Private Const WM_PAINT As Integer = &HF
        Private _ButtonWidth As Integer = SystemInformation.HorizontalScrollBarArrowWidth
        Private _LockCombo As Boolean
        Private _EnableTheme As Boolean = False
        Private _CustomBorder As Boolean
        Private _BorderColor As Color
    
        Public Property LockCombo() As Boolean
            Get
                Return _LockCombo
            End Get
            Set(ByVal value As Boolean)
                ' If the value isn't changing, then do nothing
                If value = _LockCombo Then Exit Property
                _LockCombo = value
    
            End Set
        End Property
    
        Public Property BorderColor() As Color
            Get
                Return _BorderColor
            End Get
            Set
                _BorderColor = Value
    
            End Set
    
        End Property
    
        Public Property CustomBorder() As Boolean
            Get
                Return _CustomBorder
            End Get
            Set
                _CustomBorder = Value
            End Set
    
        End Property
    
        Protected Overrides Sub OnKeydown(e As KeyEventArgs)
    
            'Disable all keys instead of e.handled=true in Keypress which still leaves you with
            'enabled arrow keys for navigating items, delete key for delete text when selected, F4 for  combobox dropdown etc.
            If LockCombo Then
                e.SuppressKeyPress = True
            Else
                e.Handled = False
            End If
    
            MyBase.OnKeyDown(e)
        End Sub
    
        Protected Overrides Sub OnMouseLeave(e As EventArgs)
    
            'If we have set Dropdown Simple then enable drawing custom border again
            'when mouse leaves combobox
            If _CustomBorder = False Then
    
                If Me.DropDownStyle = ComboBoxStyle.Simple Then
    
                    _CustomBorder = True
                    Me.Invalidate()
    
                End If
    
            End If
    
            MyBase.OnMouseLeave(e)
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
    
            Select Case m.Msg
    
                'Handle WM_LBUTTONDOWN and WM_LBUTTONDBLCLK
                'Disable clicks when locked property is set to true
                Case &H201, &H203
    
                    If LockCombo Then
                        Exit Select
                    Else
                        MyBase.WndProc(m)
                    End If
    
                    'Handle WM_SETCURSOR - when you focus into
                    'combobox with Simple dropdown enable Windows Theme back
                    'and disable custom border
                Case &H20
    
                    If _CustomBorder Then
    
                        If Me.DropDownStyle = ComboBoxStyle.Simple Then
                            _CustomBorder = False
                        End If
    
                    End If
    
                Case Else
    
                    'Just for testing purpose to see all messages in console - you can remove this line
                    Console.WriteLine(m.ToString())
    
                    MyBase.WndProc(m)
                    Exit Select
            End Select
    
            'Paint border when you set custom border property to True
            If m.Msg = WM_PAINT Then
    
                If _CustomBorder Then
    
                    Using g = Graphics.FromHwnd(Handle)
    
                        Using p = New Pen(BorderColor)
    
                            If DropDownStyle = ComboBoxStyle.DropDown Then
                                g.DrawRectangle(p, 0, 0, Width - 1, Height - 1)
                                g.DrawLine(p, Width - _ButtonWidth, 0, Width - _ButtonWidth, Height)
    
                            ElseIf DropDownStyle = ComboBoxStyle.DropDownlist Then
                                g.DrawRectangle(p, 0, 0, Width - 1, Height - 1)
    
                            ElseIf DropDownStyle = ComboBoxStyle.Simple Then
                                g.DrawRectangle(p, 0, 0, Width - 1, Height - 1)
                                g.DrawLine(New Pen(Color.Silver), 2, 0, Width - 3, 0)
                            End If
    
                        End Using
    
                    End Using
    
                End If
    
            End If
    
        End Sub
    
    End Class
    Though It's still not a completely perfect solution for me, because what I wanted was to make Simple Dropdown style to look and behave same as Textbox when It's ReadOnly. Problem is that in this case combobox has Windows theme border turned on allways (ofcourse If you have set Windows theme, but normally all users have). I solved that issue too, but windows theme border comes back very fast when you hover over my combobox, so I would need to reduce speed of Paint or do something else.

    Before closing this thread I ask kindly If someone would take a look at my example project where you can see what I'm talking about. I've also set a line for console to capture all windows messages when you'll be in control, in case somebody get's an idea for solving this Simple Dropdown style

    P.S.: Haven't tested my combobox when binded to DB yet, but I think It should work same.
    Attached Files Attached Files
    Last edited by LuckyLuke82; Mar 11th, 2017 at 02:40 PM.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2016
    Location
    Slovenia
    Posts
    575

    Re: Combobox - disable drop-down

    Guess that this is It. I allready satisfied with this, I'll just keep dropdown simple style, more important is that combobox is locked.

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