考虑到我对整个编码工作很陌生,这可能是一个非常愚蠢的问题,但请耐心等待。这就是我想做的:
我有 2 个文本框。文本框1、文本框2
我希望这样的事情发生: 当我按下 Button1 时,为我之前在操作应用程序时单击/选择的特定文本框输入“1”。
假设我点击TextBox1,然后按下Button1,那么“1”只会出现在TextBox1中 相反,如果我单击 TextBox2,然后按 Button1,则“1”将仅出现在 TextBox2 中。
有什么简单的操作吗?我知识不多,希望我的解释有道理。这是针对 VB.net 的。请简单地解释一下答案。
我尝试过类似......
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim intNumberCalc As String
intNumberCalc = TextBox1.Text
Select Case intNumberCalc
Case 1
If Button1.PerformClick Then
TextBox1.Text = "1"
End If
End Select
End Sub
即使我承认这永远不会起作用,但我只是尝试过。我真的不知道如何让 Button1 仅按下我单击的文本框上的输入。
为每个
TextBox
GotFocus
事件添加一个事件处理程序,并将当前聚焦的 TextBox 记录到局部变量中。当按下按钮时,执行如下操作:FocussedTextbox.Text &= Ctype(Sender,Button).Text
' Local Variable to store the Focussed TextBox
Private FocussedTextbox as TextBox = nothing
' Call this to initialize your handlers Once when the form is first loaded
Protected Sub InitHandlers()
For Each c As Control In p_PanelContainingTextboxes.Controls
If TypeOf c Is TextBox Then AddHandler cType(c,TextBox).GotFocus, AddressOf TextBox_GotFocus
Next
For Each c As Control In p_PanelContainingButtons.Controls
If TypeOf c Is Button Then AddHandler cType(c,Button).Clicked, AddressOf NumberButton_Clicked
Next
End Sub
' Store the Focussed TextBox to the local variable
Protected Sub TextBox_GotFocus(sender as Object, e as EventArgs)
FocussedTextBox = Sender
End Sub
' Append the text in the textbox by the text on the button
Protected Sub NumberButton_Clicked(sender as Object, e as EventArgs)
FocussedTextbox.Text &= Ctype(Sender,Button).Text
End Sub