简体   繁体   中英

How to get the index of an items while Scrolling combobox in VB.net?

In VB6 I used comboboxes to make selections of known inputs. In order to update the selection as the the user scrolls through the list I used a timer and updated the selection every 250 interval of time. This was a very nice feature to the application that would show results of the different inputs of the combobox as the user scrolled through the list with the mouse.

In VB.net the same method does not work, I did change the code to Selectindex for VB.net. I can not find any examples on the vb.net where the selectionIndex is updated.

The real trick in the code is: I can use mousehover event to grab the event but not sure how to get the index while scrolling.

Thank you for your help! Mario

I can use mousehover event to grab the event but not sure how to get the index while scrolling.

See if this helps,

Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    ComboBox1.DrawMode = DrawMode.OwnerDrawFixed 'so .DrawItem fires
End Sub

Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
    'initially all items are drawn
    e.DrawBackground()
    e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString, e.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)
    If e.State = DrawItemState.Selected Then
        Debug.WriteLine(ComboBox1.Items(e.Index).ToString) '.Selected is item where mouse is hovering 
    End If
End Sub

Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles FluidHyres.DrawItem 'initially all items are drawn e.DrawBackground() e.Graphics.DrawString(FluidHyres.Items(e.Index).ToString, e.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)

    If e.State = DrawItemState.Selected Then
        FluidHyres.SelectedIndex = e.State
        Call CalculateHydraulicResistance()
        'Debug.WriteLine(FluidHyres.Items(e.Index).ToString) '.Selected is item where mouse is hovering 
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM