How to set the Starting Index on a combobox (or similar) control?

Denaes

Senior Contributor
Joined
Jun 10, 2003
Messages
956
When you load values into the ComboBox (I have it set to dropdown combolistbox) the control loads with a -1 (blank) value.

It seems extremely easy to inherit this control and add a property called "Default Index" which attempts to select that index after loading the items collection.

Is there a property somewere I'm missing? This seems so basic of a need that I can't see MS going through 3 iterations of VS (i'm working with 2005 now) and still not addressing this.

If not, I'll have to custom control it.

Thanks :)
 
Cags said:
I may be completely misunderstanding the question, but can't you just set the SelectedIndex property to select a specific item.

Yes you could. You would have to write this code for every single combo control you have and place it somewere in the form load (or in a procedure off of form load).

Sorry for not being more specific. I meant to say that I knew about that when I was talking about making my own control for doing it.

I'm specifically talking about a property to set on the existing control.

In fact, at work, the application I'm working on porting to .net has over a thousand comboboxes (on various screens, not just one!) and all of them are going to need to display information. Some are hidden & appear and some are dynamically created.

I can't think of many reasons not to have the first index selected and it seems like there should be a toggle (or choice of index) to select which way you want it.
 
One reason is with any style other than DropDownList, it is not only possible but very likely that what the combo box contains for text does not match any items in the list and has no index. You can't add/remove a property based on the value of another property. Either it is there or it isn't, and if it is, the behavior of the SelectedIndex property would be quirky in the event that the combo box isn't a DropDownList.

It's not a very good reason to not expose the SelectedIndex to the property grid, but it's the only one that I can think of. I certainly agree that you should be able to modify it at design time without inheriting the class and overriding or shadowing the property, but Microsoft appearently doesn't.
 
Ahh I understand what you mean now, you wish to change the value from the visual ide. As you point out there doesn't currently appear a way todo it. Lucky it shouldn't take you long to inherit the control and add the functionality, its just annoying todo. As for what marble_eater says, I can't see any real reason it would be quirky as long as the value setting the default is an integer. As you mention it is possible to set other styles to a value not in the list, but since the default value will only ever be set at design time, and is only ever likely to be a value in the list, this shouldn't cause any trouble (unless i'm missing the point somewhere which is quite possible).
 
Okie, in this scenario,
you can make a default selected index = 1 because when a combo is created, it has empty items in it.

First came to my mind, you could write your control with ,
Protected Overloads Overrides Sub Validating function with

If Me.Items.Count > 0 then
Me.SelectedIndex = 0
End If
I haven't test this code, hope this help
 
Cags said:
As for what marble_eater says, I can't see any real reason it would be quirky as long as the value setting the default is an integer. As you mention it is possible to set other styles to a value not in the list, but since the default value will only ever be set at design time, and is only ever likely to be a value in the list, this shouldn't cause any trouble (unless i'm missing the point somewhere which is quite possible).
The combo box has a text property which can be set at design time to anything you would like, not necessarily from the list. Just as an example of what I mean, also note that the Text property does not work entirely as one might expect (i.e. quirky) when the combo box's drop down style is DropDownList. You can type whatever you like, but when you hit enter, the text you entered disappears.
 
Thanks for the help.

I was hoping I was just overlooking something. I can't knock the job they did, they have lookup functionality which is cool - but it's still not multi-column supported.

As of right now I can't see a single reason not to build a control with the ability to set the index manually if one is entered. If there is a problem with a specific combo, just don't enter the value.

Seems rather iffy for databound combos, but for static combos (you'd be surprised how many of those are still out there) configured at least basically at designtime, I can't see a problem.

Along the way of my wishlist, I can't help but think that I wish they had included an "index changed" event that only happens after the combo is populated. It just seems wrong that every single combobox using an index changed (which is pretty common) will error out when the application boots unless you use a Try statement or create an application state variable.
 
Back
Top