What is C#'s analogue of Delphi's TAction ?

O

Oleg Subachev

What is C#'s analogue of Delphi's TAction ?

If there is no one, how to implement the same functionality >

Oleg Subachev
 
J

Jon Skeet [C# MVP]

Oleg said:
What is C#'s analogue of Delphi's TAction ?

If there is no one, how to implement the same functionality

It would help if you could say what TAction does - otherwise you've
limited yourself to those who know both Delphi and C#.

Joanna? :)

Jon
 
O

Oleg Subachev

It would help if you could say what TAction does

TAction is a class that mainly has two events:
OnExecute - where some real job may be done
and
OnUpdate - where the instance may be enabled/disabled.

Then the TAction instance may be assigned to the special
property of different controls, such as menu item, button,
speed button etc. So that all these controls will perform
the same job on clicking. Also these controls will be
simultaneously enabled/disabled and may share common
Caption, Hint and other properties.

Oleg Subachev
 
J

Jon Skeet [C# MVP]

Oleg said:
TAction is a class that mainly has two events:
OnExecute - where some real job may be done
and
OnUpdate - where the instance may be enabled/disabled.

Then the TAction instance may be assigned to the special
property of different controls, such as menu item, button,
speed button etc. So that all these controls will perform
the same job on clicking. Also these controls will be
simultaneously enabled/disabled and may share common
Caption, Hint and other properties.

I don't believe there's anything similar in the .NET framework.
However, you could create a class which aggregated a list of controls,
and allowed you to manipulate them (enable/disable etc) as a group
(just by looping through the list when you asked the instance to do
something).

Jon
 
J

Joanna Carter [TeamB]

"Jon Skeet [C# MVP]" <[email protected]> a écrit dans le message de (e-mail address removed)...

| It would help if you could say what TAction does - otherwise you've
| limited yourself to those who know both Delphi and C#.
|
| Joanna? :)

You rang ?

TAction is a non-visual component that can be added to a TActionList using a
component editor. Basically, controls like buttons and menu items have an
Action property and a TAction provides a RAD way of specifying what happens
when button or menu item is clicked. It provides an OnHint event where the
Hint value can be supplied as an when required and an OnExecute and OnUpdate
where you can specify what happens when the control is clicked and what how
to alter the client controls' state when things like Enabled or Checked
change. A TAction can be attached to several controls so that there is a
single place where code for a single "Action" like Copy, Cut or Paste, etc
can react to a button, popup menu and main menu for the same action.

To set up a TAction, place a TActionList on a form and then use the
component editor to add TActions to it. Add code to the event handlers and
then hook up whatever controls need to call that Action by setting the
Action property of those controls to one of the Actions in the list.

Joanna
 
J

Joanna Carter [TeamB]

"Jon Skeet [C# MVP]" <[email protected]> a écrit dans le message de (e-mail address removed)...

| I don't believe there's anything similar in the .NET framework.
| However, you could create a class which aggregated a list of controls,
| and allowed you to manipulate them (enable/disable etc) as a group
| (just by looping through the list when you asked the instance to do
| something).

Oleg, take a look at creating a Component derivative that implements
IExtenderProvider. Make sure that it is capable of capturing the Click event
of Control. This will set you in a similar direction; I suggest that you
look at the source for TControl, TActionLink and TActionList to see how
Delphi works, and then simplify what you see.

Here's a starter :

[ProvideProperty("InvokeAction", typeof(Control))]
public class ActionProvider : Component, IExtenderProvider
{
private class Properties
{
private bool invokeAction = false;

public bool InvokeAction
{
get { return invokeAction; }
set { invokeAction = value; }
}
}

private Hashtable properties = new Hashtable();

private EventHandler execute;

public event EventHandler Execute
{
add { execute += value; }
remove { execute -= value; }
}

public ActionProvider(IContainer parent)
{
parent.Add(this);
}

bool IExtenderProvider.CanExtend(object obj)
{
return obj is Control;
}

private Properties EnsurePropertiesExists(object key)
{
Properties p = (Properties) properties[key];

if (p == null)
{
p = new Properties();

properties[key] = p;
}

return p;
}

[Category("Behaviour")]
[Description("Calls centralised event handler for Click event of linked
controls")]
[DefaultValue(false)]
public bool GetInvokeAction(Control c)
{
return EnsurePropertiesExists(c).InvokeAction;
}

public void SetInvokeAction(Control c, bool value)
{
EnsurePropertiesExists(c).InvokeAction = value;

if (value)
c.Click += HandleClick;
else
c.Click -= HandleClick;
}

private void HandleClick(object sender, EventArgs args)
{
if (!DesignMode)
{
Control control = (Control) sender;
if (control != null && execute != null)
{
execute(this, EventArgs.Empty);
}
}
}
}


Joanna
 
M

Marc Scheuner [MVP ADSI]

TAction is a non-visual component that can be added to a TActionList using a
component editor. Basically, controls like buttons and menu items have an
Action property and a TAction provides a RAD way of specifying what happens
when button or menu item is clicked.

Very convenient way of doing things, which is - unfortunately - sorely
lacking from the .NET framework. Makes you wonder why.....

There are a number of free and commercial "replacements" for this
annoyance.

There are a number of pretty straightforward "Delphi-to-C#"
translations up on CodeProject - see these for instance:

Simplifying GUI development with Actions
http://www.codeproject.com/csharp/actionlistprovider.asp

ActionLists for Windows.Forms
http://www.codeproject.com/cs/miscctrl/actionlist.asp

Or there's the commercial package by some former Delphites called
"CommandMaster" which can be found here:
http://www.componentscience.net/Products/CommandMaster/tabid/63/Default.aspx

No personal experience with any of those, though.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top