Menus and Toolbars [C# newbie]

M

Mark F.

If an app has several toolbars and menus the event handler methods can
create a lot of code in the form's cs file. What is the best approach to
breaking it all up so that it is more organized?

Thanks,
Mark
 
D

DeveloperX

If an app has several toolbars and menus the event handler methods can
create a lot of code in the form's cs file. What is the best approach to
breaking it all up so that it is more organized?

Thanks,
Mark

That's quite a big subject.

You could investigate partial classes which allows you to spread one
class over several files (framework 2+) or Regions if you're framework
1.1.

If you're creating a control that can raise lots of events investigate
System.ComponentModel.EventHandlerList.

For a GUI based framework CAB (Composite UI Application Block) has
been coming up alot.
 
K

Kevin Spencer

Hi Mark,

First, make sure that your event handlers call methods that do the actual
work. This way, a menu item and a tool bar item can perform the same task
without duplicating the code.

With regards to organization, a couple of points. First, remember that once
the method stub has been generated, you may move it to any location in the
code you wish. So, you can group similar types of methods together. Then you
can use collapsible regions to further organize your code. Example:

#region Menu Event Handlers
//...
#endregion

#region Toolbar Event Handlers
//...
#endregion

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.
 
M

Mark F.

Kevin Spencer said:
Hi Mark,

First, make sure that your event handlers call methods that do the actual
work. This way, a menu item and a tool bar item can perform the same task
without duplicating the code.

With regards to organization, a couple of points. First, remember that
once the method stub has been generated, you may move it to any location
in the code you wish. So, you can group similar types of methods together.
Then you can use collapsible regions to further organize your code.
Example:

#region Menu Event Handlers
//...
#endregion

#region Toolbar Event Handlers
//...
#endregion

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

Thanks for the tips. I typically call a menu event method from a toolbar
button handler if the two serve the same function (e.g., open file, save,
etc.). My main form class still ends up with lots of code. I have figured
out how to do my file handling etc. in a separate class so I only have to
call a method from the menu or tool button event handler.
 
M

Mark F.

DeveloperX said:
That's quite a big subject.

You could investigate partial classes which allows you to spread one
class over several files (framework 2+) or Regions if you're framework
1.1.

If you're creating a control that can raise lots of events investigate
System.ComponentModel.EventHandlerList.

For a GUI based framework CAB (Composite UI Application Block) has
been coming up alot.

Thanks for the help. Not sure what "Composite UI Application Block" is so
I'll have to research it.

BTW: using VS 2005 and service packs.

Mark
 
S

Stefan Hoffmann

hi Kevin,

Kevin said:
First, make sure that your event handlers call methods that do the actual
work. This way, a menu item and a tool bar item can perform the same task
without duplicating the code.
In Borland Delphi there exists a component called ActionList, which is a
collection of actions, your methods.

These single actions can be bound to many controls. A menu item and
toolbar item sharing the same value in the property Action invoke the
same method. This is some kind of binding methods to controls.

Is there anything compareable in C# / .Net?


mfG
--> stefan <--
 
K

Kevin Spencer

As long as the Control Event Handlers have the same method signature, you
can attach the same Event Handler method to multiple Control events. I hope
you can forgive me if I don't know anything about an ActionList, but it
sounds similar. Delegates in the .Net platform are "Multi-cast delegates" -
meaning that they can point to any number of methods with the same method
signature.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.
 
T

Tom Spink

Mark said:
Thanks for the tips. I typically call a menu event method from a toolbar
button handler if the two serve the same function (e.g., open file, save,
etc.). My main form class still ends up with lots of code. I have figured
out how to do my file handling etc. in a separate class so I only have to
call a method from the menu or tool button event handler.

Hi,

That's a great start to reducing code redundancy, but if you're building a
big application, why not go all the way and implement the command pattern?

http://en.wikipedia.org/wiki/Command_pattern

If you Google for it, there are plenty of C# examples of implementations of
the command pattern, and variants thereof.
 

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