Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding text from Text box to List box

Status
Not open for further replies.

Ca1icoJack

IS-IT--Management
Nov 27, 2008
36
GB
I have a userform which has a text box, a list box, an add button, a remove button and a start button. I want to be able to type text into the text box, hit enter to add it to the list box and type in my next addition.

I have each item being added at the top of the list, and want the focus of the list box to be on this item (currently it tracks the bottom item, scrolling away from my latest one).

If it is not possible to hit enter between entering characters I can use the button.

How can I do this? (Especially preventing the list box from automatically scrolling downwards)

Thanks in advance
 

What did you try so far? And where in your code you have any problems?

Have fun.

---- Andy
 
I want to be able to type text into the text box, hit enter to add it to the list box and type in my next addition. "

Hitting Enter does one of two things. Either it makes a new line in the textbox, or it moves focus to the next control in the TabOrder.
Code:
Sub cboAdd_Click()
ListBox1.AddItem TextBox1.Text
With TextBox1
   .Text = ""
   .SetFocus
End With
End Sub
This is code for an Add button. Typing text in Textbox1, and then Enter moves focus to this Add button. TabIndex being: Textbox1 TabIndex = 0 (first), cboAdd TabIndex = 1 (second).

The code of the Add button takes the text of TextBox1, adds it to Listbox1, clears Textbox1, and returns focus back to TextBox1 (for the next addition).

"I have each item being added at the top of the list, and want the focus of the list box to be on this item"

Using AddItem puts the new item at the bottom, not the top. What do you mean by "focus of the listbox"? There is no focus until you click on the control, and you can click on any visible item.

faq219-2884

Gerry
My paintings and sculpture
 
Using AddItem puts the new item at the bottom, not the top.
Gerry, what about this ?
ListBox1.AddItem TextBox1.Text[!], 0[/!]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

That will get you started:
Code:
Option Explicit

Private Sub cmdAdd_Click()

ListBox1.AddItem TextBox1, 0
ListBox1.Selected(0) = True
TextBox1.Text = ""
TextBox1.SetFocus

End Sub

Private Sub TextBox1_Change()
cmdAdd.Enabled = Len(Trim(TextBox1.Text))
End Sub

Private Sub UserForm_Initialize()
cmdAdd.Enabled = False
End Sub

Have fun.

---- Andy
 
<joking...>You are so annoying PHV.</joking...>

OK...but just to give you a hard time....if you comment you should give the whole story.

BY DEFAULT, AddItem puts the new item at the bottom, in that the arguments for AddItem are:

AddItem [item], [varIndex]

and that BY DEFAULT varIndex = control.ListCount, in that:

ListBox1.AddItem TextBox1.Text

is the same as:

ListBox1.AddItem TextBox1.Text, ListBox1.ListCount

And further, that you can place an AddItem anywhere in the list (although of course not greater than ListCount).

ListBox1.AddItem TextBox1.Text, 5

places the new AddItem as the 6th item.

faq219-2884

Gerry
My paintings and sculpture
 
Didn't we resolve Ca1icoJack's thread707-1517161 problem several days ago ...

And, and joining in the 'whole story' thing,

BY DEFAULT varIndex = control.ListCount

is incorrect. By default varIndex actually = -1 (which happens to have the same effect as if it equalled control.ListCount). Actually, technically it doesn't have a default value at all. However, if it is missing this causes VBA to pass -1 as the relevant parameter in the underlying LB_INSERTSTRING API message that is sent to the control

 
Thanks for the info about returning focus to the textbox. Pressing enter neither seems to move the focus to the next control or add an extra line in the text box...

What I meant by 'focus' is that when the list box is filled it begins to scroll rather than always display the most recently added item. I've tried all your suggestions but none seem to help this.

I did have another thread, that was about how to add an item to the top row, not how to use the enter key to add text or how to prevent the listbox from scrolling, I guess I should've added this to that thread...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top