473,320 Members | 2,080 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,320 software developers and data experts.

How to add properties to the Item object of a combobox?

4
I would like to add the properties "visible" and "enabled" to the Items object of a combobox in VB .Net so that I can hide or disable individual line items of the combobox. I have created the class "CustomListItem" that inherits the combobox.objectcollection. I have also created the class "CustomComboBox". Now, I would like to override the custombox.Items object with my custom one. Is this possible to do?
Jul 31 '09 #1
1 2410
Meggan
4
I have found a workable solution. Maybe not the prettiest code, but it works so far.

I have created a custom array which holds the data for combobox and visible and enabled boolean.

I have created a custom combobox that inherits the MS combobox. I added a visablerows property. I added the following code to handle the measureitems code:

-------------------------------------------------------
Private Sub HandleMeasureItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MeasureItemEventArgs) _
Handles Me.MeasureItem

'If (e.Index = -1) Then Return

Dim arrayitem As CustomCBArray

arrayitem = sender.DataSource(e.Index)

If arrayitem.Visable Then
If e.ItemHeight = 0 Or e.ItemWidth = 0 Then
e.ItemHeight = sender.Bounds.Height - 2
e.ItemWidth = sender.Bounds.Width - 4
End If
Else
e.ItemHeight = 0
e.ItemWidth = 0
End If

arrayitem = Nothing

End Sub

----------------------------------------

I created another sub to handle the drawitems event in the custom combobox

-------------------------------------------
Private Sub DrawPullDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles Me.DrawItem

If (e.Index = -1) Then Return

Dim arrayitem As CustomCBArray
Dim boxcolor As New System.Drawing.Color
Dim fontcolor As System.Drawing.Brush
Dim box_x As Integer = 0
Dim box_y As Integer = 0
Dim box_h As Integer = 0
Dim box_w As Integer = 0
Dim dstring As String = ""
Dim itemind As Integer = 0

itemind = e.Index

boxcolor = sender.BackColor
fontcolor = Brushes.Black

' Draw the background of the item.
e.DrawBackground()

Try
arrayitem = sender.DataSource(itemind)

If Not arrayitem.Visable Then
box_x = 0
box_y = 0
box_w = 0
box_h = 0
Else
box_x = e.Bounds.X
box_y = e.Bounds.Y
box_w = e.Bounds.Width
box_h = e.Bounds.Height
End If

If Not arrayitem.Enabled Then
fontcolor = Brushes.DarkGray
End If

dstring = arrayitem.DisplayString.ToString

Catch
dstring = ""
End Try

arrayitem = Nothing

Dim rectangle As Rectangle = New Rectangle(box_x, _
box_y, box_w, box_h)

If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
boxcolor = Color.Black
fontcolor = Brushes.White
End If
e.Graphics.FillRectangle(New SolidBrush(boxcolor), rectangle)

' Draw each string in the array, using a different size, color,
' and font for each item.
e.Graphics.DrawString(dstring, _
e.Font, fontcolor, _
New RectangleF(box_x, box_y, box_w, box_h))

' Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle()

End Sub

--------------------------------------------------------------
I set the "DrawMode" propery of the combobox to "OwnerDrawVariable"

When I fill the control I set the visable and enabled values in the array. When I go to set a value for the combobox while loading the form, if enabled is false and visable is false (an old data value is still being used in the form) and I want it to be visable, I set the visable flag to true and add one to the visablerows property. Then I refill the control as follows:

------------------------------------------------------------

Public Sub O_ReFillControl(ByRef cbo As CustomComboBox)

If IsDBNull(cbo.DataSource) Then Exit Sub

Dim tarry As New ArrayList
Dim vr As Integer

tarry = cbo.DataSource
vr = cbo.VisableRows

With cbo
.DataSource = Nothing
.Items.Clear()

.DisplayMember = "DisplayString"
.ValueMember = "RecordID"
.DataSource = tarry
.VisableRows = NZ(vr, 0)

If .VisableRows < .MaxDropDownItems Then
.DropDownHeight = .VisableRows * .ClientRectangle.Height
End If
End With

End Sub

------------------------------------------------------------------
Note: you should add the last if statement above to the fill control procedure as well.
Aug 5 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nikhil Patel | last post by:
Hi all, I am using the standard System.Windows.Forms.ComboBox controls on a form. There are 5 or 6 comboboxes and their selection depends on each other. The comboboxes behave strangely probably...
4
by: Daniel Mihaita | last post by:
Hello there, I need to code an object with two properties that binds dynamicaly, something like bind comboboxes on web pages. So my object needs to gave two properties Prop1 and Prop2. I need...
1
by: nickyw | last post by:
Hi, Can anyone help, I want to get a reference to a combobox item based on the item's value rather than the text (basically what I am asking is, is there a function like 'FindStringExact' for...
7
by: Yavuz Bogazci | last post by:
Hi, i have a question: how can i add items to my comboboxes with more informations. For Example i want to show the names of my customers an behind the names i want to store id's or phonenumbers...
7
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and...
5
by: VB Newbie | last post by:
I am creating a user control containing a combobox using VB.NET(2003) I want to add 2 public properties "DataSource" and "Items" like the "System.Windows.Forms.ComboBox" here is my code, but it...
4
by: Jerad Rose | last post by:
I'm baffled by this -- is there not a typed object used for ComboBox Items? Best I can tell, all of the methods for ComboBox that accept an Item are of type Object. Why in the world is a...
6
by: Smokey Grindle | last post by:
Say I have a combo box with the following simple object Public class MyObject public ID as integer public Name as string public overrides sub ToString() as string return name end sub end...
2
by: =?Utf-8?B?SmFzb24gQmFybmV0dA==?= | last post by:
I've created a component that inherits from ComboBox. Wiithin its constructor, I've hardcoded some initial items that I'd like added. These items are of a custom class. When I add my control...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.