I have created and attached an AddHandler to a Button.Click event. When the button.click event starts, I can't get out of it, because it does the same actions over and over. The only way I can stop it is to cancel the debug session. When I
reach the End Sub statement in the LoadAndShowButtonItems, I expect to return to the form and make another selection which might be a button or some other control. Code follows:
Private Function AttachAddHandlerForButtonInMeControls(ByVal btnName As String) _ As Boolean button = New Button For Each buttonFound As Button In Me.Controls.OfType(Of Button)() If Convert.ToString(buttonFound.Name) = btnName Then button = buttonFound button.Visible = True AddHandler button.Click, New EventHandler(AddressOf LoadAndShowButtonItems) ' AddHandler button.Click, AddressOf LoadAndShowButtonItems ' Have run with either AddHandler and get the same result either way. Return True Exit Function End If Next Return False End Function Private Sub LoadAndShowButtonItems(ByVal sender As Object, _ ByVal e As System.EventArgs) Dim buttonName = New Button() buttonName = CType(sender, Windows.Forms.Button) md_Idx = CInt(Microsoft.VisualBasic.Right(buttonName.Name, 2)) ' Selects the Item to process Dim newDiaDayItemSelect As New diaDayItemSelect newDiaDayItemSelect.dateWanted = dateArray(md_Idx, 0) newDiaDayItemSelect.ShowDialog() ' Selects the type of action wanted for the above selected item and places ' the action wanted in frmCalMonthAction which is in a module. Dim newdiaCboGetAction As New diaCboGetAction newdiaCboGetAction.ShowDialog() ' Does the froCalMonthAction on the item which is: copy, add, edit, delete or view Dim newDiaCalEvent As New diaCalEvent newDiaCalEvent.itemDateFromCalMonth = itemReturn.ItemDate newDiaCalEvent.itemKeyFromCalMonth = itemReturn.KeyItem newDiaCalEvent.itemAction = frmCalMonthAction newDiaCalEvent.ShowDialog() FinishProcessingButtonEvent(buttonName) End Sub Private Sub FinishProcessingButtonEvent(ByRef buttonName As Button) If frmCalMonthAction = "view" Then Exit Sub End If If frmCalMonthAction = "delete" Then nameOfButton(md_Idx, 0) -= 1 If nameOfButton(md_Idx, 0) < 0 Then buttonName.Visible = False Me.Refresh() Exit Sub Else Exit Sub End If End If 'Assumed frmCalMonthAction = "copy" and if date range not met, it's not on the ' frmCalMonth, so exit sub. If itemReturn.ItemDate < startDateToRetrieve _ Or itemReturn.ItemDate > finalDateToRetrieve Then Exit Sub End If InsertItemInCalendarDateControl(itemReturn) Me.Refresh() End Sub
Terry 01