473,625 Members | 3,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event in a User Control

Can someone give me a very simple example on how to do this? As an example I
have a commaned button in a user control. Once this user control is placed
on a form I want to be able to respond in the form to the button click on
the user control. In a simple example - how do I do this. Do I need to use
Delegates, and if so can you give a very simple example.

Many thanks

Paul Bromley
Nov 20 '05 #1
5 1515
I forgot to ad to this, that when I click the button on my usercontrol, I
would like to call a subroutine in my project and send to it 2 string
parameters. I have invested a lot of time on user controls and delegates and
am about to give up!

Best wishes

Paul Bromley

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Can someone give me a very simple example on how to do this? As an example I have a commaned button in a user control. Once this user control is placed
on a form I want to be able to respond in the form to the button click on
the user control. In a simple example - how do I do this. Do I need to use
Delegates, and if so can you give a very simple example.

Many thanks

Paul Bromley

Nov 20 '05 #2
If you'd like to send some extra information in your events (your 2 strings)
you'll have to create a new class that inherits from the EventArgs class.
Please check out this MSDN link, if you have problems implementing it let me
know.
http://msdn.microsoft.com/library/de...classtopic.asp
Here's a very basic control that raises an event.

Namespace SimpleControlEx ample

Public Class SimpleControl
Inherits System.Windows. Forms.UserContr ol

Public Event ButtonClick(ByV al sender As System.Object, ByVal e As
System.EventArg s)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(40, 68)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(76, 23)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Raise Event!"
'
'SimpleControl
'
Me.Controls.Add (Me.Button1)
Me.Name = "SimpleCont rol"
Me.ResumeLayout (False)

End Sub

#End Region

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
RaiseEvent ButtonClick(Me, e)
End Sub
End Class
End Namespace

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:eu******** *****@TK2MSFTNG P10.phx.gbl...
I forgot to ad to this, that when I click the button on my usercontrol, I
would like to call a subroutine in my project and send to it 2 string
parameters. I have invested a lot of time on user controls and delegates and am about to give up!

Best wishes

Paul Bromley

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Can someone give me a very simple example on how to do this? As an example
I
have a commaned button in a user control. Once this user control is

placed on a form I want to be able to respond in the form to the button click on the user control. In a simple example - how do I do this. Do I need to use Delegates, and if so can you give a very simple example.

Many thanks

Paul Bromley


Nov 20 '05 #3
Thanks Andy,

I have now managed to raise an event in my project - just need to sort out
how to get this to call my subroutine and send the strings. One thing that I
had not dones is to create a Namespace for my control and then use an
Imports Statement in my project. I had better get off to bed now! If anyone
else can help then I'd be very grateful. Perhaps I will sort things
tomorrow.

Best wishes

Paul Bromley
"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
If you'd like to send some extra information in your events (your 2 strings) you'll have to create a new class that inherits from the EventArgs class.
Please check out this MSDN link, if you have problems implementing it let me know.
http://msdn.microsoft.com/library/de...classtopic.asp

Here's a very basic control that raises an event.

Namespace SimpleControlEx ample

Public Class SimpleControl
Inherits System.Windows. Forms.UserContr ol

Public Event ButtonClick(ByV al sender As System.Object, ByVal e As
System.EventArg s)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(40, 68)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(76, 23)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Raise Event!"
'
'SimpleControl
'
Me.Controls.Add (Me.Button1)
Me.Name = "SimpleCont rol"
Me.ResumeLayout (False)

End Sub

#End Region

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
RaiseEvent ButtonClick(Me, e)
End Sub
End Class
End Namespace

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:eu******** *****@TK2MSFTNG P10.phx.gbl...
I forgot to ad to this, that when I click the button on my usercontrol, I
would like to call a subroutine in my project and send to it 2 string
parameters. I have invested a lot of time on user controls and delegates

and
am about to give up!

Best wishes

Paul Bromley

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Can someone give me a very simple example on how to do this? As an

example
I
have a commaned button in a user control. Once this user control is

placed on a form I want to be able to respond in the form to the button click on the user control. In a simple example - how do I do this. Do I need to use Delegates, and if so can you give a very simple example.

Many thanks

Paul Bromley



Nov 20 '05 #4
Hi Andy,

Many thanks for your input so far on this one. The MSDN link was useful and
fortunately I can understand it. One thing that I am still having problems
with though - the Usercontrol should be self-contained' and needs to raise
an event in an extrenal Sub-routine that is within my project - so the
question is - how do I reference that sub-routine fom the control, or am I
missing something here?? I can now see how to pass the parameters OK - just
cannot reference the external sub-routine.

Can anyone else suggest??

Best wishes

Paul Bromley
"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Don't worry about the namespace thing - I screwed up when I originally
created the solution. I came up with a sample project for you (without
namespaces :). I think you'll be able to follow what's going on, but if you have any questions about it let me know.

Good luck!
-Andy

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:OU******** *****@TK2MSFTNG P11.phx.gbl...
Thanks Andy,

