473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Problem handling Events from user control

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/de...singevents.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


Nov 21 '05 #1
5 1382
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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
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/de...singevents.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


Nov 21 '05 #2
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.

"Paul Bromley" wrote:
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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
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/de...singevents.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



Nov 21 '05 #3
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.

"Paul Bromley" wrote:
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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
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/de...singevents.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



Nov 21 '05 #4
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

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
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.

"Paul Bromley" wrote:
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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
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/de...singevents.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



Nov 21 '05 #5
Looks fine to me.

"Paul Bromley" wrote:
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

"TrtnJohn" <Tr******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
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.

"Paul Bromley" wrote:
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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:Ol**************@TK2MSFTNGP09.phx.gbl...
> 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/de...singevents.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
>
>
>
>
>
>


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tim Geiges | last post by:
Since I am being challenged with learning c# I figured I could pass some of the pain on to you guys :-) I have another question(this one is important for me to fix before I can get my app to Beta)...
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
3
by: Tim::.. | last post by:
Can someone please tell my why I get the following problem when I type the following piece of code! How do I get around this??? The idea is that when a user clicks a button on a form it causes...
5
by: batista | last post by:
Hi, I have created a user control in C#,and using it in one of my webpages. Now, i want to do something like this: call a function from user control(dll), but which is implemented in the...
9
by: Sridhar | last post by:
Hi, I have created a web page which includes a place holder. I also have a dropdown list in that webpage. when I select one of the choices in that dropdown list, It will load a user control...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
1
by: coderjoe | last post by:
I'm new to VB.Net and I'm using the 2003 version. I created a user control that has two labels on it. The two labels are side by side. The first is bold font and will be used to display a line...
6
by: Joel | last post by:
2 Questions: (1) The documentation says application.run() creates a standard message loop on the current thread and "optionally" shows a form. This is really confusing because I was of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.