我遇到了一个问题,涉及在按钮单击事件中更改表单上当前选定文本框的文本。这可能吗?如果是这样,我该怎么做?
编辑:我不知道文本框的名称,但它始终是当前选定或“聚焦”的文本框。
主要问题是当您按下按钮时,文本框不再是焦点。通过出现在这里的解决方案,您可以这样做:
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