Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Globally modify button behavior 1

Status
Not open for further replies.

MasterRacker

Active member
Oct 13, 1999
3,343
US
Here's the scenario: Let's say I want my app to play a "click" noise every time a button is clicked. I'm thinking the proper way to do this would be to create a user control (call it "NoisyButton"). I'm thinking that a NoisyButton plays it's sound on a MouseDown event, which which would leave the Click event free for use everywhere in the app, that I put a NoisyButton.

My question is: what's the best way to retrofit this into an existing app? Is there a way to globally trap all click events, determine if it came from a button, play the sound and return where you came from? Or do I need to go through the app and replace all buttons with a NoisyButton? Other options?

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me
 
This is what I would do:

Create a class with a static method to play the sound. Say you call it PlaySound().

I would then add one method in each form to call on the form's loading to iterate through all your controls recursively. If the type is of button, then I would add an event handler for the MouseUp or Click event to a new method which simply calls the PlaySound() method.

So what you would need is: a class with a method to play the sound, a method in each form to iterate the controls, and a method for an event handler in each form to handle all of the button's clicks.
 
Why wouldn't you just create your NoisyButton and do a solution-wide replace. That is what an IDE is for.

As long as your noisy button extends a regular button - you're all set.

public class NoisyButton : System.Windows.Forms.Button
{
public NoisyButton()
{
}


protected override void OnClick(....)
{
//play my sound
base.OnClick(e);
}
}


Replace all instances of "System.Windows.Forms.Button" with "MySolution.NoisyButton"

 
MasterRacker,
I agree with JurkMonkey, it will take some effort may be equal to that of looping through ur controls at each form, but it'll make ur button events editable anytime just from ur NoisyButton class and it'll save time of extra loops running at each Form's load.
 
One of my questions was whether or not good old fashioned search&replace was the best way to go.

Didn't realize could extend the OnClick that way (should have figured it out though...) Nice clean solution.

Jeff
[purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day

"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top