in Education by
I'm a newbie in C# bu I'm experienced Delphi developer. In Delphi I can use same code for MenuItem and ToolButton using TAction.OnExecute event and I can disable/enable MenuItem and ToolButton together using TAction.OnUpdate event. Is there a similar way to do this in C# without using external libraries? Or more - How C# developers share code between different controls? Ok, may be I write my question in wrong way. I want to know not witch property to use (I know about Enabled property) but I want to know on witch event I should attach to if I want to enable/disable more than one control. In delphi TAction.OnUpdate event ocurs when Application is idle - is there similar event in C#? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Try the a modification of the command pattern: public abstract class ToolStripItemCommand { private bool enabled = true; private bool visible = true; private readonly List controls; protected ToolStripItemCommand() { controls = new List(); } public void RegisterControl(ToolStripItem item, string eventName) { item.Click += delegate { Execute(); }; controls.Add(item); } public bool Enabled { get { return enabled; } set { enabled = value; foreach (ToolStripItem item in controls) item.Enabled = value; } } public bool Visible { get { return visible; } set { visible = value; foreach (ToolStripItem item in controls) item.Visible = value; } } protected abstract void Execute(); } Your implementations of this command can be stateful in order to support your view's state. This also enables the ability to build "undo" into your form. Here's some toy code that consumes this: private ToolStripItemCommand fooCommand; private void wireUpCommands() { fooCommand = new HelloWorldCommand(); fooCommand.RegisterControl(fooToolStripMenuItem, "Click"); fooCommand.RegisterControl(fooToolStripButton, "Click"); } private void toggleEnabledClicked(object sender, EventArgs e) { fooCommand.Enabled = !fooCommand.Enabled; } private void toggleVisibleClicked(object sender, EventArgs e) { fooCommand.Visible = !fooCommand.Visible; } HelloWorldCommand: public class HelloWorldCommand : ToolStripItemCommand { #region Overrides of ControlCommand protected override void Execute() { MessageBox.Show("Hello World"); } #endregion } It's unfortunate that Control and ToolStripItem do not share a common interface since they both have "Enabled" and "Visible" properties. In order to support both types, you would have to composite a command for both, or use reflection. Both solutions infringe on the elegance afforded by simple inheritance.

Related questions

0 votes
    I'm a newbie in C# bu I'm experienced Delphi developer. In Delphi I can use same code for ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I'm a newbie in C# bu I'm experienced Delphi developer. In Delphi I can use same code for ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    I am using log4net in a C# project, in the production environment, I want to disable all the logging ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I am using log4net in a C# project, in the production environment, I want to disable all the logging ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    how can i disable the legend in the telerik Radchart? This legend is at the left side of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    What are the procedures used to enable or disable an application in the ServiceNow?...
asked May 24, 2021 in Technology by JackTerrance
0 votes
    (This post isn't helpful) I'm using this code in onCreateOptionsMenu to enable/disable menu items in a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 26, 2022 in Education by JackTerrance
0 votes
    (This post isn't helpful) I'm using this code in onCreateOptionsMenu to enable/disable menu items in a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I have a Fragment1, and i am make newInstance of Fragment1 and start in Fragment1, i.e. my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    This question's answers are a community effort. Edit existing answers to improve this post. It is not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I want to make a method that will populate the lst_List list with rows from various tables and fields. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I want to make a method that will populate the lst_List list with rows from various tables and fields. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    I'm writing an app for Android in Xamarin Forms. I want to navigate the user from one page to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
...