MargusLehiste
asked on
Raising an Event WITH event arguments
I do get the part of how events, delegates and event handling works.
What I currently do is (it is a usercontrol class):
Public Class ucl_mpowLocations
...
Public Event Location_Changed(ByVal sender As System.Object)
...
Private Sub ddlLocations_SelectedIndex Changed(By Val sender As Object, ByVal e As System.EventArgs) Handles ddlLocations.SelectedIndex Changed
RaiseEvent Location_Changed(Me)
End Sub
...
My Question is that if I wanted my Event signature to look like this :
Public Event Location_Changed(ByVal sender As System.Object, E as eventArgs)
how would i raise it ?
And what would be the benefit of passing those arguments?
(Actually Im not really clear about those event args overall - only example ive "gotten" is in WinForms where u use form1_closing event and write e.cancel)
What I currently do is (it is a usercontrol class):
Public Class ucl_mpowLocations
...
Public Event Location_Changed(ByVal sender As System.Object)
...
Private Sub ddlLocations_SelectedIndex
RaiseEvent Location_Changed(Me)
End Sub
...
My Question is that if I wanted my Event signature to look like this :
Public Event Location_Changed(ByVal sender As System.Object, E as eventArgs)
how would i raise it ?
And what would be the benefit of passing those arguments?
(Actually Im not really clear about those event args overall - only example ive "gotten" is in WinForms where u use form1_closing event and write e.cancel)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Vigrid - your comment was very interesting.
I would be extremely exhuberated if you could also provide me an example how to accomplish what you mentioned.
Basically the class ucl_mpowLocations is a userControl. Inside it I have DropDowList for selecting location.
So how would I add my own event arguments to that list -
through those arguments I want to provide OLD location, the New (Selected location) and also a mechanism to reverse the selection (lets say some users dont have authority to change location)
I would be extremely exhuberated if you could also provide me an example how to accomplish what you mentioned.
Basically the class ucl_mpowLocations is a userControl. Inside it I have DropDowList for selecting location.
So how would I add my own event arguments to that list -
through those arguments I want to provide OLD location, the New (Selected location) and also a mechanism to reverse the selection (lets say some users dont have authority to change location)
ASKER
m-w.com: exuberated with joy over solving the problem
This is how your custom event argument class would look like:
Public Class LocationChangedEventArgs
Inherits EventArgs
Private oldLoc As Integer
Private newLoc As Integer
Public Sub New(oldLoc As Integer, newLoc As Integer)
Me.oldLoc = oldLoc
Me.newLoc = newLoc
End Sub
Public ReadOnly Property OldLocation() As Integer
Get
Return oldLoc
End Get
End Property
Public ReadOnly Property NewLocation() As Integer
Get
Return newLoc
End Get
End Property
End Class
I don't understand, how would you like to add a mechanism to reverse the selection. i can't think of a way to pass a mechanism to the event argument, sorry. Maybe I misunderstood you?
Whenever an user changes the location in the DropDownList in your complex control, you have to be aware of this event. That means that you basically have to handle the DropDownList's SelectedIndexChanged event triggers (just add support to it using VS.NET designer - the simple way - select Properties of the DDL, click on the events button in the properties area, and double click the required event).
Some reading: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsListControlClassSelectedIndexChangedTopic.asp
And a piece of code that the online MSDN is missing (unfortunately it's spaghetti code):
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script language="VB" runat="server">
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("DateTimeValue" , GetType(DateTime)))
dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))
dt.Columns.Add(New DataColumn("CurrencyValue" , GetType(Double)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = DateTime.Now
If (i Mod 2) <> 0 Then
dr(3) = True
Else
dr(3) = False
End If
dr(4) = 1.23 *(i + 1)
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
RadioButtonList1.DataSourc e = CreateDataSource()
RadioButtonList1.DataTextF ield = "StringValue"
RadioButtonList1.DataValue Field = "CurrencyValue"
RadioButtonList1.DataBind( )
End If
End Sub
Sub Index_Changed(sender As Object, e As EventArgs)
Label1.Text = "You selected " & RadioButtonList1.SelectedI tem.Text & _
" with a value of : " & RadioButtonList1.SelectedI tem.Value & "."
End Sub
</script>
<body>
<form runat=server>
<asp:RadioButtonList id="RadioButtonList1"
OnSelectedIndexChanged="In dex_Change d"
AutoPostBack="true"
runat="server"/>
<br>
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
I would try the above and try to get a glimpse of how the events stuff works. Sorry, I'm not experienced in using VB.NET and I can't provide a working example in VB.NET that you could trust. If you would go for a C# code, I'd be glad to help you out.
Public Class LocationChangedEventArgs
Inherits EventArgs
Private oldLoc As Integer
Private newLoc As Integer
Public Sub New(oldLoc As Integer, newLoc As Integer)
Me.oldLoc = oldLoc
Me.newLoc = newLoc
End Sub
Public ReadOnly Property OldLocation() As Integer
Get
Return oldLoc
End Get
End Property
Public ReadOnly Property NewLocation() As Integer
Get
Return newLoc
End Get
End Property
End Class
I don't understand, how would you like to add a mechanism to reverse the selection. i can't think of a way to pass a mechanism to the event argument, sorry. Maybe I misunderstood you?
Whenever an user changes the location in the DropDownList in your complex control, you have to be aware of this event. That means that you basically have to handle the DropDownList's SelectedIndexChanged event triggers (just add support to it using VS.NET designer - the simple way - select Properties of the DDL, click on the events button in the properties area, and double click the required event).
Some reading: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsListControlClassSelectedIndexChangedTopic.asp
And a piece of code that the online MSDN is missing (unfortunately it's spaghetti code):
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<html>
<script language="VB" runat="server">
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue",
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("DateTimeValue"
dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))
dt.Columns.Add(New DataColumn("CurrencyValue"
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = DateTime.Now
If (i Mod 2) <> 0 Then
dr(3) = True
Else
dr(3) = False
End If
dr(4) = 1.23 *(i + 1)
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
RadioButtonList1.DataSourc
RadioButtonList1.DataTextF
RadioButtonList1.DataValue
RadioButtonList1.DataBind(
End If
End Sub
Sub Index_Changed(sender As Object, e As EventArgs)
Label1.Text = "You selected " & RadioButtonList1.SelectedI
" with a value of : " & RadioButtonList1.SelectedI
End Sub
</script>
<body>
<form runat=server>
<asp:RadioButtonList id="RadioButtonList1"
OnSelectedIndexChanged="In
AutoPostBack="true"
runat="server"/>
<br>
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
I would try the above and try to get a glimpse of how the events stuff works. Sorry, I'm not experienced in using VB.NET and I can't provide a working example in VB.NET that you could trust. If you would go for a C# code, I'd be glad to help you out.
Public Event Location_Changed(ByVal sMyArg As String)
and raise
RaiseEvent Location_Changed("Argument