Link to home
Start Free TrialLog in
Avatar of MargusLehiste
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_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlLocations.SelectedIndexChanged
        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
Avatar of vigrid
vigrid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Volkan Vardar
Volkan Vardar

declare
Public Event Location_Changed(ByVal sMyArg As String)

and raise
RaiseEvent Location_Changed("Argument here")

Avatar of MargusLehiste

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)
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.DataSource = CreateDataSource()
            RadioButtonList1.DataTextField = "StringValue"
            RadioButtonList1.DataValueField = "CurrencyValue"
            RadioButtonList1.DataBind()
        End If
    End Sub

    Sub Index_Changed(sender As Object, e As EventArgs)        
        Label1.Text = "You selected " & RadioButtonList1.SelectedItem.Text & _
            " with a value of : " & RadioButtonList1.SelectedItem.Value & "."
    End Sub
 
 </script>
 
 <body>
 
    <form runat=server>
 
       <asp:RadioButtonList id="RadioButtonList1"
            OnSelectedIndexChanged="Index_Changed"
            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.