Adding Blank line to Combo Box

Bigbeanpole

Member
Joined
Jul 7, 2005
Messages
15
Programming Experience
1-3
Adding Blank line to Combo Box - RESOLVED

I know how to do this manually, but I'm trying to add a blank line to a combobox that is being populated by a DataSource. I know I need to add a blank row to the datatable prior to binding it (or perhaps, after would work as well?) however, I'm at a loss for how to accomplish this. :(

Any/all helps/suggestions would be greatly appreciated!
 
Last edited:
VB.NET:
Dim[/color][/size][size=2] i [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer

[/color][/size][size=2][/size][size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] 13

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ComboBox1.Items.Add("item " & i)

[/size][size=2][color=#0000ff]Next

[/color][/size][size=2][/size][size=2][color=#0000ff]Me[/color][/size][size=2].ComboBox1.Items.Add([/size][size=2][color=#0000ff]string[/color][/size][size=2].Empty)[/size]
[size=2][color=#0000ff]

or
VB.NET:
[/color][/size]
[size=2][size=2][color=#0000ff]Dim[/color][/size][size=2] i [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size]
[size=2][color=#0000ff][size=2][color=#0000ff]Me[/color][/size][size=2].ComboBox1.Items.Add([/size][size=2][color=#0000ff]""[/color][/size][size=2])

[/size][/color][/size][size=2][/size][size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] 13

[/size][size=2][color=#0000ff]Me[/color][/size][size=2].ComboBox1.Items.Add("item " & i)

[/size][size=2][color=#0000ff]Next

Cheers ;)

[/size]
 
Can I just say that I think adding a blank line to a ComboBox is a bad idea. When the user selects that item then they are selecting an empty string. If that is what you want, then great. If you are actually trying to indicate that they have made no selection, you should set the SelectedIndex to -1. The user cannot do this themselves, but I usually add this code in this situation:
VB.NET:
	Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
		If e.KeyCode = Keys.Delete Then
			'Clear the selection.
			Me.ComboBox1.SelectedIndex = -1
		End If
	End Sub
This allows the user to clear the ComboBox with the DELETE key.
 
Hi,
Only a little suggestion if i may ... the code Me.comboBox.SelectedIndex = -1 could throw an exception beside the fact that M$ is claiming that this "bug" has been resolved with FW v1.1

You can find more about this here
http://support.microsoft.com/default.aspx?scid=kb;en-us;327244

Btw, i think actually, i guess that original threader doesn't want to accomplish this to empty certain control (if it was so he would ask how to get empty combobox text or something Me.comboBox.Text = String.Empty btw) but rather he maybe wanted to add a blank line after each row within comboBox. However it was just guessing.


Cheers ;)
 
yeah i am gussing what he wants, and i am also looking a solution but not a for each loop :)
Actually blank line does not mean there will be no contents, we are facing similar sitution so i will explain u bit more, we need to add at start an item with text (------) which is surely not coming from database. but if u put DataSource with combobox then we have to leave this particular item (one solution to add this as dummy row but surely it is realy bad one to do this ) so any suggesstion!!!!
 
Sorry about the delay folks. I was out of touch this weekend.


Thank you to all who replied. I am indeed looking for a way to "blank" the initial selection of the combo box. I tried an on form event of selected index = -1 (even put it in there twice, as I remember hearing that was a slight bug awhile back, and looking at the link kulrom left, my previous knowledge was correct :)) and it didn't do what I needed it to do. I think it didn't work that way because I am sorting the dataset, and I noticed when you sort, it loads the combo box, then performs the sort (or at least, in my case it is).

If anybody can direct me on how to sort/filter a combo box when the form loads without the selection changing (thus rendering the selected index=-1 useless), I would be greatly appreciative!
 
Unfortunately, kulrom, post #2 won't work because I have the source attached via the datasource method. VB complains that I can't edit the itme list if it's attached in that way.


Modifying the datasource does seem to work, although I'm finding a different problem now.
I have an overloaded sub that will populate my combo box (I have one method that simply populates the combo box, one that populates it and sorts it, and one that populates, sorts and filters it.). It seems almost as if the combo box forgets what it's display members are, because unless I reset the displaymember (and value member for that matter) within the calling sub (in this case, still working in the form load event), it shows the valuemember instead of the displaymember. :|

Thank you in advance for any assistance that can be provided
 
jmcilhinney said:
When you were calling SelectedIndex = -1, were you doing it after the ComboBox was populated and the data sorted? If not, then it will have no effect.

I have the following in my code:
VB.NET:
PopulateComboBox(myComboBox,myDataSource,mySortString,myFilterString)
myComboBox.SelectedIndex = -1

The above code doesn't work as expected (which is to say, doesn't select an empty string. I have all of this in my form load. Could that be the reason it doesn't work properly?)
 
Back
Top