Disable items in Combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevinp

    Disable items in Combobox

    I've spent the last four hours Google searching for a way to disable
    items in a Combobox. I found one example in C++ which I can't get to
    work and another in C# that I couldn't get to work either.

    Does anyone have some good code for an owner-drawn combobox?
  • cfps.Christian

    #2
    Re: Disable items in Combobox

    This would be a tough one to hack. You can make a lineitem user
    control that can be disabled at will and handles the selection code.
    The line item held within another usercontrol that has some kind of
    dynamic sizing to handle adding of multiple line items. Me personally
    the best way I've found to lock down an item in a drop down is to
    remove it.

    Comment

    • Kevinp

      #3
      Re: Disable items in Combobox

      On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
      <kmahoney@nospa m_fireacademy.o rgwrote:
      >I've spent the last four hours Google searching for a way to disable
      >items in a Combobox. I found one example in C++ which I can't get to
      >work and another in C# that I couldn't get to work either.
      >
      >Does anyone have some good code for an owner-drawn combobox?

      I've already got a custom combobox control that I copied from someone
      else and made the changes that I needed.

      What I can't figure out is how to add a property to an item in the
      combobox.

      Like this: cboMyCombo.Item s(2).Enabled = False

      I know how to make a property for the Combobox, but not for an item in
      the Combobox. Can anyone help me?

      Comment

      • SuNcO

        #4
        Re: Disable items in Combobox

        What about a trick.. You can set on an Array what index want to "disable".
        On the SelectedIndexCh anged event you can check if that option is in the
        Array and then do a SelectedIndex = 0 (or the last one selected)

        Many stuff is just simply tricks (like this)

        Hope this help

        --
        Windows Live Butterfly
        My little blog - http://sunco.codigoplus.com (in Spanish)

        "Kevinp" <kmahoney@nospa m_fireacademy.o rgescribió en el mensaje de
        noticias:oit694 50c7e9ckalss8ib r4qi8p9f2au5i@4 ax.com...
        On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
        <kmahoney@nospa m_fireacademy.o rgwrote:
        >
        >>I've spent the last four hours Google searching for a way to disable
        >>items in a Combobox. I found one example in C++ which I can't get to
        >>work and another in C# that I couldn't get to work either.
        >>
        >>Does anyone have some good code for an owner-drawn combobox?
        >
        >
        I've already got a custom combobox control that I copied from someone
        else and made the changes that I needed.
        >
        What I can't figure out is how to add a property to an item in the
        combobox.
        >
        Like this: cboMyCombo.Item s(2).Enabled = False
        >
        I know how to make a property for the Combobox, but not for an item in
        the Combobox. Can anyone help me?

        Comment

        • Martin H.

          #5
          Re: Disable items in Combobox

          Hello Kevin,

          the following code will have a quick hack version of what you need.
          Just create a form and add the following code. Then, compile the project.

          Now, add one "EnableItemComb oBox" and a button. Copy the Form1 code into
          that form.

          Run the program and click the button. Then, open the list of the
          ComboBox. The "Disabled" entry will be in the color your Windows
          settings define for DisabledCaption (Default: light gray).

          Best regards,

          Martin

          Public Class EnableItemCombo Box
          Inherits ComboBox

          Private vDisabledItems As New Collection

          Public Property DisabledItems(B yVal Index As Integer) As Boolean
          Get
          Dim retVal As Boolean
          If vDisabledItems. Contains("Key" & CStr(Index)) Then
          retVal = CBool(vDisabled Items("Key" & CStr(Index)))
          Else
          retVal = False
          End If
          Return retVal
          End Get
          Set(ByVal value As Boolean)
          If vDisabledItems. Contains("Key" & CStr(Index)) Then
          vDisabledItems. Remove("Key" & CStr(Index))
          End If

          If value = True Then
          vDisabledItems. Add("-1", "Key" & CStr(Index))
          End If
          End Set
          End Property

          Private Sub EnableItemCombo Box_DrawItem(By Val sender As Object, ByVal e
          As System.Windows. Forms.DrawItemE ventArgs) Handles Me.DrawItem
          Dim Pn As Pen
          If DisabledItems(e .Index) = False Then
          Pn = New Pen(Color.FromK nownColor(Known Color.WindowTex t))
          Else
          Pn = New Pen(Color.FromK nownColor(Known Color.InactiveC aptionText))
          End If

          Dim Br As Brush = Pn.Brush
          e.DrawBackgroun d()
          e.DrawFocusRect angle()
          e.Graphics.Draw String(Me.Items (e.Index).ToStr ing, e.Font, Br,
          e.Bounds.X, e.Bounds.Y)
          End Sub

          Private Sub EnableItemCombo Box_SelectedInd exChanged(ByVal sender As
          Object, ByVal e As System.EventArg s) Handles Me.SelectedInde xChanged
          If DisabledItems(M e.SelectedIndex ) = True Then
          Dim NewIndex As Integer = FindEnabledInde x(Me.SelectedIn dex, True)
          If NewIndex = -1 Then
          NewIndex = FindEnabledInde x(Me.SelectedIn dex, False)
          End If

          If NewIndex -1 Then
          Me.SelectedInde x = NewIndex
          Beep()
          End If
          End If
          End Sub

          Private Function FindEnabledInde x(ByVal StartValue As Integer, ByVal
          Descending As Boolean) As Integer
          Dim EndValue As Integer
          Dim Steps As Integer
          Dim t As Integer
          Dim retValue As Integer = -1

          If Descending = True Then
          StartValue = Items.Count - 1
          EndValue = 0
          Steps = -1
          Else
          StartValue = 0
          EndValue = Items.Count - 1
          Steps = 1
          End If

          For t = StartValue To EndValue Step Steps
          If DisabledItems(t ) = False Then
          retValue = t
          Exit For
          End If
          Next

          Return retValue
          End Function
          End Class



          Public Class Form1

          Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As _
          System.EventArg s) Handles Button1.Click
          EnableItemCombo Box1.Items.Add( "Enabled")
          EnableItemCombo Box1.Items.Add( "Disabled")
          EnableItemCombo Box1.DisabledIt ems(1) = True
          EnableItemCombo Box1.DrawMode = DrawMode.OwnerD rawFixed
          End Sub
          End Class


          On 02.08.2008 04:49, Kevinp wrote:
          On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
          <kmahoney@nospa m_fireacademy.o rg wrote:
          >
          >I've spent the last four hours Google searching for a way to disable
          >items in a Combobox. I found one example in C++ which I can't get to
          >work and another in C# that I couldn't get to work either.
          >>
          >Does anyone have some good code for an owner-drawn combobox?
          >
          >
          I've already got a custom combobox control that I copied from someone
          else and made the changes that I needed.
          >
          What I can't figure out is how to add a property to an item in the
          combobox.
          >
          Like this: cboMyCombo.Item s(2).Enabled = False
          >
          I know how to make a property for the Combobox, but not for an item in
          the Combobox. Can anyone help me?

          Comment

          • Kevinp

            #6
            Re: Disable items in Combobox

            Thanks Martin. Exactly what I needed. I did make one change though. I
            added this to the class:

            Public Sub New()
            Me.DrawMode = Windows.Forms.D rawMode.OwnerDr awFixed
            End Sub

            ....instead of adding it to every form I use the new control on. No
            biggee--just saves a little work.

            Thanks,
            Kevin

            On Mon, 04 Aug 2008 13:30:16 +0800, "Martin H." <hkshk@gmx.netw rote:
            >Hello Kevin,
            >
            >the following code will have a quick hack version of what you need.
            >Just create a form and add the following code. Then, compile the project.
            >
            >Now, add one "EnableItemComb oBox" and a button. Copy the Form1 code into
            >that form.
            >
            >Run the program and click the button. Then, open the list of the
            >ComboBox. The "Disabled" entry will be in the color your Windows
            >settings define for DisabledCaption (Default: light gray).
            >
            >Best regards,
            >
            >Martin
            >
            >Public Class EnableItemCombo Box
            >Inherits ComboBox
            >
            >Private vDisabledItems As New Collection
            >
            >Public Property DisabledItems(B yVal Index As Integer) As Boolean
            > Get
            > Dim retVal As Boolean
            > If vDisabledItems. Contains("Key" & CStr(Index)) Then
            > retVal = CBool(vDisabled Items("Key" & CStr(Index)))
            > Else
            > retVal = False
            > End If
            > Return retVal
            > End Get
            > Set(ByVal value As Boolean)
            > If vDisabledItems. Contains("Key" & CStr(Index)) Then
            > vDisabledItems. Remove("Key" & CStr(Index))
            > End If
            >
            > If value = True Then
            > vDisabledItems. Add("-1", "Key" & CStr(Index))
            > End If
            > End Set
            >End Property
            >
            >Private Sub EnableItemCombo Box_DrawItem(By Val sender As Object, ByVal e
            >As System.Windows. Forms.DrawItemE ventArgs) Handles Me.DrawItem
            >Dim Pn As Pen
            > If DisabledItems(e .Index) = False Then
            > Pn = New Pen(Color.FromK nownColor(Known Color.WindowTex t))
            > Else
            > Pn = New Pen(Color.FromK nownColor(Known Color.InactiveC aptionText))
            > End If
            >
            >Dim Br As Brush = Pn.Brush
            >e.DrawBackgrou nd()
            >e.DrawFocusRec tangle()
            >e.Graphics.Dra wString(Me.Item s(e.Index).ToSt ring, e.Font, Br,
            >e.Bounds.X, e.Bounds.Y)
            >End Sub
            >
            >Private Sub EnableItemCombo Box_SelectedInd exChanged(ByVal sender As
            >Object, ByVal e As System.EventArg s) Handles Me.SelectedInde xChanged
            > If DisabledItems(M e.SelectedIndex ) = True Then
            > Dim NewIndex As Integer = FindEnabledInde x(Me.SelectedIn dex, True)
            > If NewIndex = -1 Then
            > NewIndex = FindEnabledInde x(Me.SelectedIn dex, False)
            > End If
            >
            > If NewIndex -1 Then
            > Me.SelectedInde x = NewIndex
            > Beep()
            > End If
            > End If
            >End Sub
            >
            >Private Function FindEnabledInde x(ByVal StartValue As Integer, ByVal
            >Descending As Boolean) As Integer
            >Dim EndValue As Integer
            >Dim Steps As Integer
            >Dim t As Integer
            >Dim retValue As Integer = -1
            >
            > If Descending = True Then
            > StartValue = Items.Count - 1
            > EndValue = 0
            > Steps = -1
            > Else
            > StartValue = 0
            > EndValue = Items.Count - 1
            > Steps = 1
            > End If
            >
            > For t = StartValue To EndValue Step Steps
            > If DisabledItems(t ) = False Then
            > retValue = t
            > Exit For
            > End If
            > Next
            >
            >Return retValue
            >End Function
            >End Class
            >
            >
            >
            >Public Class Form1
            >
            >Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As _
            >System.EventAr gs) Handles Button1.Click
            >EnableItemComb oBox1.Items.Add ("Enabled")
            >EnableItemComb oBox1.Items.Add ("Disabled")
            >EnableItemComb oBox1.DisabledI tems(1) = True
            >EnableItemComb oBox1.DrawMode = DrawMode.OwnerD rawFixed
            >End Sub
            >End Class
            >
            >
            >On 02.08.2008 04:49, Kevinp wrote:
            >On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
            ><kmahoney@nosp am_fireacademy. org wrote:
            >>
            >>I've spent the last four hours Google searching for a way to disable
            >>items in a Combobox. I found one example in C++ which I can't get to
            >>work and another in C# that I couldn't get to work either.
            >>>
            >>Does anyone have some good code for an owner-drawn combobox?
            >>
            >>
            >I've already got a custom combobox control that I copied from someone
            >else and made the changes that I needed.
            >>
            >What I can't figure out is how to add a property to an item in the
            >combobox.
            >>
            >Like this: cboMyCombo.Item s(2).Enabled = False
            >>
            >I know how to make a property for the Combobox, but not for an item in
            >the Combobox. Can anyone help me?

            Comment

            Working...