Save the value of the Click event in a variable

0

I am programming for a practice a set of questions in windows forms, which I have a textbox with the question and 3 buttons with the options.

The question is how could you save the value of the click of each button on a variable (any) since the buttons would change value through the passing of the questions. so use it in an if to compare directly and show if it corresponds to the correct answer or not.

For example:

//suponiendo que la sentencia fuera corercta
(boton1 == clikeado){   
    MessageBox.Show("Correcto");
}
else
{
   //resto de la botones 
   MessageBox.Show("Incorrecto");
}

The truth is that I do not know how to embody it, thank you.

    
asked by Agus Veneziano 28.09.2018 в 00:07
source

2 answers

2

You can use the Tag property of the button to save any object. Let's say you have 3 buttons and only one has the correct answer.

button1.Tag = false;
button2.Tag = false;
button3.Tag = true;

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Tag = false;
            button2.Tag = false;
            button3.Tag = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                Button boton = (Button)sender; //sender es el objeto que envia el evento, es decir el butón. 

                bool correcto = Convert.ToBoolean(boton.Tag); //Haces un cast para sacar el valor del tag 

                if (correcto)
                {
                    MessageBox.Show("Correcto");
                }
                else
                {
                    MessageBox.Show("Incorrecto");

                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                Button boton = (Button)sender;

                bool correcto = Convert.ToBoolean(boton.Tag);

                if (correcto)
                {
                    MessageBox.Show("Correcto");
                }
                else
                {
                    MessageBox.Show("Incorrecto");

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                Button boton = (Button)sender;

                bool correcto = Convert.ToBoolean(boton.Tag);

                if (correcto)
                {
                    MessageBox.Show("Correcto");
                }
                else
                {
                    MessageBox.Show("Incorrecto");

                }
            }
        }


    }
    
answered by 28.09.2018 / 03:41
source
1

Assuming that the correct answer is in the text of your button, you only need to capture it with btn1.Text , with this small code you can assign or bring the value of the current text of your button.

In the case that you need to save it in a variable, it occurs to me that for easy manipulation you can save your value in a list. EJ:

List<string> respuestas = new List<string>();
respuestas.Add(btn1.Text);

I hope it serves you, anyway, add more information to your question if you want more concise answers

    
answered by 28.09.2018 в 03:08