Question

I am currently working on a VB.net WinForm project (a calculator). My question is regarding composite user controls. I have created a composite user control which includes about 30 buttons. From another class, is it possible to know when the user clicks on a specific button ("EqlBut" in my case)? I have tried the following without success. Any suggestions?

The code below is found in my Calc.vb class which imports my composite control CalcCompCtrl.vb

Imports MyCalc.CalcCompCtrl

Public Class Calc
Sub EqlBut_Click(sender As System.Object, e As System.EventArgs) Handles EqlBut.Click
    'do stuff when user clicks button labeled EqlBut in composite user control
End Sub
End Class

It does not like the EqlBut.Click and says: Handles clause requires a WithEvents variable defined in the containing type or one of its bases.

Public Class CalcCompCtrl

Public Sub EqlBut_Click(sender As System.Object, e As System.EventArgs) Handles EqlBut.Click
    If ModeSel.SelectedIndex = iDecimal Then
      Select Case sMathOp
        Case "+"
      End Select
    End If
End Sub
End Class
Was it helpful?

Solution

You could Look into using custom events in the Control.

To Declare a Event You would Do Something Like This.

'This Would Be Done At the Global Level in the Control
'Parameters Can Be Anything that you want your event handler to have access to.

Public Event TotalCalculated(ByVal total as Integer)

Then To Raise The Event Your Code in your control Would Look Like this

Sub EqlBut_Click(sender As System.Object, e As System.EventArgs) Handles EqlBut.Click
    Dim total as Integer = SomeNumber
    RaiseEvent TotalCalculated(total)
End Sub

Then In your Code that uses your Control

Sub TotalCalculated(ByVal total as Integer) Handles CalcCompCtrl.TotalCalculated
    'Do Stuff Each Time The Total Calculated Event is Raised
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top