473,395 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Jul 30 '08 #1
5 18596
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sql | last post by:
Hi all, I am using a CheckBoxList control on my aspx page that I bind to an array at runtime. After I databind it, I check two of the items in the CheckBoxList controls. Now I also need to disable...
5
by: Bob Bedford | last post by:
I create checkboxes using datas from a database (with PHP). I store all values in an array, as I must pass this value like a Form value. Now, in some cases, when a checkbox is selected, I must...
1
by: Bobby | last post by:
I want to enable/disable individual items in a checkedlistbox. I have a field in a db that tells me whether the item should be enabled. I am just not sure how to disable an item in the...
1
by: Rob R. Ainscough | last post by:
What I'm trying to accomplish is: Disable a ComboBox (not bound to any data) when a .Clear method is executed. So far a cboComboBox.Items.Clear does NOT trigger any of the events I expected it...
8
by: Steven Garrad | last post by:
Hi All I have a custom class that is derived from the PictureBox class. One of the properties from the PictureBox class is "SizeMode" and in my custom class I want to disable the user from being...
0
by: =?Utf-8?B?Q2VWYXU=?= | last post by:
Hi there, I'm trying to use a ComboBox in a way, that the Control is disabled (Enable = false), but I still want to use the Controls Click Event. Does anybody know a way ? Regards,
2
by: kewldudehere | last post by:
Hi All, Is it Possible to disable or greyout some items in dropdownlist. My application is role based and want to disable selection of some items in dropdownlist based upon user role. But i want...
1
by: Bob | last post by:
Is there any practical way to disable items within a ListView control? I'd like to be able to 'filter' the list incrementally without actually removing anything (so I can remove the filter to get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.