By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

Disable items in Combobox

P: n/a
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?
Jul 30 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
Jul 31 '08 #2

P: n/a
On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
<kmahoney@nospam_fireacademy.orgwrote:
>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.Items(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?
Aug 1 '08 #3

P: n/a
What about a trick.. You can set on an Array what index want to "disable".
On the SelectedIndexChanged 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@nospam_fireacademy.orgescribió en el mensaje de
noticias:oi********************************@4ax.co m...
On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
<kmahoney@nospam_fireacademy.orgwrote:
>>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.Items(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?
Aug 4 '08 #4

P: n/a
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 "EnableItemComboBox" 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 EnableItemComboBox
Inherits ComboBox

Private vDisabledItems As New Collection

Public Property DisabledItems(ByVal Index As Integer) As Boolean
Get
Dim retVal As Boolean
If vDisabledItems.Contains("Key" & CStr(Index)) Then
retVal = CBool(vDisabledItems("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 EnableItemComboBox_DrawItem(ByVal sender As Object, ByVal e
As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
Dim Pn As Pen
If DisabledItems(e.Index) = False Then
Pn = New Pen(Color.FromKnownColor(KnownColor.WindowText))
Else
Pn = New Pen(Color.FromKnownColor(KnownColor.InactiveCaptio nText))
End If

Dim Br As Brush = Pn.Brush
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(Me.Items(e.Index).ToString, e.Font, Br,
e.Bounds.X, e.Bounds.Y)
End Sub

Private Sub EnableItemComboBox_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles Me.SelectedIndexChanged
If DisabledItems(Me.SelectedIndex) = True Then
Dim NewIndex As Integer = FindEnabledIndex(Me.SelectedIndex, True)
If NewIndex = -1 Then
NewIndex = FindEnabledIndex(Me.SelectedIndex, False)
End If

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

Private Function FindEnabledIndex(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(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
EnableItemComboBox1.Items.Add("Enabled")
EnableItemComboBox1.Items.Add("Disabled")
EnableItemComboBox1.DisabledItems(1) = True
EnableItemComboBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
End Class
On 02.08.2008 04:49, Kevinp wrote:
On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
<kmahoney@nospam_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.Items(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?
Aug 4 '08 #5

P: n/a
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.DrawMode.OwnerDrawFixed
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." <hk***@gmx.netwrote:
>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 "EnableItemComboBox" 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 EnableItemComboBox
Inherits ComboBox

Private vDisabledItems As New Collection

Public Property DisabledItems(ByVal Index As Integer) As Boolean
Get
Dim retVal As Boolean
If vDisabledItems.Contains("Key" & CStr(Index)) Then
retVal = CBool(vDisabledItems("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 EnableItemComboBox_DrawItem(ByVal sender As Object, ByVal e
As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
Dim Pn As Pen
If DisabledItems(e.Index) = False Then
Pn = New Pen(Color.FromKnownColor(KnownColor.WindowText))
Else
Pn = New Pen(Color.FromKnownColor(KnownColor.InactiveCaptio nText))
End If

Dim Br As Brush = Pn.Brush
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(Me.Items(e.Index).ToStrin g, e.Font, Br,
e.Bounds.X, e.Bounds.Y)
End Sub

Private Sub EnableItemComboBox_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles Me.SelectedIndexChanged
If DisabledItems(Me.SelectedIndex) = True Then
Dim NewIndex As Integer = FindEnabledIndex(Me.SelectedIndex, True)
If NewIndex = -1 Then
NewIndex = FindEnabledIndex(Me.SelectedIndex, False)
End If

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

Private Function FindEnabledIndex(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(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
EnableItemComboBox1.Items.Add("Enabled")
EnableItemComboBox1.Items.Add("Disabled")
EnableItemComboBox1.DisabledItems(1) = True
EnableItemComboBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub
End Class
On 02.08.2008 04:49, Kevinp wrote:
>On Wed, 30 Jul 2008 12:10:27 -0400, Kevinp
<kmahoney@nospam_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.Items(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?
Aug 4 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.