Loop in Combobox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan V via DotNetMonster.com

    #1

    Loop in Combobox

    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


    --
    Message posted via http://www.dotnetmonster.com
  • Cor Ligthert [MVP]

    #2
    Re: Loop in Combobox

    Ivan,
    [color=blue]
    > 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!
    >[/color]
    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


    Comment

    • Al Reid

      #3
      Re: Loop in Combobox

      "Ivan V via DotNetMonster.c om" <forum@DotNetMo nster.com> wrote in message news:525516B531 3C8@DotNetMonst er.com...[color=blue]
      > 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
      >
      >
      > --
      > Message posted via http://www.dotnetmonster.com[/color]

      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_KeyDo wn(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles ComboBox1.KeyDo wn

      Dim cbo As ComboBox = DirectCast(send er, ComboBox)

      If e.KeyCode = Keys.Down Then

      If cbo.SelectedInd ex < cbo.Items.Count - 1 Then

      cbo.SelectedInd ex += 1

      ElseIf cbo.SelectedInd ex = cbo.Items.Count - 1 Then

      cbo.SelectedInd ex = 0

      End If

      e.Handled = True

      ElseIf e.KeyCode = Keys.Up Then

      If cbo.SelectedInd ex > 0 Then

      cbo.SelectedInd ex -= 1

      ElseIf cbo.SelectedInd ex = 0 Then

      cbo.SelectedInd ex = 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.

      --
      Al Reid


      Comment

      • Phill.  W

        #4
        Re: Loop in Combobox

        "Ivan V via DotNetMonster.c om" <forum@DotNetMo nster.com> wrote in message
        news:525516B531 3C8@DotNetMonst er.com...[color=blue]
        > 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.[/color]

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

        Private Sub Combo1_KeyUp(se nder ..., 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.


        Comment

        Working...