I have now managed to raise an event in my project - just need to sort out
how to get this to call my subroutine and send the strings. One thing that
I
had not dones is to create a Namespace for my control and then use an
Imports Statement in my project. I had better get off to bed now! If anyone
else can help then I'd be very grateful. Perhaps I will sort things
tomorrow.

Best wishes

Paul Bromley
"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
If you'd like to send some extra information in your events (your 2

strings)
you'll have to create a new class that inherits from the EventArgs

class. Please check out this MSDN link, if you have problems implementing it let
me
know.

http://msdn.microsoft.com/library/de...classtopic.asp


Here's a very basic control that raises an event.

Namespace SimpleControlEx ample

Public Class SimpleControl
Inherits System.Windows. Forms.UserContr ol

Public Event ButtonClick(ByV al sender As System.Object, ByVal e As
System.EventArg s)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeCompo nent()

'Add any initialization after the InitializeCompo nent()
call
End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.Button1 = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(40, 68)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(76, 23)
Me.Button1.TabI ndex = 0
Me.Button1.Text = "Raise Event!"
'
'SimpleControl
'
Me.Controls.Add (Me.Button1)
Me.Name = "SimpleCont rol"
Me.ResumeLayout (False)

End Sub

#End Region

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
RaiseEvent ButtonClick(Me, e)
End Sub
End Class
End Namespace

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:eu******** *****@TK2MSFTNG P10.phx.gbl...
> I forgot to ad to this, that when I click the button on my
usercontrol,
I
> would like to call a subroutine in my project and send to it 2

string > parameters. I have invested a lot of time on user controls and

delegates and
> am about to give up!
>
> Best wishes
>
> Paul Bromley
>
>
>
>
>
> "Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
> news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
> > Can someone give me a very simple example on how to do this? As an
example
> I
> > have a commaned button in a user control. Once this user control is placed
> > on a form I want to be able to respond in the form to the button click on
> > the user control. In a simple example - how do I do this. Do I
need to use
> > Delegates, and if so can you give a very simple example.
> >
> > Many thanks
> >
> > Paul Bromley
> >
> >
>
>



Nov 20 '05 #5
You don't really want your control to reference a specific sub because now
that sub would be tightly bound to that control. Rather, interested parties
listen for your event, and react accordingly.

Did you see the code that was in the Form (SimpleControl_ Click) ?

Could you call your sub from there? I might need to know more details about
your specific project if that doesn't make sense to you.

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:OB******** *****@tk2msftng p13.phx.gbl...
Hi Andy,

Many thanks for your input so far on this one. The MSDN link was useful and fortunately I can understand it. One thing that I am still having problems
with though - the Usercontrol should be self-contained' and needs to raise
an event in an extrenal Sub-routine that is within my project - so the
question is - how do I reference that sub-routine fom the control, or am I
missing something here?? I can now see how to pass the parameters OK - just cannot reference the external sub-routine.

Can anyone else suggest??

Best wishes

Paul Bromley
"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Don't worry about the namespace thing - I screwed up when I originally
created the solution. I came up with a sample project for you (without
namespaces :). I think you'll be able to follow what's going on, but if you
have any questions about it let me know.

Good luck!
-Andy

"Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
news:OU******** *****@TK2MSFTNG P11.phx.gbl...
Thanks Andy,

I have now managed to raise an event in my project - just need to sort out how to get this to call my subroutine and send the strings. One thing that
I
had not dones is to create a Namespace for my control and then use an
Imports Statement in my project. I had better get off to bed now! If

anyone
else can help then I'd be very grateful. Perhaps I will sort things
tomorrow.

Best wishes

Paul Bromley
"Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
> If you'd like to send some extra information in your events (your 2
strings)
> you'll have to create a new class that inherits from the EventArgs

class.
> Please check out this MSDN link, if you have problems implementing it
let
me
> know.
>

