'vb.net Insert text into active textbox

In a windows form, I have a button and two textboxes. When I click the button, I want some text to be added into the cursor position of the textbox that is holding the cursor.I know it's very simple to insert text into a textbox at cursor position. But with two textboxes,

I think about the focus property to determine which textbox is currently being typed in, but then as when I click the button, the focus jump to the button...

My question, how to know the "last focused control" .. or anyway to know if a textbox is being texted ?



Solution 1:[1]

There's no inbuilt LastFocusedTextBox property. It's simply up to you to remember.

Private lastFocusedTextBox As TextBox

Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter
    lastFocusedTextBox = DirectCast(sender, TextBox)
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If lastFocusedTextBox IsNot Nothing Then
        lastFocusedTextBox.SelectedText = "Hello World"
    End If
End Sub

You could use the Leave event just as well under the circumstances, but using Enter means that lastFocusedTextBox will contain the correct value in code while that TextBox has focus too.

Solution 2:[2]

You can use a Flag to check last TextBox focus: Ex:

Dim iTxt=0
 Private Sub txt1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtFile.MouseClick
iTxt=1
    End Sub
 Private Sub txt2_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtFile.MouseClick
iTxt=2
    End Sub

Solution 3:[3]

I had the same problem, so I added a label that determines which text box is selected.

If textbox1 is selected then the label1.text = "textbox1"
// then the button will have this IF statement

If label1.text = "textbox1" then 
textbox1.text = "example"
end if 

If textboxt2 is selected then the label1.text = "textbox2"
// then the button will have this IF statement

If label1.text = "textbox2" then 
textbox2.text ="example"
end if 

Solution 4:[4]

I have invented a new way to know if the textbox1 or textbox2 has the focus, check it and enjoy.

Public Class Form1

Dim clr As Color = Nothing

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    clr = TextBox1.BackColor
End Sub

Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus
    Select Case sender.name
        Case "TextBox1"
            TextBox1.BackColor = Color.Navy
        Case "TextBox2"
            TextBox2.BackColor = Color.Navy
    End Select
End Sub

Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.LostFocus, TextBox2.LostFocus
    Select Case sender.name
        Case "TextBox1"
            TextBox1.BackColor = clr
        Case "TextBox2"
            TextBox2.BackColor = clr
    End Select
End Sub

End Class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jmcilhinney
Solution 2 D T
Solution 3 miken32
Solution 4 purple_2022