Problem handling Events from user control

P

Paul Bromley

For some time now I have been struggling trying to understand how to handle
events originating in a User Control that I have designed when using this in
an application. Basically I need to respond to button clicks in my user
control. I have struggled trying to understand Delegates, aqnd m still
struggling. I then came across the following tutorial on MSDN and thought I
had 'cracked it'

http://msdn.microsoft.com/library/d...us/vbcn7/html/vbcondeclaringraisingevents.asp

I have used the follwoing code in my User Control to raise the event:-


Public Class ResultsControl
Inherits System.Windows.Forms.UserControl
Public Event Result(ByVal Percent As String)

Private Sub cmdPotassium_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPotassium.Click
RaiseEvent Result(txtPotassium.Text)
End Sub

I have used the following code in my application to handle the event:-

Imports InfoResultsControl
Public Class frmOriginal
Private WithEvents mResultsPasteIt As InfoResultsControl.ResultsControl

This works:-

Private Sub mResultsPasteIt_Result(ByVal Percent As String) Handles
ResultsControl.Result
MsgBox(Percent)
End Sub

This DOES NOT work

Private Sub mResultsPasteIt_Result(ByVal Percent As String) Handles
mResultsPasteIt.Result
MsgBox(Percent)
End Sub

From the article, I should be using the second option and the WithEvents
variable, but as stated above this does not work, whereas the first option
does work. What am I doing wrong??? Am I correct in pursuing this method, or
should I work harder on understanding delegates, and if the latter is there
an easy tutorial on the Internet?? I am a hobbyist programmer.

I notice that mResultsPasteIt_Result when displayed in the L dropdown box
has a lock icon by it - is this relevant and correct?


Many thanks

Paul Bromley
 
P

Paul Bromley

I have just read the article and realised that I needed to insert the
following into my code:-

Private Sub frmOriginal_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
mResultsPasteIt = New InfoResultsControl.ResultsControl

It is still NOT working however!!


Paul Bromley
 
G

Guest

In order to use the WithEvents variable it has to point to an actual instance
of the object you are catching events from. Just declaring it as the same
type is not enough.
 
G

Guest

More clarification:

The object you want the WithEvents variable to point to is the same instance
you dropped on the form. That is why yourNew
InfoResultsControl.ResultsControl didn't work either.

Typically you don't need to use WithEvents on UserControls dropped on a form
because the IDE automatically wires up the events for you. (Your working
case). The only time you really need is for standard VB classes that raise
events.
 
P

Paul Bromley

Many thanks for taking the time to help me - it makes a lot of sense what
you are saying to me. What is the best way for codiing and raising an event
in a user control?? Is the code that I have used suitable:-

i.e. - Declaring a Public event variable:-

Public Event Result(ByVal Percent As String)

Then raising ot:-
Private Sub cmdPotassium_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPotassium.Click
RaiseEvent Result(txtPotassium.Text)
End Sub

Is this a suitable way of coding an event in a user control??

Is there a better way??

Many thanks

Paul Bromley
 
G

Guest

Looks fine to me.

Paul Bromley said:
Many thanks for taking the time to help me - it makes a lot of sense what
you are saying to me. What is the best way for codiing and raising an event
in a user control?? Is the code that I have used suitable:-

i.e. - Declaring a Public event variable:-

Public Event Result(ByVal Percent As String)

Then raising ot:-
Private Sub cmdPotassium_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPotassium.Click
RaiseEvent Result(txtPotassium.Text)
End Sub

Is this a suitable way of coding an event in a user control??

Is there a better way??

Many thanks

Paul Bromley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top