http://msdn.microsoft.com/library/de...classtopic.asp >
>
> Here's a very basic control that raises an event.
>
> Namespace SimpleControlEx ample
>
> Public Class SimpleControl
> Inherits System.Windows. Forms.UserContr ol
>
> Public Event ButtonClick(ByV al sender As System.Object, ByVal e
As
> System.EventArg s)
>
> #Region " Windows Form Designer generated code "
>
> Public Sub New()
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeCompo nent()
>
> 'Add any initialization after the InitializeCompo nent()
call >
> End Sub
>
> 'UserControl overrides dispose to clean up the component
list. > Protected Overloads Overrides Sub Dispose(ByVal disposing As
> Boolean)
> If disposing Then
> If Not (components Is Nothing) Then
> components.Disp ose()
> End If
> End If
> MyBase.Dispose( disposing)
> End Sub
>
> 'Required by the Windows Form Designer
> Private components As System.Componen tModel.IContain er
>
> 'NOTE: The following procedure is required by the Windows Form > Designer
> 'It can be modified using the Windows Form Designer.
> 'Do not modify it using the code editor.
> Friend WithEvents Button1 As System.Windows. Forms.Button
> <System.Diagnos tics.DebuggerSt epThrough()> Private Sub
> InitializeCompo nent()
> Me.Button1 = New System.Windows. Forms.Button
> Me.SuspendLayou t()
> '
> 'Button1
> '
> Me.Button1.Loca tion = New System.Drawing. Point(40, 68)
> Me.Button1.Name = "Button1"
> Me.Button1.Size = New System.Drawing. Size(76, 23)
> Me.Button1.TabI ndex = 0
> Me.Button1.Text = "Raise Event!"
> '
> 'SimpleControl
> '
> Me.Controls.Add (Me.Button1)
> Me.Name = "SimpleCont rol"
> Me.ResumeLayout (False)
>
> End Sub
>
> #End Region
>
> Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
> System.EventArg s) Handles Button1.Click
> RaiseEvent ButtonClick(Me, e)
> End Sub
> End Class
> End Namespace
>
>
>
> "Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
> news:eu******** *****@TK2MSFTNG P10.phx.gbl...
> > I forgot to ad to this, that when I click the button on my

usercontrol,
I
> > would like to call a subroutine in my project and send to it 2 string > > parameters. I have invested a lot of time on user controls and

delegates
> and
> > am about to give up!
> >
> > Best wishes
> >
> > Paul Bromley
> >
> >
> >
> >
> >
> > "Paul Bromley" <fl*******@dsl. pipex.com> wrote in message
> > news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
> > > Can someone give me a very simple example on how to do this? As
an > example
> > I
> > > have a commaned button in a user control. Once this user control

is > placed
> > > on a form I want to be able to respond in the form to the button

click
> on
> > > the user control. In a simple example - how do I do this. Do I

need
to
> use
> > > Delegates, and if so can you give a very simple example.
> > >
> > > Many thanks
> > >
> > > Paul Bromley
> > >
> > >
> >
> >
>
>



Nov 20 '05 #6

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

Similar topics

3
2557
by: Tim Thomas | last post by:
Hi, I am very new to .NET and am in the process of building my first web application. I will briefly describe what i am trying to achieve: I have a system where suppliers register their details, their locations, and then add themselves to categories. Each category requires additional info from the suppliers, this additional category info is stored in its own DB table. a suppliers may add themselves to as many categories as required....
6
3366
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
4
3015
by: Jordan | last post by:
I need to dynamically add an ImageButton control to a user control and and do some server-side processing when the user clicks it. While I the ImageButton is added to the user control at runtime, as needed, I'm having trouble wiring up its click event procedure. The problem is that when I go to subscribe the ImageButton to the delegate, the ImageButton is <undefined> (i.e., throws the "object not found" exception). Note that I'm using...
1
3452
by: Marek Murin | last post by:
Hi all, I have created vb.net user control that has to be used by vb6 form. Everything goes well with putting the vb.net user control on the VB6 form until I want to receive any event from my control. The event handler is displayed on VB6 IDE combo and you can create a sub for it as usual, but when I run the vb6 form to test it, it won't work. I have spent too much time til now with that without finding solution. Can anybody help me ?
3
1310
by: Phillip N Rounds | last post by:
In diagnosing a problem, I noted that a button_click event gets run only after the page Page_Load event of the post back. (VS 2003, ASPNET 1.1, C#) Can this be correct? I'm trying to set session variables in a Button_Click event which determines how the page is supposed to appear in the PostBack, so of course it fails. Where should I put my code so that the session variables are re-set before the PostBack?
1
5248
by: David Veeneman | last post by:
I am writing a control that relies on its host to validate the contents of one of its fields. The control fires a custom 'FooNeedsValidating' event and passes the field's data with the event. The host handles the event, validates the data and returns the validation results to the control by a callback to a control method, SetFooError(). All of that works great. If the host returns false on the validation, I display an error glyph next to...
9
2465
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the post event will always execuate after the system is completed... - I want to wrap this functionality in a framework, so I could possibly have 3 or 4 levels of inherited objects that need to have these pre / post events executed before and after the...
5
2157
by: rn5a | last post by:
Consider the following user control which resides in Address.ascx: <script runat="server"> Public Property Address() As String Get Address = txtAddress.Text End Get Set(ByVal value As String) txtAddress.Text = value End Set
2
2315
by: ccbalapatel | last post by:
Hi, We have recently converted our code to ASP.NET 2.0. We have an user control that is embedded inside the page. The user control has a link button and the event handler for the link button is written inside the user control. When the link button is clicked, the page is posted back and the page_load method of the user control also executes fine. But, the event handler for the link button is not called. The same code works fine on ASP.NET...
5
3071
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc and the specific type of control frequently changes. I have a common event in the base class that each child user control inherits from and that the page wires up and uses to get status from the child user control. I hope all pretty simple and not...
0
8192
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8696
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8502
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7188
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6119
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.