How do I add a handler to a dynamically created textbox?
In my application, when user adds a new file it adds a new textbox into a new tab. Which that works great. Before I decided to add the multidocument capabilities, I had a contextmenu that became active when the user right clicked the textbox. I still have the code for that which I can re-use I just want to know how I can do the same for my dynamically created textboxes. How do I set it up so I can create a event handler for the dynamic textbox like I would one on design time.
Re: How do I add a handler to a dynamically created textbox?
Re: How do I add a handler to a dynamically created textbox?
Hi tg I should of been more clear.
I added these to the code I use to add the tabs and textbox
vb Code:
AddHandler tb.DragDrop, AddressOf tb_DragDrop
AddHandler tb.MouseUp, AddressOf tb_mouseup
tb is the textbox I declared.
I then added my sub for that.
vb Code:
Private Sub tb_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
'this appends the listviewitem's tag to the rtb on dragdrop
Dim page As RadPageViewPage = RadPageView1.SelectedPage
For Each txt As FastColoredTextBox In page.Controls.OfType(Of FastColoredTextBox)()
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
Dim item As ListViewItem = DirectCast(e.Data.GetData(GetType(ListViewItem)), ListViewItem)
txt.AppendText(item.Tag.ToString & Environment.NewLine)
End If
Next
End Sub
Is this the correct way of doing it? I cannot add the handles tb.dragdrop beside it the sub, Unless I don't need it?
I have another sub that I need to add the tb. handler to although when I try to add it it says it cannot resolve symbol. I point it out in the code below.
vb Code:
Private Sub all_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver, mainsidemenu.DragOver, ScriptEditor.DragOver <-----I need to add the tb.dragover
If e.Data.GetDataPresent(GetType(ListViewItem)) Then
e.Effect = DragDropEffects.All
End If
End Sub
Thanks TG.
Re: How do I add a handler to a dynamically created textbox?
Looks like I did have it right I just left something out of my other code. Thanks.
Re: How do I add a handler to a dynamically created textbox?
Sorry I was on a mobile when I did that reply other wise it would have been more indepth. But yes, the AddHandler is instead of the Handles clause... it allows you to hook and unhook (using RemoveHandler) events to subs... or even rewire handlers... As long as the signature of the sub matches the signature of the event you can hook anything up at run time.
-tg