Click here to Skip to main content
15,900,467 members
Home / Discussions / C#
   

C#

 
GeneralRe: CultureInfo and DateTime.parse Pin
OriginalGriff14-Feb-11 9:04
mveOriginalGriff14-Feb-11 9:04 
GeneralRe: CultureInfo and DateTime.parse Pin
Paul E Davies14-Feb-11 21:51
Paul E Davies14-Feb-11 21:51 
Questionsound on btn clik Pin
aeman11-Feb-11 2:28
aeman11-Feb-11 2:28 
AnswerRe: sound on btn clik Pin
OriginalGriff11-Feb-11 2:41
mveOriginalGriff11-Feb-11 2:41 
GeneralRe: sound on btn clik Pin
aeman11-Feb-11 4:06
aeman11-Feb-11 4:06 
AnswerRe: sound on btn clik Pin
OriginalGriff11-Feb-11 4:41
mveOriginalGriff11-Feb-11 4:41 
GeneralRe: sound on btn clik Pin
aeman11-Feb-11 5:53
aeman11-Feb-11 5:53 
AnswerRe: sound on btn clik Pin
OriginalGriff11-Feb-11 8:50
mveOriginalGriff11-Feb-11 8:50 
OK: to solve your problem, replace "openFileDialog1.FileName" with "play":
         if (play != null)
             {
             // ...play your sund.
System.Media.SoundPlayer simplesound = new System.Media.SoundPlayer(play);
             simplesound.Play();
             }


And you will hear your noise.

However, I lied to you, a little bit, earlier.
This is not the way I would do it. The way I would do it is cleaner, and a lot nicer, but it is quite a bit more advanced than I think you are, and needs more advanced C# - which is why I didn't recommend it.

On reflection, I think that may have been a mistake. So, here is the way I would actually have done it: it uses one of the so-called "Pillars of Object Oriented Programming" - inheritance.

What you want to do is make a button "click", or play a sound when it is pressed. That is, to add functionality to a button. The proper way to do that is to produce a new Button, which has all the features of the old, but with the addition of playing a sound. We inherit the button into a new class, and add our functions to it. It isn't difficult: Create a new class (like I told you earlier) only this time, call it "ClickButton". The whole class file consists of:

public class ClickButton : Button
    {
    private static System.Media.SoundPlayer click = null;
    private static string clickPath = null;
    public static string ClickSound
        {
        get { return clickPath; }
        set
            {
            clickPath = value;
            if (clickPath == null)
                {
                click = null;
                }
            else
                {
                click = new System.Media.SoundPlayer(clickPath);
                }
            }
        }
    public ClickButton()
        {
        base.Click += new EventHandler(MyClick);
        }
    private void MyClick(object sender, EventArgs e)
        {
        if (click != null)
            {
            click.Play();
            }
        }
    }
All this does is declare a new class, inherited from Button. It adds two private variables: one a string to hold the path to the sound file, and the other a SoundPlayer we can load with the sound to save time when we play it. The only reason we store the string path, is so we can return it in the ClickSound property getter. When you set the property, we load the file and save both the path and the sound in a playable state.

The constructor adds our click handler to the default Button handlers, so whenever our button is clicked, we get told.

Our click handler checks if there is a sound, and if so, plays it.

Build that, then add a ClickButton to your form(s) - it should appear at the top of your Toolbox in design mode.

All you have to do then is set the ClickSound to the appropriate file when the user selects it:
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
    {
    ClickButton.ClickSound = ofd.FileName;
    }
Every time you want a button that makes a noise, use a ClickButton instead of a Button and it will work (after you have set the sound file).

Did any of that make sense? Laugh | :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."

GeneralRe: sound on btn clik Pin
aeman11-Feb-11 21:26
aeman11-Feb-11 21:26 
GeneralRe: sound on btn clik Pin
OriginalGriff11-Feb-11 21:31
mveOriginalGriff11-Feb-11 21:31 
GeneralRe: sound on btn clik Pin
aeman11-Feb-11 22:15
aeman11-Feb-11 22:15 
GeneralRe: sound on btn clik Pin
OriginalGriff11-Feb-11 22:21
mveOriginalGriff11-Feb-11 22:21 
GeneralRe: sound on btn clik Pin
aeman12-Feb-11 5:25
aeman12-Feb-11 5:25 
AnswerRe: sound on btn clik Pin
musefan11-Feb-11 2:47
musefan11-Feb-11 2:47 
AnswerRe: sound on btn clik Pin
DaveyM6911-Feb-11 3:17
professionalDaveyM6911-Feb-11 3:17 
QuestionRTP to streaming webcam P2P or to a webpage [modified] Pin
Honik8911-Feb-11 2:16
Honik8911-Feb-11 2:16 
AnswerRe: RTP to streaming webcam P2P or to a webpage Pin
Ali Al Omairi(Abu AlHassan)11-Feb-11 15:16
professionalAli Al Omairi(Abu AlHassan)11-Feb-11 15:16 
GeneralRe: RTP to streaming webcam P2P or to a webpage Pin
Honik8911-Feb-11 22:13
Honik8911-Feb-11 22:13 
AnswerRe: RTP to streaming webcam P2P or to a webpage Pin
Henry Minute12-Feb-11 3:18
Henry Minute12-Feb-11 3:18 
QuestionIntearact with DB ? Pin
Hum Dum10-Feb-11 22:21
Hum Dum10-Feb-11 22:21 
AnswerRe: Intearact with DB ? Pin
Pete O'Hanlon10-Feb-11 23:27
mvePete O'Hanlon10-Feb-11 23:27 
GeneralRe: Intearact with DB ? Pin
Hum Dum10-Feb-11 23:52
Hum Dum10-Feb-11 23:52 
GeneralRe: Intearact with DB ? Pin
PIEBALDconsult11-Feb-11 0:15
mvePIEBALDconsult11-Feb-11 0:15 
AnswerRe: Intearact with DB ? Pin
PIEBALDconsult10-Feb-11 23:28
mvePIEBALDconsult10-Feb-11 23:28 
GeneralRe: Intearact with DB ? Pin
Hum Dum10-Feb-11 23:57
Hum Dum10-Feb-11 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.