Loop in Combobox

  • Thread starter Thread starter Ivan V via DotNetMonster.com
  • Start date Start date
I

Ivan V via DotNetMonster.com

Hi All:

I would like to know if I can loop my selection in a combobox. For
instance, I got 8 items in a combobox, and when I selected it using the up
and down arrow, it will stop in the first record even I keep on press the up
arrow, on the other hand, if it's the last selection, it will stop in the
last record even I keep on press the down arrow. Is there any way that if I
keep on press the up or down arrow, the slection in it will keep on moving
such as if i am at the first record, and I press the up asrrow, then it will
goto the last selection record, another way is, if i am at the last record
and I keep on press the down arrow, it will goback to the first record. Hope
someone can help me out!

Best rgds,
Ivan Vong
 
Ivan,
I would like to know if I can loop my selection in a combobox. For
instance, I got 8 items in a combobox, and when I selected it using the up
and down arrow, it will stop in the first record even I keep on press the
up
arrow, on the other hand, if it's the last selection, it will stop in the
last record even I keep on press the down arrow. Is there any way that if
I
keep on press the up or down arrow, the slection in it will keep on moving
such as if i am at the first record, and I press the up asrrow, then it
will
goto the last selection record, another way is, if i am at the last record
and I keep on press the down arrow, it will goback to the first record.
Hope
someone can help me out!
What do you mean with a down and with an up arrow from a combobox?

I can also not understand how you would achieve this with a combobox, with a
listbox I can understand it with some extra buttons on a form or with an
extra scrollbar

Cor
 
Ivan V via DotNetMonster.com said:
Hi All:

I would like to know if I can loop my selection in a combobox. For
instance, I got 8 items in a combobox, and when I selected it using the up
and down arrow, it will stop in the first record even I keep on press the up
arrow, on the other hand, if it's the last selection, it will stop in the
last record even I keep on press the down arrow. Is there any way that if I
keep on press the up or down arrow, the slection in it will keep on moving
such as if i am at the first record, and I press the up asrrow, then it will
goto the last selection record, another way is, if i am at the last record
and I keep on press the down arrow, it will goback to the first record. Hope
someone can help me out!

Best rgds,
Ivan Vong

Assuming you have a ComboBox named ComboBox1 on a form and it has items in the Items collection, I think this may give you a start.

/////
Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown

Dim cbo As ComboBox = DirectCast(sender, ComboBox)

If e.KeyCode = Keys.Down Then

If cbo.SelectedIndex < cbo.Items.Count - 1 Then

cbo.SelectedIndex += 1

ElseIf cbo.SelectedIndex = cbo.Items.Count - 1 Then

cbo.SelectedIndex = 0

End If

e.Handled = True

ElseIf e.KeyCode = Keys.Up Then

If cbo.SelectedIndex > 0 Then

cbo.SelectedIndex -= 1

ElseIf cbo.SelectedIndex = 0 Then

cbo.SelectedIndex = cbo.Items.Count - 1

End If

e.Handled = True

End If

End Sub
\\\\\\

I'm sure you will need to clean it up abit and tailor it to suite your needs, but it's a start.
 
Ivan V via DotNetMonster.com said:
if i am at the first record, and I press the up asrrow, then it will goto
the last selection record, another way is, if i am at the last record and
I keep on press the down arrow, it will goback to the first record.

Try handling the KeyUp Event on the ComboBox : (air-code)

Private Sub Combo1_KeyUp(sender ..., e ...) _
Handles Combo1.KeyUp

With Combo1
If e.KeyCode = Keys.Up _
And .SelectedIndex = 0 _
Then
.SelectedIndex = .Items.Count - 1
e.Handled = True

ElseIf e.KeyCode = Keys.Down _
And .SelectedIndex = .Items.Count - 1 _
Then
.SelectedIndex = 0
e.Handled = True

End If

End With

End Sub

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top