动态创建一个带有文本框的新选项卡,带有单击和按键事件的选项卡中的按钮(AddHandler 是我无法开始工作的)
dynamically create a new tab with textbox, button in the tab with click and keypress events (AddHandler is what i cant get to work)
我正在创建一些选项卡,我需要两件我无法开始工作的事情。我需要 AddHandler
参加 Textbox.Keypress
活动和 Button.Click
活动。我可以让这些东西在 tabcontrol 之外工作,但不能在里面工作。
在下面的示例中,我的文本框和按钮从一个选项卡到另一个选项卡具有相同的名称,我认为这可能是我的问题,但即使在选项卡之间更改名称也不起作用。我假设我需要在 AddHandler 部分中更具体地给出选项卡名称和控件。在我的真实代码中有一个逻辑允许我为每个选项卡面板和控件指定唯一的名称,但我无法让简单的部分工作。
我留下了一些我尝试过的评论,但我尝试了很多很多其他的东西。
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
这是一个非常令人困惑的问题,您的代码确实可以进行一些清理,但是您需要将 AddHandler
代码添加到 addTab
子例程中,正如@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
通过向您的控件添加事件处理程序来工作。 这意味着每次在此运行时引发事件时,新的事件处理程序都会处理该事件;每次您单击 tabPageButton
时,关联的事件 tabPageButton_click
都会处理它。
因此,您只需添加一次处理程序,最好是在创建控件时添加。例如,绝对没有必要在每次按键时都创建它们。您应该查找 event handlers on MSDN.
希望对您有所帮助!
抱歉,如果代码令人困惑,我将我的实际代码剪切成一个 "sample",我可以看到混乱。现在我当然很困惑,我最初在创建选项卡的 addTab 子内部有 AddHandler,但它在那里不起作用,我错误地认为原因是控件尚未创建,所以我将其移出。今天早上把它移回这个子里效果很好,我不知道我做错了什么,但是把它移到它所属的地方它工作得很好,非常感谢,我为此工作了 2 天,尝试和谷歌搜索。下次我将 post 真实代码而不是示例,以减少混淆,并删除我评论的尝试(我认为这些将有助于展示我正在尝试的内容,但我认为它没有)
我正在创建一些选项卡,我需要两件我无法开始工作的事情。我需要 AddHandler
参加 Textbox.Keypress
活动和 Button.Click
活动。我可以让这些东西在 tabcontrol 之外工作,但不能在里面工作。
在下面的示例中,我的文本框和按钮从一个选项卡到另一个选项卡具有相同的名称,我认为这可能是我的问题,但即使在选项卡之间更改名称也不起作用。我假设我需要在 AddHandler 部分中更具体地给出选项卡名称和控件。在我的真实代码中有一个逻辑允许我为每个选项卡面板和控件指定唯一的名称,但我无法让简单的部分工作。
我留下了一些我尝试过的评论,但我尝试了很多很多其他的东西。
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
这是一个非常令人困惑的问题,您的代码确实可以进行一些清理,但是您需要将 AddHandler
代码添加到 addTab
子例程中,正如@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
通过向您的控件添加事件处理程序来工作。 这意味着每次在此运行时引发事件时,新的事件处理程序都会处理该事件;每次您单击 tabPageButton
时,关联的事件 tabPageButton_click
都会处理它。
因此,您只需添加一次处理程序,最好是在创建控件时添加。例如,绝对没有必要在每次按键时都创建它们。您应该查找 event handlers on MSDN.
希望对您有所帮助!
抱歉,如果代码令人困惑,我将我的实际代码剪切成一个 "sample",我可以看到混乱。现在我当然很困惑,我最初在创建选项卡的 addTab 子内部有 AddHandler,但它在那里不起作用,我错误地认为原因是控件尚未创建,所以我将其移出。今天早上把它移回这个子里效果很好,我不知道我做错了什么,但是把它移到它所属的地方它工作得很好,非常感谢,我为此工作了 2 天,尝试和谷歌搜索。下次我将 post 真实代码而不是示例,以减少混淆,并删除我评论的尝试(我认为这些将有助于展示我正在尝试的内容,但我认为它没有)