Combo crash prevention

Is there an easy way to disable the drop-down portion of a combo box? I want the user not to see the items in the drop-down list in some scenarios.

EDIT

Thanks to everyone who answered so quickly!

I already considered the possibility of placing a text field or label in the same place as the combo box, and then hid the combo box on demand, but rejected the idea due to complexity (quite a lot of combos on the form). I also looked at simple mode, but this removes part of the combo drop-down list. I suppose what I really want to do is turn off the combo, but without it, it looks like it's disabled and still allows the user to select the data to be displayed (for example, for copy and paste operations).

+3
source share
8 answers

, ComboBox ( ). . boolean, , , . OnDrawItem. , ( ) , . OnDropDown DropDownHeight = 1 (0 ), - , . , , 1 . DrawMode OwnerDrawFixed New, OnDrawItem.

reset DropDownHeight, , , - , , , , ; combobox , .

, DrawMode Normal ONLY OnDropDown, OnDrawMethod , ( ).

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
+5

, DropDownStyle ( DropDown, ... ). .

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

, , - . , , Konrad.

+4

DropDownStyle? , , , , WinForms.

Edit:

dropDownList.DropDownStyle = ComboBoxStyle.Simple;
+1

?

private void dropDownList_KeyPress(object sender, KeyPressEventArgs e)
{

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

}
+1

, , Control.Enter, ComboBox.

private void myComboBox_Enter(object sender, EventArgs e)
{
    // Do some stuff
    myComboBox.Enabled = false;
    myComboBox.Enabled = true;
}

, ComboBox.

+1

, , / , DropDown , . .

0

. true false.

0

Enabled false - . , , , - .

0

Source: https://habr.com/ru/post/1711063/


All Articles