Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Append a char to a textbox that was selected

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [RESOLVED] [2005] Append a char to a textbox that was selected

    Hi!!

    In a form how to get the control that currently has focus, knowing that it can be inside a groupbox?
    Last edited by Lasering; Nov 2nd, 2007 at 06:11 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] How to get the current control with Focus?

    Me.ActiveControl will return the control that currently has focus.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] How to get the current control with Focus?

    I thought that would help but it didnt!
    Here my problem: I have 3 multiline textbox. And a lot of buttons: Alpha, Beta, Gamma, etc. The idea is when u click on a button for example Alpha he appends the character alpha to the previously selected textbox text. For example u had selected the first textbox and then hit alpha, he would append and a alpha to the first textbox text, but if u had the second textbox selected and then hit alpha he would add to the second textbox.
    How can i do this?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Append a char to a textbox that was selected

    I would do something like this:
    VB.Net Code:
    1. Public Class Form1
    2.     Private focusedTextBox As TextBox
    3.  
    4.     Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
    5.         focusedTextBox = TextBox1
    6.     End Sub
    7.  
    8.     Private Sub TextBox2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.GotFocus
    9.         focusedTextBox = TextBox2
    10.     End Sub
    11.  
    12.     Private Sub TextBox3_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.GotFocus
    13.         focusedTextBox = TextBox3
    14.     End Sub
    15.  
    16.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    17.         focusedTextBox.AppendText("something")
    18.     End Sub
    19. End Class
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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

    Re: [2005] Append a char to a textbox that was selected

    Don't use the GotFocus event. As specified in the documentation you should use the Enter event. You could also use one method to handle the event for all TextBoxes and assign the sender to the member variable.

    Also, you may be interested in using a Button that doesn't take focus when you click it. If so then follow the Onscreen Keyboard link in my signature.
    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

  6. #6

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] Append a char to a textbox that was selected

    I end up for not using the GotFocus neither the Enter event because i needed the position of the caret when the user unselects the textbox. So later i could insert the char i want (the one respectively to the button), into the "actual caret position"
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

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

    Re: [RESOLVED] [2005] Append a char to a textbox that was selected

    That's of no consequence. The SelectionStart and SelectionLength properties of a TextBox don't change when it loses focus. If you want to insert text into a TextBox at the caret position you just set its SelectedText property, whether it has focus or not.
    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

  8. #8

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [RESOLVED] [2005] Append a char to a textbox that was selected

    Thks a lot that detail "The SelectionStart and SelectionLength properties of a TextBox don't change when it loses focus" really maded a difference.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

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