views:

48

answers:

3

I have 2 controls (MyCtrl) next to each other called ctrlLeft and ctrlRight.

Whenever one receives interaction it is considered active (by default the left one). I override OnDraw and customize the look of the active one a bit.

Currently I have a property Active and an event that I subscribe to from all MyCtrl in there I store a reference to the active one like this:

if (sender is MyCtrl) 
{
    ctrlActive = (sender as MyCtrl);
    ctrlLeft.Active = !(ctrlRight.Active = (ctrlActive == ctrlRight));
}

Either way I need to have ctrlActive as I use it for other things but what I am wondering is if this is the best way make them aware of each other?

Another option I thought of was to store references to every possible MyCtrl and then loop through em all and activate / deactivate the one that match sender just in case I in the future add a ctrlMiddle.

Are my thoughts wrong, is there better options to do this. For example, how does radiobuttons accomplish their similar functionality?

Edit: Thanks for all suggestions.

In my particular case I don't want/need a container as even if I have 3 MyCtrl only one can still be active and I don't want them to be "linked" 2 and 2 so I went with a public static MyCtrl { get; set; } that each control can check itself against and I can update it where I need to, which works in my case and rids me of the need to loop through a collection when using multiple MyCtrl.

A: 

You have to manage the activation by yourself, so I think that your method is good.

Maurizio Reginelli
+1  A: 

Your methods are sound. If you need multiple controls with only one being active consider a container (visual or otherwise) where the children supply an "activate" event TO the container and the children also subscribe to a "control activated" event FROM the container....if the child is not the control supplied by the containers "control activated" event..then paint it as not active otherwise paint as active.

Rusty
A: 

I nearly had the same idea as Rusty. But i would make it a little more general.

Why not building a container control that contains two panels (like SplitContainer). The container control has a property Active of type enum ActivePanel { First, Second } and it can be switched from outside by a setter or automatically through a subscription of the container to the Focus event (or something similar).

With such an approach you also don't need a ctrlMiddle cause you can nest your container multiple times.

I think there are still some problems to solve by this idea, but it should give you a good direction.

Oliver