Dynamic event handler stops working
I have a button on a form (within a panel) created in code. This button has an on-click event handler created with AddHandler attached to it and it works on the first click but not on subsequent clicks. There are 3 others buttons created in the same manner which always work. Any ideas? I tried doing Dim WithEvents and that didn't work.
Re: Dynamic event handler stops working
Can you attach an example that shows the problem? Or at least post the code you're using.
I stared as hard as I could, but can't really see clearly what you've done.
Re: Dynamic event handler stops working
Thanks for replying.
Definition:
Code:
Dim LastButton As Button
Creation:
Code:
LastButton = New Button
LastButton.Width = 32
LastButton.Height = 26
LastButton.Top = 6
LastButton.Left = 572
LastButton.BackColor = Color.Khaki
LastButton.Font = New Font("Courier New", 10, FontStyle.Bold)
LastButton.Text = ">|"
LastButton.Enabled = False
AddHandler LastButton.Click, AddressOf LastButton_Click
FooterPanel.Controls.Add(LastButton)
Execution:
Code:
Private Sub LastButton_Click(ByVal Sender As System.Object, ByVal e As System.EventArgs)
PagN = NPage
DisplayPage(PagN, dt, PanelWidth)
End Sub
Quote:
Originally Posted by
passel
Can you attach an example that shows the problem? Or at least post the code you're using.
I stared as hard as I could, but can't really see clearly what you've done.
Re: Dynamic event handler stops working
With events is only needed if you want to wire the handler at design time. Unless you have code that explicitly removes the handler somewhere, the handler should still be wired. Start by putting a break point in the handling routine and make sure it does indeed fire only once.
Re: Dynamic event handler stops working
I found the problem. Sometimes the execution is firing the page number textbox leave event. When I remove the page number textbox leave event it works fine. The page number textbox is set in the code. It's still strange tho because the firing of the textbox leave event appears to be delayed until a button is clicked. But it works if I take out this leave event code.
My idea was to let the user simply tab out of page number after entering that field and it would process. But now I think I'll just require hitting the enter key (which is handled by the keypress event which is not fired in the code.)
Thanks.