tags:

views:

30

answers:

1

I have a couple of textblock controls and all of them are calling a single event. But i have to wire all of them invidividually using

TextBlock1.MouseMove += new MouseEventHandler(TextBlock_MouseMove);    
TextBlock2.MouseMove += new MouseEventHandler(TextBlock_MouseMove);   
TextBlock3.MouseMove += new MouseEventHandler(TextBlock_MouseMove); 

Is there a way that I can apply the mouse move to all TextBlock without wiring them one by one

+1  A: 

You can loop through all child controls of the form and add the event handler to each of the textblocks you find. If those textblocks are in the same grid/whatever, the code should look a bit like this:

foreach(TextBlock vControl in GridName.Children)
{
    vControl.MouseMove += new MouseEventHandler(TextBlock_MouseMove);
}

Now I might have the 'foreach' part of it wrong. It might need to be cast from an object depending on how they are stored in the Children collection. While this will be good if you end up with a lot of textboxes, you can also style the textboxes to issue a command on mouse move, but that requires a pile of code up front, for a much easier time at doing it.

Rangoric
This code assumes all TextBlocks of interest are direct children of a specific panel and that all TextBlocks in the panel are of interest.
AnthonyWJones