473,385 Members | 2,210 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,385 software developers and data experts.

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 1502
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****************@TK2MSFTNGP09.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 SimpleControlExample

Public Class SimpleControl
Inherits System.Windows.Forms.UserControl

Public Event ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 68)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(76, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Raise Event!"
'
'SimpleControl
'
Me.Controls.Add(Me.Button1)
Me.Name = "SimpleControl"
Me.ResumeLayout(False)

End Sub

#End Region

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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:eu*************@TK2MSFTNGP10.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****************@TK2MSFTNGP09.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****************@tk2msftngp13.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 SimpleControlExample

Public Class SimpleControl
Inherits System.Windows.Forms.UserControl

Public Event ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 68)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(76, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Raise Event!"
'
'SimpleControl
'
Me.Controls.Add(Me.Button1)
Me.Name = "SimpleControl"
Me.ResumeLayout(False)

End Sub

#End Region

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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:eu*************@TK2MSFTNGP10.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****************@TK2MSFTNGP09.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****************@TK2MSFTNGP11.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*************@TK2MSFTNGP11.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****************@tk2msftngp13.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 SimpleControlExample

Public Class SimpleControl
Inherits System.Windows.Forms.UserControl

Public Event ButtonClick(ByVal sender As System.Object, ByVal e As
System.EventArgs)

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

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

'Add any initialization after the InitializeComponent()
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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 68)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(76, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Raise Event!"
'
'SimpleControl
'
Me.Controls.Add(Me.Button1)
Me.Name = "SimpleControl"
Me.ResumeLayout(False)

End Sub

#End Region

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

"Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
news:eu*************@TK2MSFTNGP10.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****************@TK2MSFTNGP09.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*************@tk2msftngp13.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****************@TK2MSFTNGP11.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*************@TK2MSFTNGP11.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****************@tk2msftngp13.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 SimpleControlExample
>
> Public Class SimpleControl
> Inherits System.Windows.Forms.UserControl
>
> Public Event ButtonClick(ByVal sender As System.Object, ByVal e
As
> System.EventArgs)
>
> #Region " Windows Form Designer generated code "
>
> Public Sub New()
> MyBase.New()
>
> 'This call is required by the Windows Form Designer.
> InitializeComponent()
>
> 'Add any initialization after the InitializeComponent()
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.Dispose()
> End If
> End If
> MyBase.Dispose(disposing)
> End Sub
>
> 'Required by the Windows Form Designer
> Private components As System.ComponentModel.IContainer
>
> '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.Diagnostics.DebuggerStepThrough()> Private Sub
> InitializeComponent()
> Me.Button1 = New System.Windows.Forms.Button
> Me.SuspendLayout()
> '
> 'Button1
> '
> Me.Button1.Location = New System.Drawing.Point(40, 68)
> Me.Button1.Name = "Button1"
> Me.Button1.Size = New System.Drawing.Size(76, 23)
> Me.Button1.TabIndex = 0
> Me.Button1.Text = "Raise Event!"
> '
> 'SimpleControl
> '
> Me.Controls.Add(Me.Button1)
> Me.Name = "SimpleControl"
> Me.ResumeLayout(False)
>
> End Sub
>
> #End Region
>
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> RaiseEvent ButtonClick(Me, e)
> End Sub
> End Class
> End Namespace
>
>
>
> "Paul Bromley" <fl*******@dsl.pipex.com> wrote in message
> news:eu*************@TK2MSFTNGP10.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****************@TK2MSFTNGP09.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
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...
6
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...
4
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,...
1
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...
3
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...
1
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...
9
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...
5
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)...
2
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...
5
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.