Enable / Disable buttons based on selected control

I have a very complex .NET application that contains cut / copy / paste functions. I want to enable / disable cut / copy / paste buttons depending on the selected control / content. There are many custom controls in the application. What is the best way to achieve this?

thank

+1


source to share


6 answers


Are you talking about catching the focus of a control or a design pattern that does it?

I would probably register each control to be enabled / disabled with the control it depends on. Then, when the dependent control is selected / focused, scroll through the controls and enable / disable each one. This is pretty much an 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...

+1


source


One approach:

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



eg.: textBox.GotFocus += myFunctionalityEnabler;

0


source


Have a look at using tags and LINQ

You can use a tag object to store information and then query the controls for that form / application using LINQ. Based on your results, you can set properties as needed. This is perhaps a better approach than the observer model, as if it fails, it can lead to memory leaks and performance degradation.

Just a suggestion.

0


source


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

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

0


source


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

You could put a flag in the Tag field that you could check, or you could just check if it is a text field or something else that you are doing.

This will allow you to make the buttons do nothing when the wrong control is selected. If you want to actually disable the buttons, you can do the same validation but in the MouseDown / KeyPress events for the form, or set an event handler for GotFocus / LostFocus for each control.

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

0


source


Create IControlMyForm interface

Add DisableCutCopyPaste Method

Add a method called EnableCutCopyPaste

Have a form that implements an interface.

Create class UI_Controller

Create a property of type IControlMyForm

Create Disable and Permissions for Paste Copy Button

At startup, instantiate UI_Controller

When launched, the form must be bound to a property.

Using the OnFocus (or Click, etc.) event from another control has the correct method of the UI_Controller class being called.

This is overkill if all you are doing is cutting and pasting on and off. But if your form is that complex, you'd be better off defining an interface and putting some of the more complex interactions (no matter what they are) in the UI_Controller class. This way you can clearly see what to do. Also refactoring your form becomes much easier.

If you feel like this is too much, just create an EnableCutCopyPaste procedure and a DisableCutCopyPaste procedure and put the calls in the OnFocus event (or click, etc.).

0


source







All Articles