Change picture box by clicking on a label

Asked

Viewed 53 times

-1

Good afternoon, could someone help me? I would like to click on a label my picturebox changed. I’m doing a quiz in windows form and type,picturebox function as the (wallpaper button) and the label (answer/button). So the Label contains the answer,:

  switch (pergunt) //variavel para guardar as perguntas
        {
            case 1:

                Pergunta.Text = "Quem desenvolveu o C#?";
                resp1.Text = "Microsoft";//certa
                resp2.Text = "AMD";
                resp3.Text = "Sunbed Microsystems";
                resp4.Text = "Intel";

                if (resposta == 1) //essa variavel resposta não funciona,queria armazenar a resposta dada pelo utilizador ao clicar em uma das labels nela.
                {
                    pictureBox1.Load(@"..\RespostaCerta.png");

                }
                else 
                {
                    pictureBox2.Load(@"..\RespostaErrada.png");                  
                    pictureBox3.Load(@"..\RespostaErrada.png");
                    pictureBox4.Load(@"..\RespostaErrada.png");
                }


                break;

And what I have in each of the buttons is:

   private void resp1_Click(object sender, EventArgs e)
    {
        Limpa(); //Esse metodo serve para limpar as labels para assim ser possivel realizar o sorteamento das perguntas
        SortearP();// Sortea as perguntas
        resp1_Click = Convert.ToString(resposta); //Aqui que está o problema,não consigo converter o que está dentro da Label para um valor ex 1,para que eu consiga lá no switch das perguntas fazer o if para mudar a picture box

    }

  private void resp2_Click(object sender, EventArgs e)
    {
        Limpa();
        SortearP();
        resp2_Click = Convert.ToString(resposta);
    }

    private void resp3_Click(object sender, EventArgs e)
    {
        Limpa();
        SortearP();
        resp3_Click = Convert.ToString(resposta);
    }
 private void resp4_Click(object sender, EventArgs e)
    {
        Limpa();
        SortearP();
        resp4_Click = Convert.ToString(resposta);
    }

And that’s it, thanks in advance ;)

2 answers

0

0


You can make a global response variable that the user selected and save in it the value of Label clicked. And whenever calling the Limpa(), the value of this variable is reset. After having the answer value, you can call the method to verify that this variable is correct.

After class declaration, declare the variable resposta, whereas -1 is unanswered.

protected int resposta = -1;

So whenever a Label is clicked, manipulate resposta for which value was selected:

private void resp2_Click(object sender, EventArgs e)
{
    Limpa();
    SortearP();
    resposta = 2; // botão 2 pressionado, resposta 2
}

Important note: return resposta for -1 in the method Limpa() not to cause an answer to be selected in the next question.

After that, call the method to check if the answer is correct. Add a check to check if a response has been set with if(resposta == -1) return;, so you will know that the user did not choose an answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.