Solved

Raising an Event WITH event arguments

Posted on 2004-09-17
5
561 Views
Last Modified: 2008-01-09
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)
0
Comment
Question by:MargusLehiste
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
vigrid earned 500 total points
ID: 12089429
You need to create your own XxxEventArgs class and use it as a second argument to the event. The class should store all the information specific to the event, such as last selected index number and cuurrent number. The benefit is obvious - you get the details about theevent, not only a notification. What would bevuse of information that the temperature has changed (for example) without telling to what temperature it did change. Using the sender/info combination is a standard in .NET.

RaiseEvent Location_Changed(Me, New XxxEventArgs(...))

It should look something like that. HTH
0
 
LVL 6

Expert Comment

by:vardium
ID: 12091092
declare
Public Event Location_Changed(ByVal sMyArg As String)

and raise
RaiseEvent Location_Changed("Argument here")

0
 
LVL 1

Author Comment

by:MargusLehiste
ID: 12092794
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)
0
 
LVL 1

Author Comment

by:MargusLehiste
ID: 12092804
m-w.com: exuberated with joy over solving the problem
0
 
LVL 4

Expert Comment

by:vigrid
ID: 12094764
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now