Can't catch the Button_Click Event

Kasper Larsen

Member
Joined
Mar 5, 2005
Messages
6
Programming Experience
Beginner
Hi.....

I have a small problem with a toolbar control that I have in my application. It seems that I sometimes can't catch the button_click events, other times I can. I am able to press the buttons, but nothing happens in the button_clicked event handler. Is it possible that I set a property that discards these events or dos someone know what the problem could be?

Kasper
 
a toolbar button is much different then the button object that you can slap onto a form, each button in a toolbar has the same click event as all the other buttons (which means that you can code the click event for a button on the toolbar, and it doesnt matter what button you click, the same event fires for all of them) so to figure out which button was actually clicked takes a little bit of code using the the e.Button property in the event:

let's say you have a toolbar with three buttons on it
VB.NET:
Private Sub tlbParent_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolbarButtonClickEventArgs) Handles tlbParent.ButtonClick
	Select Case tlbParent.Button.IndexOf(e.Button)
		Case 0 
			'Left button
		Case 1
			'Middle button
		Case 2
			'Right button
		End Select
End Sub

and so on, hope this helps
 
Thanks for the reply JuggaloBrotha. I am using the code you are suggesting to handle the click event. I have just pated the first part of the code below.

VB.NET:
 'Toolbars 
 
Private Sub Toolbar_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles Toolbar.ButtonClick
Select Case e.Button.ToolTipText
 
Case "Gentagelse"
 
Dim frmRecurrence As frmConRecurrence
 
Try
 
If _PartofSeries = False And _RecurrenceEnabled = False Then
 
_RecurrenceData = New ConRecurrenceData(Me.dtpDate.Value, Me.dtpStart.Value, Me.dtpEnd.Value)
 
End If
.
.
.

In some situations the code works fine, and the events are caugth. Other times the Sub routine is not being called when the user hits the toolbar buttons. I can still press the buttons, so it is not disabled. So my question is, if the are any properties that can prevent the sub routine from being called?


 
i would suggest using the
VB.NET:
Select Case tlbParent.Button.IndexOf(e.Button)
Case 0
Case 1
End Select

as opposed to the tooltips, because if you change a tooltip then that button will no longer work until you modify the tooltip your looking for by using the select case based on button position if you add a button, you'll have to go to the toolbar click event and add the code for the new button, which will remind you to update the cases anyways
 
The problem is not the logic whitin the Sub routine (even though I see your point with using the index instead of the tooltip to figure out which of the buttons that where pressed). The problem is, that the entire Sub routine is not being called when I press any buttons in the toolbar. If I place a break point at the begining of the Sub routine, I will find that the code here is never being executed. Like I also mentioned, the routine work fine under some conditions, but not others.... I just can't figure out why the routine isn't called when it has the handler added to it...
 
Back
Top