Search code examples
vb.netaddhandler

dynamically create a new tab with textbox, button in the tab with click and keypress events (AddHandler is what i cant get to work)


I am creating some tabs and I need two things to work that I can't get to work. I need to AddHandler for a Textbox.Keypress event AND a Button.Click event. I can make these things work outside of the tabcontrol but not in.

In the example below my text box and buttons have same name from on tab to the another, I thought that might be my problem but even changing names between tabs does not work. I assume I need to be more specific in the AddHandler part to give the tab name as well as control. There is a logic in my real code to allow me to give unique names to each tab panel and controls, but i can't get the simple part to work.

I left some of the things I tried commented, but I tried LOTS and LOTS of other things.

Public Class Form1
  Public Sub addTab(tabPageName As String)

    Dim tabpage As New TabPage
    tabpage.Text = tabPageName
    tabpage.Name = "tabPage1" 'real code has logic to make sure names are unique
    Dim label1 As New Label
    Dim txtCreator As New TextBox
    Dim combox1 As New ComboBox
    Dim tabPageButton2 As New Button

    tabPageButton2.Parent = tabpage
    label1.Parent = tabpage
    txtCreator.Parent = tabpage
    combox1.Parent = tabpage

    label1.Location = New Point(10, 10)
    txtCreator.Location = New Point(150, 10)
    combox1.Location = New Point(300, 10)
    tabPageButton2.Location = New Point(20, 40)

    label1.Text = "Creator"
    txtCreator.Name = "txtCreator"

    'fill the comboboxes...this will come from a database but testing now.
    combox1.Items.Add("one")
    combox1.Items.Add("two")
    combox1.Items.Add("three") 'ok that works so should work from DB no problem.

    tabRoleClass.TabPages.Add(tabpage)

  End Sub

  Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    addTab("First Tab")
    AddHandler Controls("tabRoleClass.tabPage1.tabPageButton2").Click, AddressOf tabPageButton_click
    'AddHandler CType(Controls("tabPageButton"), Button).Click, AddressOf tabPageButton_click
    'AddHandler Controls("tabPageButton").Click, AddressOf tabPageButton_click
    AddHandler CType(Controls("txtCreator"), TextBox).KeyPress, AddressOf txtcreator_keypress 'the Keypress to call lookup
  End Sub
  Private Sub tabPageButton_click(sender As System.Object, e As System.EventArgs) 'Handles tabPageButton.click
    MessageBox.Show(tabRoleClass.SelectedTab.Name.ToString)
  End Sub

  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    addTab("Second Tab")
    tabRoleClass.SelectedIndex = tabRoleClass.TabCount - 1
    'AddHandler Controls("tabRoleClass.tabPage1.tabPageButton2").Click, AddressOf tabPageButton_click
    'AddHandler CType(Controls("tabPageButton"), Button).Click, AddressOf tabPageButton_click
    'AddHandler Controls("tabPageButton").Click, AddressOf tabPageButton_click
    'AddHandler CType(Controls("txtCreator"), TextBox).KeyPress, AddressOf txtcreator_keypress 'the Keypress to call lookup
  End Sub
  Private Sub txtcreator_keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) 'Handles txtCreator.KeyPress
    MessageBox.Show("keypress worked on " & tabRoleClass.SelectedTab.Name.ToString)
  End Sub
End Class

Solution

  • This is a very confusing question and your code could really do with some cleaning, but you need to add the AddHandler code to the addTab subroutine as pointed out by @Plutonix:

    Public Sub addTab(tabPageName As String)
       Dim tabpage As New TabPage
       Dim tabPageButton As New Button
       Dim txtCreator As New TextBox
    
       /.../
    
       AddHandler tabPageButton.Click, AddressOf tabPageButton_click
       AddHandler txtCreator.KeyDown, AddressOf txtcreator_keypress
    
       tabRoleClass.TabPages.Add(tabpage)
    End Sub
    
    Private Sub tabPageButton_click()
       MessageBox.Show(tabRoleClass.SelectedTab.Name.ToString)
    End Sub
    
    Private Sub txtcreator_keypress()
       MessageBox.Show("keypress worked on " & tabRoleClass.SelectedTab.Name.ToString)
    End Sub
    
    
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
       addTab("First Tab")        
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
       addTab("Second Tab")
       tabRoleClass.SelectedIndex = tabRoleClass.TabCount - 1
    End Sub
    

    AddHandler works by adding event handlers to your controls. This means that each time an event is raised during this runtime, the new event handler will handle the event; everytime you click your tabPageButton the associated event tabPageButton_click will handle it.

    Therefore, you will only need to add the handler once, preferably upon the creation of the control. There is absolutely no need to create them upon every single keypress, for example. You should look up event handlers on MSDN.

    Hope this helps!