Results 1 to 6 of 6

Thread: RESOLVED - Scroll list portion of combobox programatically

  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.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Scroll list portion of combobox programatically

    How many entries is 'several'? You can just adjust the CB's MaxDropDownItems property so that scroll bars never appear!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

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

    Re: Scroll list portion of combobox programatically

    In some cases it could be 50 or 60, so if the scrolling thing is possible I'd like to know how it can be done.
    "It's cold gin time again ..."

    Check out my website here.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Scroll list portion of combobox programatically

    Fairy snuff. I'll have a think on the morrow.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Scroll list portion of combobox programatically

    I could only get it to work if I run the scroll code from a timer where there is at least a 100ms delay from the time the list is dropped and I tell the list to scroll up, tried it like this...

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Public Enum ComboBoxButtonState As Int32
            STATE_SYSTEM_NONE = 0
            STATE_SYSTEM_INVISIBLE = &H8000
            STATE_SYSTEM_PRESSED = &H8
        End Enum
    
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RECT
            Public Left, Top, Right, Bottom As Int32
        End Structure
    
        <StructLayout(LayoutKind.Sequential)> _
            Public Structure COMBOBOXINFO
            Public cbSize As Int32
            Public rcItem As RECT
            Public rcButton As RECT
            Public buttonState As ComboBoxButtonState
            Public hwndCombo As IntPtr
            Public hwndEdit As IntPtr
            Public hwndList As IntPtr
        End Structure
    
        <DllImport("user32.dll")> _
        Public Shared Function GetComboBoxInfo(ByVal hWnd As IntPtr, ByRef pcbi As COMBOBOXINFO) As Boolean
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
        End Function
    
        Private Const WM_HSCROLL As Int32 = &H114
        Private Const WM_VSCROLL As Int32 = &H115
        Private Enum ScrollType As Int32
            SB_LINEUP = 0
            SB_LINEDOWN = 1
            SB_TOP = 6
            SB_BOTTOM = 7
        End Enum
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 100
        End Sub
    
        Private Sub ComboBox1_DropDown(sender As Object, e As System.EventArgs) Handles ComboBox1.DropDown
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            Dim cbi As New COMBOBOXINFO
            cbi.cbSize = Marshal.SizeOf(cbi)
            If GetComboBoxInfo(ComboBox1.Handle, cbi) Then ' scroll combo list to top
                SendMessage(cbi.hwndList, WM_VSCROLL, ScrollType.SB_TOP, 0)
            End If
        End Sub
    
    End Class
    parts from,
    http://www.pinvoke.net/default.aspx/...boBoxInfo.html

  6. #6

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

    Re: Scroll list portion of combobox programatically

    Edgemeal - thanks very much - your solution worked well for me.
    "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