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

Time:12-26

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.

CodePudding user response:

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

CodePudding user response:

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
  • Related