Results 1 to 6 of 6

Thread: RESOLVED - Scroll list portion of combobox programatically

Threaded View

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Resolved RESOLVED - Scroll list portion of combobox programatically

    Hello all. Hopefully I can get some help on this.
    I have a combobox called cboSelTags. The items are populated from a database table, however before I populate those database items, I add my own entry:
    Code:
    cboSelTags.Items.Add("(New)")
    So Index 0 is "(New)" and the regular entries are Indexes 1 thru however many items were in the table.
    I always default the selection to the first entry, so after I populate the combobox, I have this code:
    Code:
            If cboSelTags.Items.Count > 1 Then
                    cboSelTags.SelectedIndex = 1
            Else
                cboSelTags.SelectedIndex = 0
            End If
    Now here's the problem. The client wants the default selection to be the first database item, so that part is fine as I have it - but - when the list is dropped down the for the first time, he still wants to see the "(New)" entry. We will see the (New) entry if the list is relatively small and does not require the list portion to grow a scroll bar - however - if there several items in the list and there is a scroll bar, you have to SCROLL UP to see the "(New)" entry (in other words, the first item you see when you drop the list down is that first database item (the selected one) and you have to scroll up one entry to see "(New)".

    I'd like to be able to do that "scroll up" automatically. I have searched around and could not find a solution. One thing that looked promising was a VB6 example using APIs that could get the handle for the list portion of the combobox (I realize the handle for the list portion of the combobox is different from the handle of the combobox itself). I converted that code to VB.NET and tried to adapt it, but I can't get it to work (it doesn't blow up, it just seems to have no effect). BTW, I was never a rock star with APIs. This is what I have at the moment:
    Code:
        Private Structure RECT
            Dim Left As Integer
            Dim Top As Integer
            Dim Right As Integer
            Dim Bottom As Integer
        End Structure
    
        Private Structure COMBOBOXINFO
            Dim cbSize As Integer
            Dim rcItem As RECT
            Dim rcButton As RECT
            Dim stateButton As Integer
            Dim hwndCombo As Integer
            Dim hwndEdit As Integer
            Dim hwndList As Integer
        End Structure
    
        Private Declare Function GetComboBoxInfo Lib "user32" (ByVal hwndCombo As Integer, ByRef CBInfo As COMBOBOXINFO) As Integer
        Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As Object) As Integer
        Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Integer, ByVal lpString As String) As Integer
    
    
       Private Sub cboSelTags_DropDown(sender As Object, e As System.EventArgs) Handles cboSelTags.DropDown
    
            Const WM_SCROLL As Integer = &H115S
    
            If cboSelTags.SelectedIndex = 1 Then
                Dim hList As Integer
                hList = GetComboListHandle(cboSelTags)
                SendMessage(hList, WM_SCROLL, 0, VariantType.Null)
            End If
    
        End Sub
    
        Private Function GetComboListHandle(ByRef ctl As System.Windows.Forms.ComboBox) As Integer
            Dim CBI As COMBOBOXINFO
            CBI.cbSize = Len(CBI)
            Call GetComboBoxInfo(ctl.Handle.ToInt32, CBI)
            GetComboListHandle = CBI.hwndList
        End Function
    Any help on this, using the APIs or not, would be much appreciated ...
    Last edited by BruceG; Sep 26th, 2013 at 11:23 PM. Reason: resolved
    "It's cold gin time again ..."

    Check out my website here.

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