views:

209

answers:

1

I have a DropDownList whose SelectedIndexChanged event I set:

move.SelectedIndexChanged += Move;

public void Move(object sender, EventArgs e)
{
}

I'd like to create a class derived from EventArgs which is the argument "e" that gets passed to the Move method.

public void Move(object sender, EventArgs e)
{
  MyEventArgs my_e = (MyEventArgs)e;

  int Id = my_e.ID;
  position = my_e.position;
  etc;
}

Is that possible? If it were, I could give this class properties that I could use in the event:

I need to do this as I'd like to pass more information to the Move method than a DropDownList currently contains. I can put this information in the ID of the DropDownList but that is ugly and requires messy string parsing.

eg

ID = "Id_4_position_2"

Note: As requested by Developer Art

I am moving elements in a list. I need to know the old order and the new order. I can do that by using the DropDownList ID to store the old order and SelectedValue to store the new order. So actually, I have all I need but it seems inelegant to put an order in an ID. I also want to avoid custom events, seems too much work.

alt text

+1  A: 

That's not possible.

You cannot cast a parent class to a child class.

You mean something like this:

public class MyEventArgs : EventArgs
{
}

Then you have EventArgs e and you wish to typecast it to the MyEventArgs. That won't work.

A child class extends the parent class which means it has more meat. The parent class is narrower. There is no sensible way to somehow automatically extend a parent class object to become a child class object.


What if you subclass the UI control and add a custom property to it?

public class PositionDropDownList : DropDownList
{
    public int Id { get; set; }
    public int Position { get; set; }
}

You set these values when you generate controls for your form. Then you can get to them in your event:

public void Move(object sender, EventArgs e)
{
  PositionDropDownList ddl = (PositionDropDownList)sender;

  int Id = ddl.Id;
  position = ddl.Position;
}

Would something like that work for you?

Developer Art
Good point. Is there another way to achieve what I want?
Petras
Technically, it is possible to define your own events with your own desired event format. I'm not sure though you can tap into the control internals to substitute this event with yours.
Developer Art
Can you probably explain what extra information you need the control to pass in event arguments?
Developer Art
See additional comments above
Petras