|
-
Dec 23rd, 2012, 03:48 PM
#1
Thread Starter
Member
[RESOLVED] How to set Combo box Item index
i know in VB6 this line of code
Code:
With Combo1
.AddItem "Zion", 0
.AddItem "Boy"
.AddItem "Apple"
End With
put Zion at the top of the list at runtime
How can i achieve this with VB.net
-
Dec 23rd, 2012, 04:52 PM
#2
Thread Starter
Member
Re: How to set Combo box Item index
-
Dec 23rd, 2012, 06:45 PM
#3
Re: How to set Combo box Item index
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.
-
Dec 24th, 2012, 12:43 PM
#4
Thread Starter
Member
Re: How to set Combo box Item index
 Originally Posted by jmcilhinney
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.
That's why i came here
i tried something like this
combobox1.Items.Insert(0, ("Choose One"))
but seem not to work
-
Dec 25th, 2012, 06:55 AM
#5
Re: How to set Combo box Item index
 Originally Posted by KofiJeff
That's why i came here
i tried something like this
combobox1.Items.Insert(0, ("Choose One"))
but seem not to work
Sorry, I seem to have missed where you explained that to us in your first post. I'm guessing that you have bound a list, e.g. a DataTable to the ComboBox in order to populate it in the first place. Am I right?
-
Dec 27th, 2012, 02:46 PM
#6
Thread Starter
Member
Re: How to set Combo box Item index
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
-
Dec 27th, 2012, 03:32 PM
#7
Frenzied Member
Re: How to set Combo box Item index
This works fine for me:
Me.ComboBox1.Items.Insert(0, ("Help0"))
Me.ComboBox1.Items.Insert(1, "Help1")
Me.ComboBox1.Items.Insert(2, "Help2")
Me.ComboBox1.Items.Insert(3, "Help3")
Explain "but not seem to work"
-
Dec 27th, 2012, 04:37 PM
#8
Thread Starter
Member
Re: How to set Combo box Item index
 Originally Posted by billboy
This works fine for me:
Explain "but not seem to work"
your code seem not to work well when the combo box is sorted
-
Dec 27th, 2012, 05:30 PM
#9
Frenzied Member
Re: How to set Combo box Item index
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
-
Dec 27th, 2012, 05:46 PM
#10
Thread Starter
Member
Re: How to set Combo box Item index
Suppose i have this line of code
Code:
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
NB: my aim is to keep 'Zion' on top of the list in the combo box after
the combo box is sorted.
Hope i made myself clear
-
Dec 27th, 2012, 06:05 PM
#11
Frenzied Member
Re: How to set Combo box Item index
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?
-
Dec 27th, 2012, 06:24 PM
#12
Thread Starter
Member
Re: How to set Combo box Item index
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
Just like i want to keep 'Please Select'
on top of the list to tell users know what to do
-
Dec 27th, 2012, 06:56 PM
#13
Frenzied Member
Re: How to set Combo box Item index
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"
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
-
Dec 27th, 2012, 07:18 PM
#14
Re: How to set Combo box Item index
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.
-
Dec 27th, 2012, 07:56 PM
#15
Frenzied Member
Re: How to set Combo box Item index
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
-
Dec 27th, 2012, 08:25 PM
#16
Re: How to set Combo box Item index
 Originally Posted by billboy
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?
-
Dec 27th, 2012, 08:37 PM
#17
Frenzied Member
Re: How to set Combo box Item index
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
-
Dec 27th, 2012, 10:00 PM
#18
Member
Re: How to set Combo box Item index
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...
-
Dec 27th, 2012, 10:28 PM
#19
Re: How to set Combo box Item index
 Originally Posted by Edgemeal
I don't think insert would work if the combo is set to be sorted.
I'd just use a cuebanner instead, see remarks on using EM_SETCUEBANNER message.
example,..
Code:
Imports System.Runtime.InteropServices
Public Class Form1
#Region "SETCUEBANNER"
Private Const EM_SETCUEBANNER As Integer = &H1501
<DllImport("user32.dll", EntryPoint:="SendMessageW")>
Private Shared Function SendMessageW(hWnd As IntPtr, Msg As UInt32, wParam As Boolean, <MarshalAs(UnmanagedType.LPWStr)> lParam As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function
Private Sub SetCueBanner(ByVal control As Control, ByVal text As String)
If TypeOf control Is ComboBox Then
' get handle to the "edit" control in combobox control
Dim edit_hWnd As IntPtr = FindWindowEx(control.Handle, IntPtr.Zero, "Edit", Nothing)
If Not edit_hWnd.Equals(IntPtr.Zero) Then
SendMessageW(edit_hWnd, EM_SETCUEBANNER, True, text)
End If
ElseIf TypeOf control Is TextBox Then
' get handle to textbox
SendMessageW(control.Handle, EM_SETCUEBANNER, True, text)
End If
End Sub
#End Region
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' set combo to sorted
ComboBox1.Sorted = True
'set combo cuebanner text
SetCueBanner(ComboBox1, "Please Select")
' add some items to combo
ComboBox1.Items.Add("Moses")
ComboBox1.Items.Add("Able")
ComboBox1.Items.Add("Dan")
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' add more items to combo
ComboBox1.Items.Add("Zebra")
ComboBox1.Items.Add("Mustang")
' Optional: reset combo index to display the cuebanner.
ComboBox1.SelectedIndex = -1
End Sub
End Class
That also only works if the DropDownStyle is set to DropDown, which means that the user can type anything into the control. Maybe there's a way to do it when the DropDownStyle is DropDownList but the is no edit window to send that message to.
-
Dec 28th, 2012, 05:01 AM
#20
Thread Starter
Member
Re: How to set Combo box Item index
-
Dec 28th, 2012, 01:19 PM
#21
Frenzied Member
Re: How to set Combo box Item index
Your welcome
Please mark the thread as Resolved
-
Dec 29th, 2012, 05:55 AM
#22
Thread Starter
Member
Re: How to set Combo box Item index
IS STILL NOT SOLVED
I guess is not possible
-
Dec 29th, 2012, 10:11 AM
#23
Junior Member
Re: How to set Combo box Item index
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|