Results 1 to 23 of 23

Thread: [RESOLVED] How to set Combo box Item index

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    Resolved [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

  2. #2

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    Re: How to set Combo box Item index

    still waiting

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    Re: How to set Combo box Item index

    Quote Originally Posted by jmcilhinney View Post
    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to set Combo box Item index

    Quote Originally Posted by KofiJeff View Post
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    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

  7. #7
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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"

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    Re: How to set Combo box Item index

    Quote Originally Posted by billboy View Post
    This works fine for me:



    Explain "but not seem to work"

    your code seem not to work well when the combo box is sorted

  9. #9
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  10. #10

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    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

  11. #11
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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?

  12. #12

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    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

  13. #13
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to set Combo box Item index

    Quote Originally Posted by billboy View Post
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    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

  18. #18
    Member
    Join Date
    Oct 2012
    Posts
    37

    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...

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to set Combo box Item index

    Quote Originally Posted by Edgemeal View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    Re: How to set Combo box Item index

    thanks to all

  21. #21
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: How to set Combo box Item index

    Your welcome

    Please mark the thread as Resolved

  22. #22

    Thread Starter
    Member
    Join Date
    Feb 2012
    Posts
    44

    Re: How to set Combo box Item index

    IS STILL NOT SOLVED
    I guess is not possible

  23. #23
    Junior Member
    Join Date
    May 2011
    Location
    Virginia Beach, VA
    Posts
    22

    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
  •  



Click Here to Expand Forum to Full Width