PDA

View Full Version : Set focus in a list box



Djblois
01-11-2007, 02:04 PM
I am programmatically adding text to a list box. (The list box is used to name a report the user is using - and the text I am adding is a suggested name for the report) However, if the customer wants to type his own name I want him to just start typing. So, in other words I want the text to be highlighted and when someone starts typing it automatically replaces the old text with the new text. Is this possible?

Tommy
01-11-2007, 03:07 PM
Hi Djblois,

This sub will select all text in the textbox.


Private Sub TextBox1_Enter()
TextBox1.SelLength = Len(TextBox1.Text)
End Sub


HTH

Djblois
01-11-2007, 03:21 PM
Can I use that to select it right when the Userform is activated?

Tommy
01-11-2007, 03:48 PM
I tried this and it worked. When I saw the form the text was highlighted :)
Private Sub UserForm_Initialize()
TextBox1.Text = "FRIED TATERS"
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End Sub

lucas
01-11-2007, 09:14 PM
TextBox1.Text = "FRIED TATERS"

:rotlaugh: Hi Tommy.....:hi:

Djblois
01-12-2007, 07:04 AM
This is what I have now:

Me.ReportName.Value = Me.Caption
'Me.ReportName.SetFocus
'Me.ReportName.SelStart = 0
Me.ReportName.SelLength = Len(Me.ReportName.text)

I have tried with different lines commented out and I can't get it to work. The cursor just starts after the text.

Tommy
01-12-2007, 07:38 AM
Hey Steve :hi:

You should see the names I use for subs/functions :devil2:

Djblois,
I used the below posted code and the text was high lighted. For some reason the .SelLength does not work unless the .SelStart is set first. :dunno
Private Sub UserForm_Initialize()
Me.ReportName.Value = Me.Caption 'I would have used .Text here
Me.ReportName.SelStart = 0
Me.ReportName.SelLength = Len(Me.ReportName.Text)
End Sub

If you are still having trouble could you upload the file so I can look at it?

Djblois
01-12-2007, 08:06 AM
I tried it that way also. I also tried with .text instead of .value and it still doesn't work. All it does is start the cursor at the end of the text instead of at the beginning of the text.

lucas
01-12-2007, 08:34 AM
Daniel, are you running this on a textbox or a listbox....? your first post indicates that your trying to type into a listbox....

Tommy
01-12-2007, 09:24 AM
My Bad :mkay
Private Sub UserForm_Initialize()
Me.ReportName.AddItem Me.Caption
Me.ReportName.Selected(0) = True
End Sub


Thanks Steve :)

Djblois
01-12-2007, 09:28 AM
My bad I mean a textbox not a list box

Tommy
01-12-2007, 09:43 AM
listbox with name ReportName


Private Sub UserForm_Initialize()
Me.ReportName.AddItem Me.Caption
Me.ReportName.Selected(0) = True
End Sub


TextBox with name ReportName



Private Sub UserForm_Initialize()
Me.ReportName.Value = Me.Caption
Me.ReportName.SelStart = 0
Me.ReportName.SelLength = Len(Me.ReportName.Text)
End Sub


So now I'm confused, can you upload a sample? The sample I have uploaded has a textbox with the name of TextBox1 and a listbox namd ReportName. When you see the form both sets of text will be highlighted.

lucas
01-12-2007, 10:43 AM
That seems to work great Tommy....just start typing then hit enter..enter data and hit enter again...etc.

malik641
01-12-2007, 09:44 PM
I used the below posted code and the text was high lighted. For some reason the .SelLength does not work unless the .SelStart is set first. :dunno
Private Sub UserForm_Initialize()
Me.ReportName.Value = Me.Caption 'I would have used .Text here
Me.ReportName.SelStart = 0
Me.ReportName.SelLength = Len(Me.ReportName.Text)
End Sub That's because when you place text in the Me.ReportName textbox, it automatically sets the .SelStart to Len("TextYouEntered"). So you have to reset it to get the .SelLength to work correctly :thumb

