472,796 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 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 18456
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{

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.