473,545 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18648
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@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?
Aug 1 '08 #3
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:oi**** *************** *************@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?
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 "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?
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.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." <hk***@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?
Aug 4 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1580
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 these two items so that users can't uncheck them. But I don't know how to disable individual items. Can you please help me achieving...
5
5970
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 disactivate other checkboxes. How can I do this ? here is the code:
1
9842
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 checkedlistbox Thanks, Robert *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
1
1504
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 would. I've placed break points in the following events and still they are not triggered: Click SelectedValueChange
8
1868
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 able to modify this. How can I remove this property from my class? Thanks, Steve
0
1221
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
2980
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 to show all items in dropdownlist. Thanks
1
3119
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 back to the full list). The ListView will have a large number of items, so it would be time-consuming to clone them and create new ListViews, etc....
0
7410
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.