How to associate SpinButton to a listbox?

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
Hello,

I have a form with a listbox. It has some properties and, using a "textbox_change", a array and a button "finish", I can save values to that properties in a sheet.

To facilitate handling, without having to keep clicking all the time in the listbox, I created a SpinButton and it's working - I use spinbutton1_SpinUp and _SpinDown, ex:

Code:
Private Sub SpinButton1_SpinUp()
    If ListBox1.ListIndex + 1 < ListBox1.ListCount Then
        ListBox1.Selected(ListBox1.ListIndex + 1) = True
    End If
    TextBox1.SetFocus
End Sub

However, I wish every time we type "enter", it goes to the next item on listbox. I tried, but I don't know how to call "_SpinDown" inside "Textbox1_enter()".

There must be a better way to solve it.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I don't get that behavior when I try what you describe. My guess is that either something in your textbox_change event handler or some other textbox event handler is altering the listindex. If you're sure that's not the case then have a look at the tab order of your controls and which are or are not tab stops. You may inadvertantly be "clicking" another control.

If it's still a mystery, then set a breakpoint at the beginning of the textbox1_change event handler and step through the code. Before you do that - SAVE YOUR WORKBOOK. I cannot count how many times I have crashed Excel when stepping through code related to userform control event handlers. Things can get very, very twitchy.
 
Upvote 0
Shoulda added, when you get to the END SUB statement of the event handler that you're stepping through, hit the F5 key, not the F8 key. Otherwise you'll end up stepping through EVERY event handler.

Also, when you do this, you can set a watch on USERFORM1.LISTBOX.LISTINDEX (edit the userform name as appropriate) and birddog the listindex to see what's going on.
 
Upvote 0
Thanks, I've changed the TabOrder and discovered that listbox1_change was altering somethings. Now, when I type "Tab" and "left or right", I can move, I think it's almost enough.

Just a last problem:

It's working:

Code:
Private Sub SpinButton1_Change()
If SpinButton1.Value > ListBox1.ListCount Then
           SpinButton1.Value = ListBox1.ListCount
End If          
If ListBox1.ListCount > SpinButton1.Value And SpinButton1.Value >= 0 And SpinButton1.Value <= ListBox1.ListCount Then
         ListBox1.ListIndex = SpinButton1.Value     
End If     
TextBox1.SetFocus      
End Sub

and

Code:
Private Sub ListBox1_Change()
    SpinButton1.Value = ListBox1.ListIndex
    TextBox1.Text = MyArray(ListBox1.ListIndex, 2)
    TextBox1.SetFocus
End Sub
But, TextBox1.SetFocus is not working. I don't know why, it works just each two times I click on spinbutton, not every time I click.
 
Last edited:
Upvote 0
Typically in a situation like that I set some debug.print statements in the event handlers - something like:
Code:
debug.print "textbox1_change", me.textbox1.text
or
Code:
debut.print "spinbutton1_change", me.spinbutton.value
and use that to try and trace the flow of the event handlers as I manipulate controls.
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top