tags:

views:

86

answers:

4

I need to implement Pause and Resume events of a MP3 player inside the same button click event. following is the code i have tried and its not working,Can any one give me the solution

private void button3_Click(object sender, EventArgs e)
{
    if (button3.Text == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";           
    }
    if (button3.Text == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
    }
}
+5  A: 

You are changing the button3.Text property within the first if statement. When the second if statement is tested it is true (both if statements are running with each button click when the Text property is "Pause")

Use if, else to run one code block or another.

Use if, else if statements if you want a test to be run on the second code block also.

You should also take account of the possibility that neither of these cases is true.

if (button3.Text == "Pause")
{
    CommandString = "pause mp3file";
    mciSendString(CommandString, null, 0, 0);
    Status = true;
    button3.Text = "Resume";
}
else if(button3.Text == "Resume")
{   
    CommandString = "resume mp3file";
    mciSendString(CommandString, null, 0, 0);
    button3.Text = "Pause";
}
Lewray
+1 for good advice (but missed the change back to "Pause" though)
Abel
edited to include answer from @Arseny
Lewray
+4  A: 

at first glance it won't work properly 'cause in 2 case

if (button3.Text == "Resume")
{   
    CommandString = "resume mp3file";
    mciSendString(CommandString, null, 0, 0);
}

you missed the line:

button3.Text = "Pause";

Actually it's not a good idea to check the button state by its text property. As a simple solution you need to have a boolean flag to check against it.

Arseny
+1 well spotted
Abel
Ah... I missed that, but noticed the two if statements
Lewray
@Lewray it's not you but the author :)
Arseny
+1  A: 

you have two if consecutive if statements. You need just one if/else statement.

change your code to:

    if (button3.Text == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";           
    }
    else if (button3.Text == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
    }
Matt Jacobsen
A: 

The problem is:

By the time you get to the second if statement, you have changed the Text of the button, hence both statements are being run...

Here is a quick test:

        if (button1.Text == "Pause")
        {
            label1.Text = label1.Text + " saw pause ";
            button1.Text = "Resume";


        }
        if (button1.Text == "Resume")
        {
            label1.Text = label1.Text + " saw resume ";
            button1.Text = "Pause";
        }

returns: label1 saw pause saw resume.

There are two ways to fix this:

You could insert a 'return;' statement within each if statement:

private void button3_Click(object sender, EventArgs e)
{

    if (button3.Text == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";
        return;
    }
    if (button3.Text == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
        button3.Text = "Pause";
        return;
    }
}

Or secondly, you could capture the value of the button text once:

private void button3_Click(object sender, EventArgs e)
{
    String value = button3.Text;
    if (value == "Pause")
    {
        CommandString = "pause mp3file";
        mciSendString(CommandString, null, 0, 0);
        Status = true;
        button3.Text = "Resume";

    }
    if (value == "Resume")
    {   
        CommandString = "resume mp3file";
        mciSendString(CommandString, null, 0, 0);
        buton3.Text = "Pause"; // As mentioned before, this is required too.
    }
}

Hope that helps.

Steve