Step through the code with a watch window on Me.ReportName.SelStart and you'll see what I mean. Thanks for bringing that up, it was fun testing :)

Good to see you Tommy, BTW :hi:

Tommy
01-15-2007, 07:50 AM
Hey BTW,

Almost missed you there.


it automatically sets the .SelStart to Len

Thank you, that makes sense for a change :) I as usually don't think about why, just how to fix it. ROFL

malik641
01-15-2007, 09:01 AM
No problem :thumb Always glad to help.

Djblois
01-17-2007, 11:18 AM
I tried the sample workbook and it works perfectly. But when I use the code for me, It doesn't work. This is all the code I have behind the userform, maybe something else is messing it up?

Option Explicit
Dim col_Selection As New Collection
Private Function fnKeyPressFilter(ByVal KeyAscii As Integer, _
AcceptCharacters As String) As Integer
'// Accept the Delete Key to enable Editing!
If KeyAscii = 8 Then Exit Function
'// Is this Key in the list of characters to deny
If InStr(1, AcceptCharacters, Chr$(KeyAscii)) > 0 Then
fnKeyPressFilter = 0
Beep
Else
'// Must be OK
fnKeyPressFilter = KeyAscii
End If

End Function
Private Sub CheckBox1_Click()
End Sub
Private Sub CheckBox2_Click()
End Sub
Private Sub Column_Click()
End Sub
Public i As Integer
Private Sub CommandButton1_Click()
Dim c

Application.ScreenUpdating = True
PivotTableOptions.Hide
DoEvents

i = 0
For Each c In PivotTableOptions.DataView.Controls
If TypeName(c) = "CheckBox" Then
If c.Value = True Then i = i + 1
End If

Next c

Application.ScreenUpdating = False

End Sub
Private Sub CommandButton2_Click()
PivotTableOptions.Hide
End
End Sub
Private Sub Reportname_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If fnKeyPressFilter(KeyAscii, "/\'[*") = 0 Then KeyAscii = 0

End Sub
Private Sub UserForm_Initialize()
Dim ctl As Control
Dim chb_ctl As clsFormEvents

Me.Reportname.text = Me.Caption
'Me.Reportname.SetFocus
Me.Reportname.SelStart = 0
Me.Reportname.SelLength = Len(Me.Reportname.text)

'Go through the checkboxes and add them to the frame
Set col_Selection = New Collection
For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
Set chb_ctl = New clsFormEvents
Set chb_ctl.chb = ctl
col_Selection.Add chb_ctl
End If
Next ctl

End Sub
Private Sub InfoSelect_Click()
Dim ctl As clsFormEvents
For Each ctl In col_Selection
ctl.selectall
Next ctl
End Sub

Private Sub InfoUnselect_Click()
Dim ctl As clsFormEvents
For Each ctl In col_Selection
ctl.unselectall
Next ctl
End Sub

Tommy
01-17-2007, 02:16 PM
Try this, I think it is because you are working on other controls and when the form is seen another control has the focus.
Private Sub UserForm_Initialize()
Dim ctl As Control
Dim chb_ctl As clsFormEvents

Me.Reportname.text = Me.Caption
'Me.Reportname.SetFocus
Me.Reportname.SelStart = 0
Me.Reportname.SelLength = Len(Me.Reportname.text)

'Go through the checkboxes and add them to the frame
Set col_Selection = New Collection
For Each ctl In Me.Controls
If TypeName(ctl) = "CheckBox" Then
Set chb_ctl = New clsFormEvents
Set chb_ctl.chb = ctl
col_Selection.Add chb_ctl
End If
Next ctl
Me.Reportname.SetFocus ' Added this here
End Sub

Djblois
01-17-2007, 02:51 PM
No that doesn't work either :(

Tommy
01-17-2007, 03:02 PM
Will/Can you post the workbook? I may/should be able to fix it from there. :)