Announcement

Collapse
No announcement yet.

Put/Place Cursor Into UserForm TextBox

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Put/Place Cursor Into UserForm TextBox

    To accomplish whats in the title, I tried (and think I used successfully previously) the following code:

    Code:
    with tbfind
        .SelStart = 0
        .SelLength = Len(.Value)
        .SetFocus
    end with
    Some details, i have a userform named Findform, a textbox in it named tbFind, and the above code is in the userform initialization event section.

    Oh also, I dont get any error, everything runs just fine, its just the cursor is not inside the textbox waiting for typing in it. (but i do think it is the focus, when i press tab it goes to the next one in the tab order)

    Looking for any ideas why i may run into problems with this. Thanks!

  • #2
    Re: Putting Cursor Into A Userform Textbox With Vb

    Code:
    Private Sub UserForm_Activate()
        TextBox1.SetFoucus
    End Sub

    Comment


    • #3
      Re: Put/Place Cursor Into UserForm TextBox

      I tried sticking that line into userform_activate, spelled correctly though of course, to no avail =(

      Oh i also forgot to mention in the original post, the textbox is on a multipage. Maybe that affects it somehow? I have controls inside and outside of the multipage.

      Also i used
      Code:
      FindForm.MultiPage1.Value = 0
      which is the page that the tbFind box is on.

      Thanks

      Comment


      • #4
        Re: Put/Place Cursor Into UserForm TextBox

        If there is any text in the box, the cursor will not appear. The code requries that the whole contents be hightlighted rather than the cursor put in a particular location. To make the cursor appear

        Code:
        TextBox1.SelLength = 0

        Comment


        • #5
          Re: Put/Place Cursor Into UserForm TextBox

          Well I gave that a shot, it did not work as well I was thinking it wouldn't, because
          Code:
          textbox1.SelLength = 0
          'is the same thing as 
          textbox1.SelLength = Len(textbox1.Value)
          'if textbox1 is clear of text
          I appreciate the suggestion though!

          I'm thinking its possibly something screwy with Vista and xl2000, or more likely some bad code/design somewhere that affects it for some reason. I was just hoping maybe someone had the same problem.

          Comment


          • #6
            Re: Put/Place Cursor Into UserForm TextBox

            What EXACTLY do you want to do? "did not work" is a helpful as telling the DR you don't feel well.

            Comment


            • #7
              Re: Put/Place Cursor Into UserForm TextBox

              Isolating the simple code to a userform with Textbox1 on page 1 of a multipage control... the code works fine.

              There's something else in your code/form that's causing your problem.
              Sub All_Macros(Optional control As Variant)

              Comment


              • #8
                Re: Put/Place Cursor Into UserForm TextBox

                Originally posted by Dave Hawley
                What EXACTLY do you want to do? "did not work" is a helpful as telling the DR you don't feel well.
                Sorry, I will be more explicit. The whole project is basically an attempt at rewriting the find feature of Excel, to make my older excel version more to my liking when using it. I'll write it in code with comments explaining what i'm expecting.
                Code:
                'press hotkey to start the addin:
                Sub MyFindAll()
                    FindForm.Show
                End Sub
                'this fires the initialization:
                Private Sub UserForm_Initialize()
                
                    With cboxLookIn
                        .AddItem "Formulas"
                        .AddItem "Values"
                        .AddItem "Comments"
                        .Value = "Formulas"
                    End With
                    
                    With cboxSearchScope
                        .AddItem "Worksheet"
                        .AddItem "Selected cells"
                        .AddItem "Entire Workbook"
                        .Value = "Worksheet"
                    End With
                    
                    With cboxMatch
                        .AddItem "Entire cell"
                        .AddItem "Any part of cell"
                        .ListIndex = 1
                    End With
                    
                    lbFound.ColumnWidths = 50
                    bReplaceAll = False
                    FindForm.MultiPage1.Value = 0
                    
                    With tbFind
                        .SelStart = 0
                        .SelLength = Len(.text)
                        .SetFocus
                    End With
                    
                End Sub
                'when the userform shows after this code, I am expecting
                'the blinking cursor prompt to be inside the textbox named
                '.tbFind.  This doesnt happen.
                Originally posted by Aaron Blood
                Isolating the simple code to a userform with Textbox1 on page 1 of a multipage control... the code works fine.
                Thanks for the suggestion, I should have tried isolating it like that before I posted. Having said that, I did try now, and it still does not put the cursor into the textbox. I did:
                Code:
                'I have a new, blank workbook.  I placed a form in it named Userform1.
                'I placed a MultiPage named MultiPage1 within it.  I placed a textbox
                'named Textbox1 within the multipage.  The code is as follows:
                Private Sub UserForm_Initialize()
                    TextBox1.SelStart = 0
                    TextBox1.SelLength = Len(TextBox1.Text)
                    TextBox1.SetFocus
                End Sub
                'i also tried placing the same code within the Activation event, so it
                'was in both events at the same time, to no avail.
                Also just want to add, using the above test, when the Userform showed and the cursor wasnt in the textbox, I clicked on page 2, then clicked back to page 1 immediately, the cursor did go inside the textbox1.

                Comment


                • #9
                  Re: Put/Place Cursor Into UserForm TextBox

                  Some follow up to this issue. Thinking about this fact
                  Originally posted by Discodrew
                  I clicked on page 2, then clicked back to page 1 immediately, the cursor did go inside the textbox1.
                  so I made some changes to the code. I removed all the setfocus code from the initialization event, and anywhere else within the project. I changed my simple procedure code to show the userform (and trigger the initialization) as follows
                  Code:
                  'it was:
                  Sub MyFindAll()
                      FindForm.Show
                  End Sub
                  'now it is
                  Sub MyFindAll()
                      FindForm.Show
                      FindForm.MultiPage1.Value = 1
                      FindForm.MultiPage1.Value = 0
                  End Sub
                  And strangely enough, this does the trick. There is no setfocus anywhere within the code, and I dont have any multipage_change event code. Upon loading and showing the userform, the cursor is now in the textbox awaiting the user to type. voila

                  Comment

                  Working...
                  X