[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?
Re: [2005] How to get the current control with Focus?
Me.ActiveControl will return the control that currently has focus.
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?
Re: [2005] Append a char to a textbox that was selected
I would do something like this:
VB.Net Code:
Public Class Form1
Private focusedTextBox As TextBox
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
focusedTextBox = TextBox1
End Sub
Private Sub TextBox2_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.GotFocus
focusedTextBox = TextBox2
End Sub
Private Sub TextBox3_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.GotFocus
focusedTextBox = TextBox3
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
focusedTextBox.AppendText("something")
End Sub
End Class
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.
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"
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.
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.