动态textbox事件 复杂版(具体情况见内容)
我创建了一个动态文本框,添加了事件,现在的情况是比如说我点击了textbox2,触发了事件得到textbox1.text=1,textbox2.text=2, 现在的问题是:当我点击textbox4,我想得到textbox3.text=4,textbox4.text=6
(就是第一次点击得到的sender值是1 to sender,而第二次点击的时候是第一次的sender+1 to 第二次的sender)
大侠们 帮忙了,脑子想破了
[解决办法]
不知道是不是这样子的
Public textbox1, textbox2, textbox3, textbox4, textbox5 As New TextBox
Private Sub Form1_Load(ByVal s As Object, ByVal e As EventArgs) Handles MyBase.Load
Static obj$
Dim ctl As Object
For i = 1 To 5
CallByName(CallByName(Me, "textbox" & i, vbGet), "name", vbLet, "textbox" & i)
Me.Controls.Add(CallByName(Me, "textbox" & i, vbGet))
Next
For Each ctl In Me.Controls
If TypeOf (ctl) Is TextBox Then
Dim x As TextBox = ctl
x.Text = Int(Rnd() * 10)
x.Top = x.Name.Substring(x.Name.Length() - 1, 1) * 40
AddHandler x.MouseClick, Sub(_s As Object, _e As MouseEventArgs)
Dim Tx_index% = _s.name.Substring(_s.Name.Length() - 1, 1)
For i = IIf(obj = "", 1, obj + 1) To Tx_index
MsgBox("TextBox" & i & "=" & CallByName(Me, "textbox" & i, vbGet).text)
Next
obj = Tx_index
End Sub
End If
Next
End Sub
[解决办法]
没看明白,重写TextBox类,例如点击textbox计数就会增加
Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim textbox1 As New MyTextBox Me.Controls.Add(textbox1) End SubEnd ClassPublic Class MyTextBox Inherits TextBox Public newTick As Integer = 0 Private Sub MyTextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click newTick += 1 Me.Text = newTick End SubEnd Class