0

Just curious as to how to modify a dynamic variable after creation.

I figure, I could just store them into some sort of list.

But I do assign them a name and event and wanted to know when the event is triggered would it be possible to modify the item with its name ?(object sender)

Edit Clarification:

On run time create new items and associate them with events.

Image img = new Image();
img.name = "Image" + someIntValue;
img.MouseDown += new MouseButtonEventHandler(selectedImageClick);
someGrid.Children.add(img);
void selectedImageClick(object sender, MouseButtonEventArgs e)
{
   //Modify that image e.g: border      
}
2
  • 3
    Can you clarify your question with perhaps a code sample? Commented Mar 29, 2011 at 16:29
  • @Tejs, hope this clarifies things. Commented Mar 29, 2011 at 16:49

1 Answer 1

2

In order to modify the sender, you'll have to cast it. Your event handler would then look something like this:

void selectedImageClick(object sender, MouseButtonEventArgs e)
{
    Image img = sender as Image;
    if (img != null)  // In case someone calls this event handler with something other than an Image
    {
        //Modify that image e.g: border
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I figured as much, anyway for the object to be multiple types ?
Well, you could do some run-time reflection to get the type of the object and check to see if it implements the method/property you are trying to affect. Alternatively, you could use a nest of if/else's. Third, and this is probably what you really want, is that you can test for a base type. You may actually want to affect properties that are implemented by the Control class. I would caution sending more than one or two types to a single Event Handler as it can start to become a twisted mess of conditional statements, making it harder to debug in the future.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.