?
Solved

Raising an Event WITH event arguments

Posted on 2004-09-17
5
Medium Priority
?
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
vigrid earned 2000 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:Volkan Vardar
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

801 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