tags:

views:

667

answers:

6

I have a very complicated .NET application that contains cut/copy/paste functionality. I want to enable/disable cut/copy/paste buttons depending on the selected control/content. The app has many user controls. What is the best way to achieve this?

Thanks

A: 

One approach:

Add a delegate to the appropriate event for each control you wish to monitor.

e.g. : textBox.GotFocus += myFunctionalityEnabler;

grepsedawk
imagine the amount of code that should I write....
+1  A: 

Are you talking about catching the focus of the control or about a design pattern in which to do this?

I would probably register each control that is to be enabled/disabled with the control that it's dependent on. Then when the dependent control gets selected/focused, spin through the controls and enable/disable each one. This is pretty much the observer pattern.

For example:

In OnInit:

selectedControl1.AddObserver(button1);
selectedControl1.AddObserver(button2);
selectedControl1.AddObserver(textBox1);

selectedControl2.AddObserver(button1);
selectedControl2.AddObserver(textbox3);

In OnFocused:

foreach(Control ctl in selectedControl1.Observers)
{
  ctl.Enabled = true;
}

Something like that...

Mike Hall
A: 

Look at using Tags and LINQ

You can utilized the tag object to hold information, then query the controls for that form/application using LINQ. Based on your results you can then set the properties as necessary. This maybe a better approach than the observer model, as if this isn't managed will it may lead to memory leaks and degragation on performance.

Just a suggestion.

A: 

One approach: Create an ExtenderProvider to add the properties Cut, Copy and Paste to every control. Add a delegate to the GotFocus event of every control and enable/disable each button in the delegate based on the extended properties values.

Another approach: Add a delegate to the GotFocus event to every control in the form using a recursive function in the Form_Load and enable/disable each button based on the control type.

Eduardo Campañó
A: 

You could just check the Form.ActiveControl property in the OnClick event in your Copy/Paste buttons.

You could put a flag in the Tag field that could be checked or you could just check whether it's a TextBox or whatever it is that your doing.

This would allow you to make the buttons not do anything when the wrong control is selected. If you want to actually disable the buttons you could do the same check but in the MouseDown/KeyPress events for the Form or set an event handler for GotFocus/LostFocus on every control.

If the check you want is whether the control is a TextBox or something like that, and you have alot of controls and you don't want to set the handler on each control manually, you could add the handler in a foreach over Form.Controls in the FormLoad event.

thealliedhacker
A: 

Create an Interface IControlMyForm

Add a method called DisableCutCopyPaste

Add a method called EnableCutCopyPaste

Have the form implement the Interface.

Create a UI_Controller class

Create a property of type IControlMyForm

Create a disable and a enable method for the Cut Copy Paste button

On startup creat an instance of UI_Controller

On startup have the Form set itself to the property.

Using the OnFocus event (or Click, etc) of the various control have a call to the right method of the UI_Controller Class.

This is overkill if all you are doing is enabling and disabling cut copy and paste. But if your form is that complex then you would be better off defining a interface and putting some of the more complex interactions (whatever they are) in a UI_Controller class. This way you clearly see what doing what. Also refactoring your form becomes a lot easier.

If you feel this is overkill then just create a EnableCutCopyPaste routine and a DisableCutCopyPaste routine and place the calls in the OnFocus event (or Click, etc).

RS Conley