积极答复者
怎样创建带相同事件的控件?

问题
答案
-
每次创建一个新的TextBox1后动态使用AddHandler textbox1.XXX事件名称, AddressOf 函数名称
ASP.NET Questions
Other Discussions
FreeRice Donate
Issues to report
Free Tech Books Search- 已标记为答案 Carl CaiModerator 2014年2月11日 8:42
-
Public Class Form1 ''' <summary> ''' 保留点击文本框的整个对象 ''' </summary> ''' <remarks></remarks> Private focustxtbox As TextBox = Nothing Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim inttop As Integer = 50 Dim tbname As Integer = 1 Dim i As Integer '10个(或更多) For i = 1 To 10 Dim objtb = New TextBox With objtb .Name = "bt" & tbname .Top = inttop .Left = 50 .Visible = True .Text = "bt" & tbname End With Me.Controls.Add(objtb) tbname += 1 inttop += 30 '动态绑定 AddHandler objtb.GotFocus, AddressOf GotFucusTextBox Next End Sub Private Sub GotFucusTextBox(sender As Object, arg As EventArgs) focustxtbox = sender End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MsgBox(focustxtbox.Text) '重新聚焦,防止点击按钮后聚焦失灵 focustxtbox.Focus() End Sub End Class
ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search- 已标记为答案 Carl CaiModerator 2014年2月11日 8:42
全部回复
-
使用WithEvent创建新控件+Handles:
Dim WithEvent txtbox1 As New TextBox() Dim WithEvent txtbox2 As New TextBox() ……………… Private Sub txtBoxEvent() Handles txtbox1.XX事件,txtbox2.XX事件 ……………… End Sub
ASP.NET Questions
Other Discussions
FreeRice Donate
Issues to report
Free Tech Books Search- 已编辑 ThankfulHeartModerator 2014年2月1日 9:31
-
谢谢,但是这样不行。我意思是在窗体运行后根据数据库里的数量来创建textbox的数量(不确定),有时候还要点击button来再次添加一些textbox。添加后我可以移动每一个textbox的位置。就是收集图形对应位置的尺寸数据,所以要可以有鼠标事件来移动textbox的位置。最后再把数据保存到数据库。
您说的这个办法我知道,与其这样到不如用工具里的textbox控件更方便(关键是不确定数量)。程序运行时我要在多文档父窗体界面上点击button然后子文档里就添加textbox。然后我可以移动textbox的位置(用鼠标按住事件,鼠标移动事件,鼠标释放事件),或者像photoshop里先添加一个文本条然后再点击移动按钮来移动,这样也行。求帮助,谢谢!
-
每次创建一个新的TextBox1后动态使用AddHandler textbox1.XXX事件名称, AddressOf 函数名称
ASP.NET Questions
Other Discussions
FreeRice Donate
Issues to report
Free Tech Books Search- 已标记为答案 Carl CaiModerator 2014年2月11日 8:42
-
谢谢您,但我还是不会。您看看再给我指导一下。我只能创建,但没办法使用它们。
Dim WithEvents objtb As Windows.Forms.TextBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inttop As Integer = 50
Dim tbname As Integer = 1
Dim i As Integer‘10个(或更多)
For i = 1 To 10
objtb = New TextBox
With objtb
.Name = "bt" & tbname.Top = inttop
.Left = 50
.Visible = True
End With
Me.Controls.Add(objtb)
tbname += 1
inttop += 30
Next
End Sub创建一排textbox,我想再程序运行时点击tb1,然后mesgbox("你点击了"& tb1.text)。我点击tb5,然后mesgbox("你点击了"& tb5.text)。求您帮忙完善一下代码该怎么写。请您帮忙写代码,我才学没代码我理解不了。谢谢!
- 已编辑 suoshu 2014年2月4日 13:48
-
Public Class Form1 ''' <summary> ''' 保留点击文本框的整个对象 ''' </summary> ''' <remarks></remarks> Private focustxtbox As TextBox = Nothing Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim inttop As Integer = 50 Dim tbname As Integer = 1 Dim i As Integer '10个(或更多) For i = 1 To 10 Dim objtb = New TextBox With objtb .Name = "bt" & tbname .Top = inttop .Left = 50 .Visible = True .Text = "bt" & tbname End With Me.Controls.Add(objtb) tbname += 1 inttop += 30 '动态绑定 AddHandler objtb.GotFocus, AddressOf GotFucusTextBox Next End Sub Private Sub GotFucusTextBox(sender As Object, arg As EventArgs) focustxtbox = sender End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click MsgBox(focustxtbox.Text) '重新聚焦,防止点击按钮后聚焦失灵 focustxtbox.Focus() End Sub End Class
ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search- 已标记为答案 Carl CaiModerator 2014年2月11日 8:42