Click here to Skip to main content
12,253,698 members (58,732 online)
Click here to Skip to main content
Add your own
alternative version

Stats

631.6K views
8.3K downloads
93 bookmarked
Posted

Multi-Column ComboBox

, 1 Jul 2003
Rate this:
Please Sign up or sign in to vote.
Implementation of an OwnerDraw MultiColumn ComboBox

Introduction

After submersing myself into the Internet in the endless search of a MultiColumn ComboBox, and trying for 2 years to develop one (in VB6), I have finally got something half-way decent. Some thanks goes out to Carlos H. Perez whose code has shown me how to do some very interesting things! There is not one routine though that has been "copied" from any Project out there, everything has been re-written (mainly because I wanted to learn) and optimized as best I knew how.

Background

The Multi-Column ComboBox is an Owner Drawn control derived from the System.Windows.Forms.ComboBox. However, the control does not use the standard "Items" array within the normal ComboBox. This is where this control excels. Since I was writing my own Control Suite, I needed a way for every Control that had a need for a Collection of Items to be universal among all Controls. Also, I hated Microsoft for taking away Keys from the Collection Classes...so I wrote my own using ideas from the CollectionGen utility. The Collections used within the ComboBox (and also the ListView shown above) are Strongly-Typed and very fast, this enables a fast lookup on the Control using either the Index or the Key of any Item.

One of the harder things to figure out was theHorizontalExtent of the drop-down List. When Items exceed the Horizontal Extent, the Horizontal Scrollbar should appear. I did not find anything anywhere on how to do this, so I did some hacking and came up with a very easy method using the CreateParams property of the control. By setting the WS_HSCROLL style bit, it gives the Control the ability (not the functionality) to have a Horizontal Scrollbar. The tough part is determining how much of a HorizontalExtent to give the drop-down list! I was able to set the HExtent by Overriding the OnDropDown Event of the ComboBox.

Calculating the Horizontal Extent

The Control needed an auto-magic way of Calculating the Horizontal Extent. Since I hate relying on Users to do the "right thing" (or other programmers in this case), I decided to make the Control "smart" enough to handle things where someone leaves off. When adding a ListColumn object to the Control (the Control NEEDS at least 1 ListColumn in order to Display data), if the programmer omits the "Width" Property, the Width will remain at "-1". By leaving the Column with a Width of -1, that Column's Width will be auto-magically calculated based on the Widest Text in that particular Column. If the Column is set to "Visible = False", then it's width will not be added into the Extent.

Using the code

The Control uses a very simple Object Model. It basically works the same way as the ListView does in "Details" mode. First, the ListColumns are added, then the ListItems are added. The ListItem Object supports a ListSubItems Collection which is used to access the other Columns within the Control (again, exactly like the ListView).

'Load the Form
Private Sub FormLoad(ByVal sender As Object, 
    ByVal e As System.EventArgs) Handles MyBase.Load
  With cboBase
    .ListColumns.Add("State ID") 'The Column's Width 
                                 'will be Automatically Calculated
    .ListColumns.Add("State Name") 
				
    For i As Integer = 0 To 19
      .ListItems.Add("Item" & i.ToString, "Item " & i.ToString)
      .ListItems(i).ListSubItems.Add("Item" & i.ToString & ":SubItem 0")
    Next i
  End With
End Sub

Points of Interest

Unfortunately, this control does not support DataBinding.  There is one thing that may be very Interesting though.  The Control has a Special Event "FocusChanging".  I needed an Event in one of my interfaces that told me which Direction the User was Tabbing through the Controls.  This allowed me to control which Tab got focus on a TabbedDialog control so that Data Entry would be a lot easier.  When the Event is Raised, it tells which Direction the User has Tabbed.  If you want to Handle that Event, and set Focus to another Control, simply tell the EventArgs that you Handled it (i.e. e.Handled = True). 

