单击按钮更改所选文本框的文本

时间:2013-10-10 08:10:19

标签: windows vb.net winforms button textbox

我遇到一个问题,涉及按钮点击事件,更改表单上当前所选文本框的文本。这可能吗?如果是这样我该如何去做呢?

编辑:我不知道文本框的名称,但它始终是当前选中或“聚焦”的文本框。

1 个答案:

答案 0 :(得分:0)

主要问题是当您按下按钮时,文本框不再是焦点。 通过解决方案显示here,您可以执行以下操作:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If lastTextBoxFocused IsNot Nothing Then
        lastTextBoxFocused.Text = "Bla bla, bla!"
    End If
End Sub

Dim lastTextBoxFocused As TextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'find all TextBox's in the Form.
    For Each Ctrl In Me.Controls
        If TypeOf Ctrl Is TextBox Then
            'attach a lambda expression to each Enter event, to "remember" the last enter
            AddHandler CType(Ctrl, TextBox).Enter, Sub(o, ev) lastTextBoxFocused = o
        End If
    Next
End Sub