tags:

views:

196

answers:

7

Is there an easy way of disabling the drop-down part of a combo box? I want to prevent the user from seeing the items in the drop-down part in some scenarios.

EDIT

Thanks to all who replied so quickly!

I had already considered the option of placing a textbox or label in the same location as the combo box and then hiding the combo-box on demand but dismissed the idea on the grounds of complexity (there are quite a lot of combos on the form). I also looked at Simple mode but this removes the drop-down button part of the combo. I suppose what I'm really looking to do is disable the combo but without it looking like it is disabled and still enabling the user to select the displayed data (for copy and paste operations for example).

A: 

In those cases where you don't want the user to be able to see/select other values, you should hide the DropDown and show a static text container instead, e.g. Label.

AdamRalph
+1  A: 

Cant you just set the DropDownStyle to simple? If I recall correctly that does what you want, although it's a while since I've touched WinForms.

Edit:

dropDownList.DropDownStyle = ComboBoxStyle.Simple;
GenericTypeTea
+1  A: 

You can control this using the DropDownStyle property (not in the DropDown event though... that is too late). That will make the combo box to appear as a text box.

if (DropDownShouldBeVisible())
{
    comboBox.DropDownStyle = ComboBoxStyle.DropDownSimple;
}
else
{
    comboBox.DropDownStyle = ComboBoxStyle.Simple;
}

Note however that the user can still select the values in the list using the arrow keys in this case. If you wish to prevent that too, replace it with another suitable control in the same location, as suggested by Konrad.

Fredrik Mörk
A: 

I'm agree with the Konrad Rudolph comment. You could put a Textbox in the same position and set the combo and textbox visibility properties true or false.

Jonathan
A: 

Set Enabled to false - this prevents the user from selecting a value. As I don't see another way of preventing the list from dropping down, the only alternative way I can think of is to remporarily remove all items and add them again later.

Thorsten Dittmar
+1  A: 

Set it to simple and then cancel any key presses?

private void dropDownList_KeyPress(object sender, KeyPressEventArgs e)
{

  if (dropDownList.DropDownStyle == ComboBoxStyle.Simple)
  {
    e.Handled = true;
  }

}
GenericTypeTea
+1  A: 

First, you need to create a new class inherited from ComboBox(full code below). You don't have to override many of the methods. Add a boolean property to help you determine when you want it to drop down. The meat of the functionality is in overriding the OnDrawItem method. Essentially, if your condition (whatever it is) is true, you don't draw any of the items in the combobox. You need to override the OnDropDown method and set the DropDownHeight=1 (0 is invalid), otherwise, the combobox will still dropdown at its normal size, but it will appear to be empty. The combobox still drops down, but you can't see it because its height is only 1 pixel. It's important to set the DrawMode to OwnerDrawFixed in the New method, so the OnDrawItem code is executed.

When you reset the DropDownHeight so the items will display, you can either use a stored value from the original height, or set it to some arbitrarily large value that you know will be larger than you need; the combobox will automatically reduce this height so that it is no larger than necessary to display all the items.

You could simplify things by setting the DrawMode to Normal and ONLY overriding the OnDropDown method, but the OnDrawMethod gives you almost full control over how your list of items is displayed (if that's what you want).

Public Class simpleCombo
    Inherits ComboBox

    Private _myCondition As Boolean = False

    Public Property myCondition() As Boolean
        Get
            Return _myCondition
        End Get
        Set(ByVal value As Boolean)
            _myCondition = value
        End Set
    End Property

    Protected Overrides Sub OnDropDown(ByVal e As System.EventArgs)
        If _myCondition Then
            Me.DropDownHeight = 1
        Else
            Me.DropDownHeight = 200 //some arbitrarily large value
        End If

        MyBase.OnDropDown(e)
    End Sub

    Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)

        If _myCondition Then
            Return
        Else
            MyBase.OnDrawItem(e)
            e.DrawBackground()
            e.Graphics.DrawString(Me.Items(e.Index), Me.Font, New SolidBrush(Me.ForeColor), e.Bounds)
            e.DrawFocusRectangle()
        End If

    End Sub

    Public Sub New()
        Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed
    End Sub

End Class
Stewbob