Private Sub cboBox3_FocusChanging(ByVal Sender As Object, 
       ByVal e As CodeSamples.Controls.FocusChangeEventArgs) 
       Handles cboBox3.FocusChanging
  If (e.Direction = CodeSamples.Controls.FocusDirection.Forward) Then
    e.Handled = True
    cboBox1.Focus()
  ElseIf (e.Direction = CodeSamples.Controls.FocusDirection.Backward) Then
    e.Handled = True
    cboBox4.Focus()
  End If
End Sub		

The other Point of Interest, is that the Control fully supports AutoTyping!  It will only allow Items to be Selected that are in the List.  The Good news is that in VS.NET 2003, Microsoft has FINALLY got the "e.Handled" Property of the "KeyPressEvents" working!  This makes it so that the AutoTyping feature does not flicker anymore!  I have also tried in the past to be able to set a Property so the Developer could choose which Column was Displayed in the Text area.  However, due to some weird functionality of the CombBox (and Inheriting from it), this will have to come when I re-write the entire ComboBox from scratch LOL!

History

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Tim McCurdy
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionBehave like normal ComboBox Pin
Member 111199757-Oct-14 3:42
memberMember 111199757-Oct-14 3:42 
QuestionThanks Pin
phalin27-Aug-14 18:20
memberphalin27-Aug-14 18:20 
GeneralItem Highlight FINALLY fixed! Pin
Tim McCurdy10-Feb-11 4:05
memberTim McCurdy10-Feb-11 4:05 
GeneralRe: Item Highlight FINALLY fixed! Pin
Member 766080210-Feb-11 20:43
memberMember 766080210-Feb-11 20:43 
Generalitem cant highlight while control change Pin
Member 76608029-Feb-11 20:22
memberMember 76608029-Feb-11 20:22 
GeneralRe: item cant highlight while control change Pin
Tim McCurdy10-Feb-11 4:05
memberTim McCurdy10-Feb-11 4:05 
GeneralUnable to Select and Item Pin
Daniel St.3-Oct-07 12:02
memberDaniel St.3-Oct-07 12:02 
GeneralRe: Unable to Select and Item Pin
Imdabaum3-Dec-10 9:24
memberImdabaum3-Dec-10 9:24 
GeneralRe: Unable to Select and Item Pin
Tim McCurdy3-Dec-10 15:50
memberTim McCurdy3-Dec-10 15:50 
Generalaspx Pin
WrongWayRookieNew18-Dec-06 12:39
memberWrongWayRookieNew18-Dec-06 12:39 
GeneralRe: aspx Pin
Tim McCurdy18-Dec-06 15:34
memberTim McCurdy18-Dec-06 15:34 
Generalwhen I use bounding from datatable. Pin
phob12-Sep-06 18:34
memberphob12-Sep-06 18:34 
GeneralTypo? in DrawGradient method in Painting.vb Pin
SBendBuckeye5-Jul-06 14:22
memberSBendBuckeye5-Jul-06 14:22 
GeneralRe: Typo? in DrawGradient method in Painting.vb Pin
Tim McCurdy7-Jul-06 7:15
memberTim McCurdy7-Jul-06 7:15 
GeneralDuplicate enumeration values Pin
SBendBuckeye5-Jul-06 13:15
memberSBendBuckeye5-Jul-06 13:15 
GeneralRe: Duplicate enumeration values Pin
Tim McCurdy7-Jul-06 7:13
memberTim McCurdy7-Jul-06 7:13 
GeneralMouse Over Highlight Pin
APM Consulting21-May-06 6:26
memberAPM Consulting21-May-06 6:26 
GeneralRe: Mouse Over Highlight Pin
Tim McCurdy31-May-06 11:20
memberTim McCurdy31-May-06 11:20 
GeneralRe: Mouse Over Highlight Pin
APM Consulting9-Jun-06 9:25
memberAPM Consulting9-Jun-06 9:25 
GeneralRe: Mouse Over Highlight Pin
_Thurein_24-Jul-06 1:35
member_Thurein_24-Jul-06 1:35 
GeneralRe: Mouse Over Highlight Pin
APM Consulting27-Jul-06 10:32
memberAPM Consulting27-Jul-06 10:32 
GeneralText in ComboBox Pin
Wu ZhiYuan4-Jan-06 16:02
memberWu ZhiYuan4-Jan-06 16:02 
GeneralRe: Text in ComboBox Pin
Tim McCurdy4-Jan-06 16:33
memberTim McCurdy4-Jan-06 16:33 
NewsAdvanced list box and combo box Pin
brett5630-Nov-05 5:40
memberbrett5630-Nov-05 5:40 
GeneralRe: Advanced list box and combo box Pin
darkbyte2316-Mar-07 17:39
memberdarkbyte2316-Mar-07 17:39 
QuestionHow to customise this coding with Macromedia Dreamweaver 2004 MX Pin
moraji23-Nov-05 17:53
membermoraji23-Nov-05 17:53 
GeneralHandling the Index Property Pin
GenoJoeInOhio18-Aug-05 16:55
memberGenoJoeInOhio18-Aug-05 16:55 
GeneralRe: Handling the Index Property Pin
Tim McCurdy20-Aug-05 7:52
memberTim McCurdy20-Aug-05 7:52 
GeneralNew Version Pin
Tim McCurdy27-May-05 3:18
memberTim McCurdy27-May-05 3:18 
GeneralDisplay from MultiColumn Pin
Oceania24-May-05 11:59
memberOceania24-May-05 11:59 
GeneralRe: Display from MultiColumn Pin
Oceania25-May-05 11:38
memberOceania25-May-05 11:38 
GeneralNeed some help.. I am new.. Pin
kekinlim24-Jan-05 22:35
memberkekinlim24-Jan-05 22:35 
GeneralNew Version Available Pin
Tim McCurdy25-Nov-04 4:28
memberTim McCurdy25-Nov-04 4:28 
GeneralRe: New Version Available Pin
Het210921-Jan-05 19:58
memberHet210921-Jan-05 19:58 
GeneralVietNamese ComboBox Auto DropDown and Auto Focus Item when typing Pin
HaiNguyenJC20-Nov-04 18:00
memberHaiNguyenJC20-Nov-04 18:00 
Questionheaders ? Pin
fdrebola5-Aug-04 22:37
memberfdrebola5-Aug-04 22:37 
AnswerRe: headers ? Pin
mikasa25-Nov-04 3:53
membermikasa25-Nov-04 3:53 
GeneralExpanding the drop down list view Pin
SupaHoopsa19-Jul-04 23:38
memberSupaHoopsa19-Jul-04 23:38 
GeneralRe: Expanding the drop down list view Pin
jatabar9-Sep-04 14:00
memberjatabar9-Sep-04 14:00 
QuestionImages? Pin
atomiton16-Jul-04 11:41
memberatomiton16-Jul-04 11:41 
AnswerRe: Images? Pin
mikasa2-Nov-04 9:08
membermikasa2-Nov-04 9:08 
QuestionHow about c# Pin
lewisv30-Mar-04 7:18
susslewisv30-Mar-04 7:18 
AnswerRe: How about c# Pin
sukesh.g26-May-04 19:24
membersukesh.g26-May-04 19:24 
GeneralHelp - Remove Item Pin
xvFaithvx11-Feb-04 2:57
memberxvFaithvx11-Feb-04 2:57 
GeneralRe: Help - Remove Item Pin
mikasa18-Feb-04 9:58
membermikasa18-Feb-04 9:58 
GeneralRe: Help - Remove Item Pin
Anonymous18-Feb-04 10:06
sussAnonymous18-Feb-04 10:06 
GeneralRe: Help - Remove Item Pin
sukesh.g19-Mar-04 23:24
membersukesh.g19-Mar-04 23:24 
GeneralRe: Help - Remove Item Pin
Tim McCurdy25-Nov-04 4:30
memberTim McCurdy25-Nov-04 4:30 
Generalhiya, intresting concept BUT Pin
RBlyth23-Jan-04 1:42
memberRBlyth23-Jan-04 1:42 
GeneralRe: hiya, intresting concept BUT Pin
mikasa24-Jan-04 9:06
membermikasa24-Jan-04 9:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 2 Jul 2003
Article Copyright 2003 by Tim McCurdy
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid