i know in VB6 this line of code
put Zion at the top of the list at runtimeCode:With Combo1
.AddItem "Zion", 0
.AddItem "Boy"
.AddItem "Apple"
End With
How can i achieve this with VB.net
Printable View
i know in VB6 this line of code
put Zion at the top of the list at runtimeCode:With Combo1
.AddItem "Zion", 0
.AddItem "Boy"
.AddItem "Apple"
End With
How can i achieve this with VB.net
still waiting
Firstly, please don't add pointless posts to your thread. The fact that you haven't received a response within an hour of posting is not reason to add another post. We are scattered all over the world in different time zones and if there's no one around who has the time, expertise and inclination to answer your question then so be it. If you don't have any additional information to add or question to ask then please don't post because bumping threads is not allowed. Also, while you may not have intended it, when I read your second post I get an image of someone standing with their hands on their hips and tapping their foot like we have somehow failed in our responsibilities.
Anyway, in .NET, a ComboBox control has an Items property and, like all other collections, it is indexed from zero. That means that the first item you add will be at index 0, the second item you add will be index 1 and so on. If you already have items in the list and you want to add another at a specific index then, like other simple collections, you can call the Insert method of the Items collection instead of Add.
Does anybody know how to use the insert property of the combo box?
Like combobox1.Items.Insert(0, ("Choose One"))
NB: I just want to put or insert a text at specific place or index in a combo box
This works fine for me:
Explain "but not seem to work"Quote:
Me.ComboBox1.Items.Insert(0, ("Help0"))
Me.ComboBox1.Items.Insert(1, "Help1")
Me.ComboBox1.Items.Insert(2, "Help2")
Me.ComboBox1.Items.Insert(3, "Help3")
your going to have to share more information then
"your code seem not to work well when the combo box is sorted"
post your code
Suppose i have this line of code
NB: my aim is to keep 'Zion' on top of the list in the combo box afterCode:Me.ComboBox1.Items.Insert(0, ("Zion"))
Me.ComboBox1.Items.Insert(1, "Able")
Me.ComboBox1.Items.Insert(2, "Dan")
Me.ComboBox1.Items.Insert(3, "Moses")
ComboBox1.selectedindex=0
ComboBox1.sorted=true
the combo box is sorted.
Hope i made myself clear
Ok unless I am missing something i dont understand. You are sorting your items but want to keep an item that should at the bottom at the top
Thats like saying "take all these letters and put them in alphabetical order but dont put them in alphabetical order," why sort them to begin? why do you want Zion at the top? maybe that will help us understand what you are trying do?
how are you inserting them? one at a time?
Just like i want to keep 'Please Select'Quote:
Me.ComboBox1.Items.Insert(0, ("Please Select"))
Me.ComboBox1.Items.Insert(1, "Able")
Me.ComboBox1.Items.Insert(2, "Dan")
Me.ComboBox1.Items.Insert(3, "Moses")
ComboBox1.selectedindex=0
ComboBox1.sorted=true
on top of the list to tell users know what to do
ok well then you can set the selected index to 3 or more properly set the text property to "Please Select"
under properties of the combobox go to text and type what you want to it to say i.e. "Please Select" or you can set it in code like my example below
Then remove the line for selected index
when you program runs the text you put in the combobox text propertie will be displayed and you items will be listed in sort format of course you should not add the item you were previously adding to show first "Zion"
Quote:
Me.ComboBox1.Text = "Please Select"
Me.ComboBox1.Items.Insert(0, "Zion")
Me.ComboBox1.Items.Insert(1, "Able")
Me.ComboBox1.Items.Insert(2, "Dan")
Me.ComboBox1.Items.Insert(3, "Moses")
ComboBox1.Sorted = True
Of course it doesn't work when the ComboBox is sorted. That's common sense. The whole point of sorting the ComboBox is to put the items is alphabetical order. The item that comes first in that order will be at the top, regardless of the order the items were added in. Again, that's the whole point of sorting the ComboBox.
If you don't want the contents of the ComboBox sorted then don't sort the contents of the ComboBox. Sort the data before adding it to the ComboBox. You can then add it to the ComboBox and it will be in the right order but the ComboBox has no specific knowledge of that, so inserting an item at the top is then possible.
He is just trying to ge the combobox to display "Please Select" or something similiar to give the user an instruction and apparently unaware of the combobox text property. So he was wanting to have the first displayed item in the combobox his instruction i.e. "please select"
I think anyway
The Text property can only display something than one of the items if the DropDownStyle is set to DropDown rather than DropDownList, in which case the user can type anything they want into the ComboBox, which is generally undesirable.
Personally, I think that putting a prompt in a ComboBox is a stupid idea. Why is it that so many people seem to think that your average user is quite capable of typing text into a TextBox or clicking a Button without any prompting but when they encounter a ComboBox they suddenly lose all memory of how a GUI works and need elementary instructions?
I agree and was only trying to surmise what the OP was trying to accomplish
With that said a button text property would generally have something indicative of the buttons use such as
"Add" "Lookup" etc... and a text box might have a label above it to indicate what is expected to be entered so perhaps a label above the combobox is a better solution to indicate what the combobox is for? not sure without understanding the OP program and what they are trying to accomplish
I fully agree that it is not necessary to have the "Please Select" or other prompts in the Combo box. Any way that is my personal opinion.
A simple trick will do to put the "Please Select" in the top of the combo box, even when the combo box is sorted. Just add a space before "Please Select" like " Please Select". This will put the "Please Select" at the top. (heh,heh). However you have to ensure that there are no more "prefixed spaces" in the items being added. You can work on that...:)
thanks to all
Your welcome
Please mark the thread as Resolved
IS STILL NOT SOLVED
I guess is not possible
You can't specify the index and sort it at the same time. Sorting will always take precedence over the index. Delete, or comment out the 'ComboBox1.sorted=True' line, and it'll work...