Consulting

Results 1 to 20 of 20

Thread: Set focus in a list box

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Set focus in a list box

    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?

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hi Djblois,

    This sub will select all text in the textbox.

    [VBA]
    Private Sub TextBox1_Enter()
    TextBox1.SelLength = Len(TextBox1.Text)
    End Sub
    [/VBA]

    HTH

  3. #3
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Can I use that to select it right when the Userform is activated?

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    I tried this and it worked. When I saw the form the text was highlighted
    [VBA]Private Sub UserForm_Initialize()
    TextBox1.Text = "FRIED TATERS"
    TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Text)
    End Sub
    [/VBA]

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Quote Originally Posted by Tommy
    [vba]TextBox1.Text = "FRIED TATERS"[/VBA]
    Hi Tommy.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    This is what I have now:

    [vba]Me.ReportName.Value = Me.Caption
    'Me.ReportName.SetFocus
    'Me.ReportName.SelStart = 0
    Me.ReportName.SelLength = Len(Me.ReportName.text)[/vba]

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

  7. #7
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Hey Steve

    You should see the names I use for subs/functions

    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.
    [vba]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[/vba]

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

  8. #8
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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.

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Daniel, are you running this on a textbox or a listbox....? your first post indicates that your trying to type into a listbox....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    My Bad
    [vba]Private Sub UserForm_Initialize()
    Me.ReportName.AddItem Me.Caption
    Me.ReportName.Selected(0) = True
    End Sub
    [/vba]

    Thanks Steve

  11. #11
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    My bad I mean a textbox not a list box

  12. #12
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    listbox with name ReportName
    [VBA]
    Private Sub UserForm_Initialize()
    Me.ReportName.AddItem Me.Caption
    Me.ReportName.Selected(0) = True
    End Sub
    [/VBA]

    TextBox with name ReportName

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

    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.

  13. #13
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That seems to work great Tommy....just start typing then hit enter..enter data and hit enter again...etc.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  14. #14
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by Tommy
    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.
    [vba]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[/vba]
    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

    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




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  15. #15
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    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

  16. #16
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    No problem Always glad to help.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  17. #17
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    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?

    [VBA]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
    [/VBA]

  18. #18
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Try this, I think it is because you are working on other controls and when the form is seen another control has the focus.
    [VBA]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
    [/VBA]

  19. #19
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    No that doesn't work either

  20. #20
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Will/Can you post the workbook? I may/should be able to fix it from there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •