473,385 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Elegant way to reuse a method handler

Hi,

I am using a toolbar object that has a number of buttons that generally
speaking have a corresponding menu item. What I was hoping to be able
to do was to write an event handler for the menu item and assign it to
the click event of the toolbar button.

But I find that the toolbar buttons don't have a click event at all, I
have to write a handler in the buttonclick event of the toolbar itself
which in turn gets a ToolbarButtonClickEventArgs param passed that I
can use to determine which button was clicked. Which leads to code like
the following

if (e.Button == btnSomething)
{
mnuSomthing_Click(this,EventArgs.Empty);
}
else
{
if (e.button == btnSomethingElse)
{
mnuSomthingElse_Click(this,EventArgs.Empty);
}
}

Surely there is a better way than this...surely.

Cheers Tim.
Nov 15 '05 #1
3 1624
Tim,

There are number of ways that can be termed as "better". Some may be an
overkill given the situation at hand.

Lets say you had a class that takes a certain delegate type as a parameter
in their constructor. This delegate instance is the actual method you'd like
each instance to "call".

You stuff instances of these classes into a hash table using the name of the
button as the key (identifier).

So the code in your event would look like this

ht.Add(xxxButton.Name, new MyObject(new
SomeDelegate(TheActualMethodForThisButton)));
and so on for each button you have.

Then in the buttonclick event

((MyObject)ht[e.Button.Name]).Execute(this, EventArgs.Empty);

Where MyObject is the class type that has an Execute method whose
implementation in turn calls the delegate passed to it in the constructor.
I hope you get the picture. Also, I don't know if "Button" has a Name
property, but any other "identifier" will do.
--
Shiv R. Kumar
http://www.matlus.com
Nov 15 '05 #2
Shiv Kumar wrote:


I hope you get the picture. Also, I don't know if "Button" has a Name
property, but any other "identifier" will do.


mmm, interesting approach. I'll look at this in more detail.

Cheers Tim.
Nov 15 '05 #3
Probably a simpler solution (one that is more fitting for this job) is to
simply stuff the delegate into the hashtable, rather than have an object
that has a method that calls a delegate etc.

--
Shiv R. Kumar
http://www.matlus.com
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kamilche | last post by:
Is there a more elegant way to change the working directory of Python to the directory of the currently executing script, and add a folder called 'Shared' to the Python search path? This is what...
9
by: Charles Prince | last post by:
How do I reuse a ostrstream? So far I have replaced all code that does this "delete <ostrstream>.str()" with "<ostrstream>.freeze(0)"
3
by: danra | last post by:
Hi, I have a question which seems to me pretty basic, unfortunately I can't seem to figure it out. Let's say I have an abstract base class Vehicle and classes Car and Truck which derive from...
3
by: Giovanni Bajo | last post by:
Hello, what's the magic needed to reuse the base-class implementation of a classmethod? class A(object): @classmethod def foo(cls, a,b): # do something pass
0
by: integragreg | last post by:
I apologize in advance if I am posting to the wrong group, but at least one of my questions is related to Platform Invoke in C#. I am using .NET Framework 1.1, and for improved performance, I...
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
8
by: Braky Wacky | last post by:
Hello, I have an ASP.NET webpage that uses an instance of System.Web.UI.HtmlControls.HtmlInputFile for uploading files to our server. I came across the documentation at MSDN for upping the...
5
by: apandapion | last post by:
I'm working with csharp and .net for the first time, and I've had a fair amount of luck. I started with the MSDN "Walkthrough : Creating a Distributed Application" tutorial and expanded from...
7
by: RichB | last post by:
I am just trying to get to grips with C# and OOP, and one of the benefits would seem to be code reuse. However I am not sure where to draw the line. I have the following section of code: if...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.