Actions i WinForms (C#) ?

E

eramfelt

I have a question on what the "proper" way is to link Controls and
logic? At my current project, which is a C# .NET 2.0 GUI application,
they use events to listen on buttons/lists/menu items. Ie. the code for
linking an action/logic to a button it looks like btn_Clicked(), and so
on (list_DblClicked(), list_KeyUp(), etc.) Is this the preferred way to
link control and logic?

Im having a little issue when it comes to enabling/disabling controls.
Look at this example, and please explain on how you would solve the
problem. I have an application where I want a certain functionality
that is:
* Accessible through a button in the tool bar
* Accessible through a menu item
* Accessible through a short cut
* Accessible through a button in a modeless Form
* Is not always available (Enable = false)
* Logic for the functionality must be in a separate class

So how would you solve this problem?
If I want the functionality to be disabled, do I need to go through all
controls and set Enable = false on them?
If I (later) want the functionality to be called from a ListView, how
can I access it from a list?


If I wanted to do something similar in Java, I would extend the
AbstractAction class and implement my functionality/logic in the
actionPerformed() method. Then I would create one instance of the class
and add it to the button/menu item/tool bar button. All the controls
would use the same Action object. The Action object has a
setEnable(bool) method that disables the action, which means the button
and the menu item will be disabled (grayed out), the short cut key no
longer works, etc. The beauty of this is that the menu/button text
comes from the Action, short cut is defined by Action, everything
(almost) is in one place/class. For more info:
http://shorl.com/gygrobonivuko and http://shorl.com/difagrysajasa

Is there anything similar to the Action (Swing) framework in .NET?
Or do I have to do it manually for every other Button/Menu item?
 
M

mabster

I have a question on what the "proper" way is to link Controls and
logic? At my current project, which is a C# .NET 2.0 GUI application,
they use events to listen on buttons/lists/menu items. Ie. the code for
linking an action/logic to a button it looks like btn_Clicked(), and so
on (list_DblClicked(), list_KeyUp(), etc.) Is this the preferred way to
link control and logic?

I've written a little extender/provider component to add an "Action"
property to buttons, menu items etc. Each "action" has an "Execute"
event and all the properties you'd expect - Text, Enabled, ToolTipText
etc - which flow through to the controls the action is bound to.

I haven't made it publicly available, but if you email me I'd be happy
to send you the DLL.

Matt
 
R

redsolo

First, sorry for the double posting. I screwed up.. (and thanks for not
flaming me)
I've written a little extender/provider component to add an "Action"
property to buttons, menu items etc. Each "action" has an "Execute"
event and all the properties you'd expect - Text, Enabled, ToolTipText
etc - which flow through to the controls the action is bound to.

But as I understand there is no support for this more MVC compliant
approach in the .NET framework? I still need to go through all the
buttons/menu items/etc and disable them when the functionality should
be not available.

Of course it is a little more hard work to do it in Java, but the
benefit is that you wont have a problem adding buttons/controls that do
the same thing as you dont have to re-implement anything.


//Erik
http://redsolo.blogspot.com
 
M

mabster

redsolo said:
First, sorry for the double posting. I screwed up.. (and thanks for not
flaming me)


But as I understand there is no support for this more MVC compliant
approach in the .NET framework? I still need to go through all the
buttons/menu items/etc and disable them when the functionality should
be not available.

Of course it is a little more hard work to do it in Java, but the
benefit is that you wont have a problem adding buttons/controls that do
the same thing as you dont have to re-implement anything.

No built-in support, no. With my ActionList component I just catch the
Application.Idle event and do what I have to do, eg:

ViewToolbar.Checked = toolStrip1.Visible;
ViewStatusBar.Checked = statusStrip1.Visible;
fileSave.Enabled = myFile.Modified;

etc etc.

It then updates all the necessary controls' enabled, visible, checked
etc properties.
 

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