Results 1 to 5 of 5

Thread: How can I set focus to TextBox in RunTime?

  1. #1

    Thread Starter
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    How can I set focus to TextBox in RunTime?

    Hi,

    I made a form with a TextBox within some Char and a Button , when you click Button, TextBox will be focused and all the text in this will be selected.

    I tried to use Event Select of TextBox class, but it does not work.

    Code:
       txtBox1.select();  --> Its OK
    
        txtBox1.select(1,30);  ---> It does not work  :(
    Give me some advices. Thanks
    !Have fun!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I set focus to TextBox in RunTime?

    Firstly, Select is a method, not an event.

    Secondly, the first overload of Select you have shown is inherited from the Control class and is described thusly in MSDN documentation:
    Activates the control.
    That's obviously what you want. The second overload is inherited from the TextBoxBase class, which is the base class for the RichTextBox and MaskedTextBox too. It is described like so:
    Selects a range of text in the text box.
    As you see, no mention of selecting the control, just text within the control.

    If you want to focus a control then call its Select or Focus method. This is basically equivalent to the user clicking or tabbing to the control. If you select text within a TextBox then, by default, the effect will not be visible if the control doesn't have focus. If you want the selection to be visible regardless then you need to set the HideSelection property to False.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: How can I set focus to TextBox in RunTime?

    Yeah, thanks so much. You are right.
    I'm a amateur. I've just coded by C#.

    I've done. But I don't know how to realize when a TextBox got Focus. I can not find out Method Get or Lost Focus of TextBox class. Do you know?
    !Have fun!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I set focus to TextBox in RunTime?

    You handle the Enter and Leave events of a control to be notified when it receives and loses focus. There are also GotFocus and LostFocus events that sound like the ones to use but they are for a very specific purpose. In almost all cases you use Enter and Leave.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Re: How can I set focus to TextBox in RunTime?

    Yeah, thanks so much.
    !Have fun!

Posting Permissions

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



Click Here to Expand Forum to Full Width