One button code to undo an action in C#

Asked

Viewed 1,370 times

5

Hello, I am developing a Bingo software for the Church that I attend. Tela Principal

When you press a button of any number, it will appear in the Picture Box. As shown in the following image.

inserir a descrição da imagem aqui

Now, realize that there is the "<< Back" button, it is in it that I need help. I need a code to program it to undo an earlier action. For example: I pressed the button 42 on the screen, but it wasn’t supposed to be it, it was supposed to be the 43, as I said: once pressed it is useless. The user would press the "<< Back" button to undo his action - which was to enable the "42" button again. How do I do that? From now on, thank you.

@Edit On request, my code is like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

    private void button1_Click(object sender, EventArgs e)
    {
        Image btn1 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\1.jpg");
        img_box.Image = btn1;
        button1.Enabled = false;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Image btn2 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\2.jpg");
        img_box.Image = btn2;
        button2.Enabled = false;
    }
    private void button3_Click_1(object sender, EventArgs e)
    {
        Image btn3 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\3.jpg");
        img_box.Image = btn3;
        button3.Enabled = false;
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Image btn4 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\4.jpg");
        img_box.Image = btn4;
        button4.Enabled = false;
    }

    private void button5_Click(object sender, EventArgs e)
    {
        Image btn5 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\5.jpg");
        img_box.Image = btn5;
        button5.Enabled = false;
    }

    private void button6_Click(object sender, EventArgs e)
    {
        Image btn6 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\6.jpg");
        img_box.Image = btn6;
        button6.Enabled = false;
    }

    private void button7_Click(object sender, EventArgs e)
    {
        Image btn7 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\7.jpg");
        img_box.Image = btn7;
        button7.Enabled = false;
    }

    private void button8_Click(object sender, EventArgs e)
    {
        Image btn8 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\8.jpg");
        img_box.Image = btn8;
        button8.Enabled = false;
    }

    private void button9_Click(object sender, EventArgs e)
    {
        Image btn9 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\9.jpg");
        img_box.Image = btn9;
        button9.Enabled = false;
    }

    private void button10_Click(object sender, EventArgs e)
    {
        Image btn10 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\10.jpg");
        img_box.Image = btn10;
        button10.Enabled = false;
    }

    private void button11_Click(object sender, EventArgs e)
    {
        Image btn11 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\11.jpg");
        img_box.Image = btn11;
        button11.Enabled = false;
    }

    private void button12_Click(object sender, EventArgs e)
    {
        Image btn12 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\12.jpg");
        img_box.Image = btn12;
        button12.Enabled = false;
    }

    private void button13_Click(object sender, EventArgs e)
    {
        Image btn13 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\13.jpg");
        img_box.Image = btn13;
        button13.Enabled = false;
    }

    private void button14_Click(object sender, EventArgs e)
    {
        Image btn14 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\14.jpg");
        img_box.Image = btn14;
        button14.Enabled = false;
    }

    private void button15_Click(object sender, EventArgs e)
    {
        Image btn15 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\15.jpg");
        img_box.Image = btn15;
        button15.Enabled = false;
    }

    private void button16_Click(object sender, EventArgs e)
    {
        Image btn16 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\16.jpg");
        img_box.Image = btn16;
        button16.Enabled = false;
    }

    private void button17_Click(object sender, EventArgs e)
    {
        Image btn17 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\17.jpg");
        img_box.Image = btn17;
        button17.Enabled = false;
    }

    private void button18_Click(object sender, EventArgs e)
    {
        Image btn18 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\18.jpg");
        img_box.Image = btn18;
        button18.Enabled = false;
    }

    private void button19_Click(object sender, EventArgs e)
    {
        Image btn19 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\19.jpg");
        img_box.Image = btn19;
        button19.Enabled = false;
    }

    private void button20_Click(object sender, EventArgs e)
    {
        Image btn20 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\20.jpg");
        img_box.Image = btn20;
        button20.Enabled = false;
    }

    private void button21_Click(object sender, EventArgs e)
    {
        Image btn21 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\21.jpg");
        img_box.Image = btn21;
        button21.Enabled = false;
    }

    private void button22_Click(object sender, EventArgs e)
    {
        Image btn22 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\22.jpg");
        img_box.Image = btn22;
        button22.Enabled = false;
    }

    private void button23_Click(object sender, EventArgs e)
    {
        Image btn23 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\23.jpg");
        img_box.Image = btn23;
        button23.Enabled = false;
    }

    private void button24_Click(object sender, EventArgs e)
    {
        Image btn24 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\24.jpg");
        img_box.Image = btn24;
        button24.Enabled = false;
    }

    private void button25_Click(object sender, EventArgs e)
    {
        Image btn25 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\25.jpg");
        img_box.Image = btn25;
        button25.Enabled = false;
    }

    private void button26_Click(object sender, EventArgs e)
    {
        Image btn26 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\26.jpg");
        img_box.Image = btn26;
        button26.Enabled = false;
    }

    private void button27_Click(object sender, EventArgs e)
    {
        Image btn27 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\27.jpg");
        img_box.Image = btn27;
        button27.Enabled = false;
    }

    private void button28_Click(object sender, EventArgs e)
    {
        Image btn28 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\28.jpg");
        img_box.Image = btn28;
        button28.Enabled = false;
    }

    private void button29_Click(object sender, EventArgs e)
    {
        Image btn29 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\29.jpg");
        img_box.Image = btn29;
        button29.Enabled = false;
    }

    private void button30_Click(object sender, EventArgs e)
    {
        Image btn30 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\30.jpg");
        img_box.Image = btn30;
        button30.Enabled = false;
    }

    private void button31_Click(object sender, EventArgs e)
    {
        Image btn31 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\31.jpg");
        img_box.Image = btn31;
        button31.Enabled = false;
    }

    private void button32_Click(object sender, EventArgs e)
    {
        Image btn32 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\32.jpg");
        img_box.Image = btn32;
        button32.Enabled = false;
    }

    private void button33_Click(object sender, EventArgs e)
    {
        Image btn33 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\33.jpg");
        img_box.Image = btn33;
        button33.Enabled = false;
    }

    private void button34_Click(object sender, EventArgs e)
    {
        Image btn34 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\34.jpg");
        img_box.Image = btn34;
        button34.Enabled = false;
    }

    private void button35_Click(object sender, EventArgs e)
    {
        Image btn35 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\35.jpg");
        img_box.Image = btn35;
        button35.Enabled = false;
    }

    private void button36_Click(object sender, EventArgs e)
    {
        Image btn36 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\36.jpg");
        img_box.Image = btn36;
        button36.Enabled = false;
    }

    private void button37_Click(object sender, EventArgs e)
    {
        Image btn37 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\37.jpg");
        img_box.Image = btn37;
        button37.Enabled = false;
    }

    private void button38_Click(object sender, EventArgs e)
    {
        Image btn38 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\38.jpg");
        img_box.Image = btn38;
        button38.Enabled = false;
    }

    private void button39_Click(object sender, EventArgs e)
    {
        Image btn39 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\39.jpg");
        img_box.Image = btn39;
        button39.Enabled = false;
    }

    private void button40_Click(object sender, EventArgs e)
    {
        Image btn40 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\40.jpg");
        img_box.Image = btn40;
        button40.Enabled = false;
    }

    private void button41_Click(object sender, EventArgs e)
    {
        Image btn41 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\41.jpg");
        img_box.Image = btn41;
        button41.Enabled = false;
    }

    private void button42_Click(object sender, EventArgs e)
    {
        Image btn42 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\42.jpg");
        img_box.Image = btn42;
        button42.Enabled = false;
    }

    private void button43_Click(object sender, EventArgs e)
    {
        Image btn43 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\43.jpg");
        img_box.Image = btn43;
        button43.Enabled = false;
    }

    private void button44_Click(object sender, EventArgs e)
    {
        Image btn44 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\44.jpg");
        img_box.Image = btn44;
        button44.Enabled = false;
    }

    private void button45_Click(object sender, EventArgs e)
    {
        Image btn45 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\45.jpg");
        img_box.Image = btn45;
        button45.Enabled = false;
    }

    private void button46_Click(object sender, EventArgs e)
    {
        Image btn46 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\46.jpg");
        img_box.Image = btn46;
        button46.Enabled = false;
    }

    private void button47_Click(object sender, EventArgs e)
    {
        Image btn47 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\47.jpg");
        img_box.Image = btn47;
        button47.Enabled = false;
    }

    private void button48_Click(object sender, EventArgs e)
    {
        Image btn48 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\48.jpg");
        img_box.Image = btn48;
        button48.Enabled = false;
    }

    private void button49_Click(object sender, EventArgs e)
    {
        Image btn49 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\49.jpg");
        img_box.Image = btn49;
        button49.Enabled = false;
    }

    private void button50_Click(object sender, EventArgs e)
    {
        Image btn50 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\50.jpg");
        img_box.Image = btn50;
        button50.Enabled = false;
    }

    private void button51_Click(object sender, EventArgs e)
    {
        Image btn51 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\51.jpg");
        img_box.Image = btn51;
        button51.Enabled = false;
    }

    private void button52_Click(object sender, EventArgs e)
    {
        Image btn52 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\52.jpg");
        img_box.Image = btn52;
        button52.Enabled = false;
    }

    private void button53_Click(object sender, EventArgs e)
    {
        Image btn53 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\53.jpg");
        img_box.Image = btn53;
        button53.Enabled = false;
    }

    private void button54_Click(object sender, EventArgs e)
    {
        Image btn54 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\54.jpg");
        img_box.Image = btn54;
        button54.Enabled = false;
    }

    private void button55_Click(object sender, EventArgs e)
    {
        Image btn55 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\55.jpg");
        img_box.Image = btn55;
        button55.Enabled = false;
    }

    private void button56_Click(object sender, EventArgs e)
    {
        Image btn56 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\56.jpg");
        img_box.Image = btn56;
        button56.Enabled = false;
    }

    private void button57_Click(object sender, EventArgs e)
    {
        Image btn57 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\57.jpg");
        img_box.Image = btn57;
        button57.Enabled = false;
    }

    private void button58_Click(object sender, EventArgs e)
    {
        Image btn58 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\58.jpg");
        img_box.Image = btn58;
        button58.Enabled = false;
    }

    private void button59_Click(object sender, EventArgs e)
    {
        Image btn59 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\59.jpg");
        img_box.Image = btn59;
        button59.Enabled = false;
    }

    private void button60_Click(object sender, EventArgs e)
    {
        Image btn60 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\60.jpg");
        img_box.Image = btn60;
        button60.Enabled = false;
    }

    private void button61_Click(object sender, EventArgs e)
    {
        Image btn61 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\61.jpg");
        img_box.Image = btn61;
        button61.Enabled = false;
    }

    private void button62_Click(object sender, EventArgs e)
    {
        Image btn62 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\62.jpg");
        img_box.Image = btn62;
        button62.Enabled = false;
    }

    private void button63_Click(object sender, EventArgs e)
    {
        Image btn63 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\63.jpg");
        img_box.Image = btn63;
        button63.Enabled = false;
    }

    private void button64_Click(object sender, EventArgs e)
    {
        Image btn64 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\64.jpg");
        img_box.Image = btn64;
        button64.Enabled = false;
    }

    private void button65_Click(object sender, EventArgs e)
    {
        Image btn65 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\65.jpg");
        img_box.Image = btn65;
        button65.Enabled = false;
    }

    private void button66_Click(object sender, EventArgs e)
    {
        Image btn66 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\66.jpg");
        img_box.Image = btn66;
        button66.Enabled = false;
    }

    private void button67_Click(object sender, EventArgs e)
    {
        Image btn67 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\67.jpg");
        img_box.Image = btn67;
        button67.Enabled = false;
    }

    private void button68_Click(object sender, EventArgs e)
    {
        Image btn68 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\68.jpg");
        img_box.Image = btn68;
        button68.Enabled = false;
    }

    private void button69_Click(object sender, EventArgs e)
    {
        Image btn69 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\69.jpg");
        img_box.Image = btn69;
        button69.Enabled = false;
    }

    private void button70_Click(object sender, EventArgs e)
    {
        Image btn70 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\70.jpg");
        img_box.Image = btn70;
        button70.Enabled = false;
    }

    private void button71_Click(object sender, EventArgs e)
    {
        Image btn71 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\71.jpg");
        img_box.Image = btn71;
        button71.Enabled = false;
    }

    private void button72_Click(object sender, EventArgs e)
    {
        Image btn72 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\72.jpg");
        img_box.Image = btn72;
        button72.Enabled = false;
    }

    private void button73_Click(object sender, EventArgs e)
    {
        Image btn73 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\73.jpg");
        img_box.Image = btn73;
        button73.Enabled = false;
    }

    private void button74_Click(object sender, EventArgs e)
    {
        Image btn74 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\74.jpg");
        img_box.Image = btn74;
        button74.Enabled = false;
    }

    private void button75_Click(object sender, EventArgs e)
    {
        Image btn75 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\75.jpg");
        img_box.Image = btn75;
        button75.Enabled = false;
    }


}

}

  • Put your code, what you’ve done, have tried. Give someone something to guide you according to what you’re doing.

3 answers

3

Really the best way to do what you want is by using a stack. If you don’t know what a stack of data is, I suggest you start by researching what a "stack" is. Follow a video link with a simple explanation Data structure Stack in C#.

Below is the code of a small class to manipulate stack access and change property Enabled of the buttons and soon after this the form code that demonstrates how the class is used without having to write code in the event click of each of the buttons.

Any questions just ask.

Class Pilhabotoes.

using System.Collections.Generic;
using System.Windows.Forms;

namespace Bingo2._1
{
    public class PilhaBotoes
    {
        private Stack<Button> pilhaBotoes = new Stack<Button>();
        public string Adicionar(Button btn)
        {
            btn.Enabled = false;
            pilhaBotoes.Push(btn);

            return btn.Text;
        }
        public string Remover()
        {
            if (pilhaBotoes.Count == 0)
                return "";

            Button btn = pilhaBotoes.Pop();
            btn.Enabled = true;

            if (pilhaBotoes.Count == 0)
                return "";
            else
                return pilhaBotoes.Peek().Text;
        }
    }
}

Form1

using System;
using System.Windows.Forms;
namespace Bingo2._1
{
    public partial class Form1 : Form
    {
        PilhaBotoes pilha;

        public Form1()
        {
            InitializeComponent();

            pilha = new PilhaBotoes();

            bt01.Click += buttonClick;
            bt02.Click += buttonClick;
            bt03.Click += buttonClick;
            bt04.Click += buttonClick;
            bt05.Click += buttonClick;
            bt06.Click += buttonClick;
            bt07.Click += buttonClick;
            bt08.Click += buttonClick;
            bt09.Click += buttonClick;
            bt10.Click += buttonClick;
            bt11.Click += buttonClick;
            bt12.Click += buttonClick;
            bt13.Click += buttonClick;
            bt14.Click += buttonClick;
            bt15.Click += buttonClick;
            bt16.Click += buttonClick;
            bt17.Click += buttonClick;
            bt18.Click += buttonClick;
            bt19.Click += buttonClick;
            bt20.Click += buttonClick;
        }

        private void buttonClick(object sender, EventArgs e)
        {
            lb_numero.Text = pilha.Adicionar((sender as Button));
        }

        private void bnt_desfazer_ultima_alteracao_Click(object sender, EventArgs e)
        {
            lb_numero.Text = pilha.Remover();
        }
    }
}
  • I’m sorry, but wouldn’t your answer be a duplicate of mine that I answered first? http://answall.com/a/76673/298. When you agree that the solution to the problem is a given answer, then it is correct that you support the answer already given by clicking the up arrow.

  • 4

    Both use stack but not equal, your answer uses a stack of int and a function to get the number when a button is clicked, I used a stack of buttons while unsealing I have the button I want to work, so much so that my event has only one line. Another point is the function to load an image file, which is much faster to pass the property Text from the button to the label. Not to mention the file risk does not exist. Negative votes should be given to wrong answers please read "When should I vote against?" (http://answall.com/help/privileges/vote-down)

2

I suggest you make two changes to your code.

  1. The first change is to create a method to be called in the event click the numeric buttons, which will greatly remove the duplicity of your code. In addition, the form will also assign dynamically the same event click to all numeric buttons, instead of you declaring them one by one manually.
  2. The second change is to add a stack to your form to save a history of all numbers selected by the user in the order in which they were selected. In C#, the stack is a class object System.Collections.Generic.Stack< T >, in which T in that case may be a int, because the stack will be storing numbers (the numbers selected by the user).

Well, let’s get to the code. First, declare a Stack object that will represent the history of selected numbers in your form as follows (remember to import the namespace System.Collections.Generic with a directive using at the top of the form source code file):

private Stack<int> _historicoDeNumerosSelecionados = new Stack<int>();

The method to be called in the event click each button must be the following:

private void ProcesseEscolhaDoUsuario(int numeroSelecionado)
{
    img_box.Image = ObtenhaImagemDoBotao(numeroSelecionado);

    // Encontra o botão clicado.
    var botaoClicado = ObtenhaBotaoNumerico(numeroSelecionado);
    botaoClicado.Enabled = false;

    // Adiciona o número selecionado no histórico.
    _historicoDeNumerosSelecionados.Push(numeroSelecionado);
}

public Button ObtenhaBotaoNumerico(int numeroDoBotao)
{
    return this.Controls.Find("button" + numeroDoBotao) as Button;
}

public Image ObtenhaImagemDoBotao(int numeroSelecionado)
{
    return Image.FromFile(string.Concat(@"C:\Users\Dudu\Desktop\Softwares\Bingo 2.0\Img\", numeroSelecionado, ".jpg"));
}

In the form builder you should put the code below, which dynamically assign the Click event to the numeric buttons:

public Form1()
{
    InitializeComponent();

    AtribuaEventosClickDosBotoesNumericos();
}

public void AtribuaEventosClickDosBotoesNumericos()
{
    // Itera os 75 botões numéricos.
    for (int i = 1; i <= 75; i++)
    {
        var botaoNumerico = ObtenhaBotaoNumerico(i);

        // Atribui o evento click para cada um dos botões numéricos.
        botaoNumerico.Click += BotaoNumerico_Click;
    }
}

// Evento a ser executado quando o usuário clicar em qualquer dos botões numéricos.
private void BotaoNumerico_Click(object sender, EventArgs e)
{
    // Obtém o número do botão clicado. Ex: button1 => numero = 1.
    string numeroDoBotao = ((Button)sender).Name.Replace("button", "");

    // Passa o número do botão para o método que irá efetuar o processamento 
    // do número selecionado. Por exemplo, no caso do button2, passa o
    // número 2, e no caso do button3 passa o número 3, etc.
    ProcesseEscolhaDoUsuario(int.Parse(numeroDoBotao));
}

Given that the event BotaoNumerico_Click will be assigned to all numeric buttons, you can now remove from your code all event statements from the buttons, provided button1_Click until button75_Click. With this refactoring we removed approximately 450 lines of duplicate code.

Now on the Event Click Back Button you do the reverse process based on the last number that was added on the stack:

public void BotaoVoltar_Click(object sender, EventArgs e)
{
    // Obtém e remove do histórico o último número que foi selecionado.
    int ultimoNumeroSelecionado = _historicoDeNumerosSelecionados.Pop();

    // A imagem deverá agora mostrar o penúltimo número selecionado, por
    // isso precisamos "espiar" a pilha para verificar qual é agora o último
    // elemento (o qual anteriormente era o penúltimo).
    var penultimoNumeroSelecionado = _historicoDeNumerosSelecionados.Peek()
    img_box.Image = ObtenhaImagemDoBotao(penultimoNumeroSelecionado );

    // Encontra o último botão clicado.
    var ultimobotaoClicado = ObtenhaBotaoNumerico(ultimoNumeroSelecionado);

    // Habilita novamente o último botão clicado.
    ultimobotaoClicado.Enabled = true;
}

1

Hello, seeing your code I’ll give you a hint, it would not be better if you use in place of the image a label and set the values of the numbers in it. Ex: at the click of the button 1 you use

btn1 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo2.0\Img\1.jpg"); img_box.Image = btn1; It would no longer be viable for you to use it like this:

 label1.Text = "1";

Why so you will spend less time and your code will get cleaner and will give you less trouble because instead of putting in each event click on a code button of this

    btn1 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo2.0\Img\1.jpg");
    img_box.Image = btn1;

 btn2 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo2.0\Img\2.jpg");
    img_box.Image = btn2;

Put this:

label1.text="1";
label1.text="2";

This is just a suggestion, because the code is yours and you can program it your way :) . The question of clicking the Back button and undo the last change, this can be done using the windows registry to record the last value. Example of a code I made to take the test: inserir a descrição da imagem aqui

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Bingo2._0
{
    public partial class Form1 : Form
    {
        Microsoft.Win32.RegistryKey key;

        public Form1()
        {
            InitializeComponent();
        }

        private void bt1_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "1");
            lb_numero.Text = "1";
            bt1.Enabled = false;

        }

        private void bt2_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "2");
            lb_numero.Text = "2";
            bt2.Enabled = false;

        }

        private void bt3_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "3");
            lb_numero.Text = "3";
            bt3.Enabled = false;

        }

        private void bt4_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "4");
            lb_numero.Text = "4";
            bt4.Enabled = false;

        }

        private void bt5_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "5");
            lb_numero.Text = "5";
            bt5.Enabled = false;

        }

        private void bt6_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "6");
            lb_numero.Text = "6";
            bt6.Enabled = false;

        }

        private void bt7_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "7");
            lb_numero.Text = "7";
            bt7.Enabled = false;

        }

        private void bt8_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "8");
            lb_numero.Text = "8";
            bt8.Enabled = false;

        }

        private void bt9_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "9");
            lb_numero.Text = "9";
            bt9.Enabled = false;

        }

        private void bt10_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "10");
            lb_numero.Text = "10";
            bt10.Enabled = false;

        }

        private void bt11_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "11");
            lb_numero.Text = "11";
            bt11.Enabled = false;

        }

        private void bt12_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "12");
            lb_numero.Text = "12";
            bt12.Enabled = false;

        }

        private void bt13_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "13");
            lb_numero.Text = "13";
            bt13.Enabled = false;

        }

        private void bt14_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "14");
            lb_numero.Text = "14";
            bt14.Enabled = false;


        }

        private void bt15_Click(object sender, EventArgs e)
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Bingo2.0\\");
            key.SetValue("restaurar", "15");
            lb_numero.Text = "15";
            bt15.Enabled = false;

        }

        private void bnt_desfazer_ultima_alteracao_Click(object sender, EventArgs e)
        {
            string pegarValorRegistro = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Bingo2.0", "restaurar", null);

            try
            {

                if (pegarValorRegistro == "1")
                {
                    bt1.Enabled = true;

                }
                else if (pegarValorRegistro == "2")
                {
                    bt2.Enabled = true;
                }

                else if (pegarValorRegistro == "3")
                {
                    bt3.Enabled = true;
                }

                else if (pegarValorRegistro == "4")
                {
                    bt4.Enabled = true;
                }
                else if (pegarValorRegistro == "5")
                {
                    bt5.Enabled = true;
                }
                else if (pegarValorRegistro == "6")
                {
                    bt6.Enabled = true;
                }
                else if (pegarValorRegistro == "7")
                {
                    bt7.Enabled = true;
                }

                else if (pegarValorRegistro == "8")
                {
                    bt8.Enabled = true;
                }
                else if (pegarValorRegistro == "9")
                {
                    bt9.Enabled = true;
                }

                else if (pegarValorRegistro == "10")
                {
                    bt10.Enabled = true;
                }
                else if (pegarValorRegistro == "11")
                {
                    bt11.Enabled = true;
                }

                else if (pegarValorRegistro == "12")
                {
                    bt12.Enabled = true;
                }

                else if (pegarValorRegistro == "13")
                {
                    bt13.Enabled = true;
                }

                else if (pegarValorRegistro == "14")
                {
                    bt14.Enabled = true;
                }

                else if (pegarValorRegistro == "15")
                {
                    bt15.Enabled = true;
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro");
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

If you want I’ll let the project link to Voce to download. http://www.mediafire.com/download/423dw34fec9n2l7/Bingo2.0.rar

Thank you...

  • Dude, do you use win10? If not, how did you make the top edge of the form look like this?

  • is Windows 10 even I’m testing it!

  • No, mine is Windows 7 itself. I do not know how it looks like this, for me it is normal.

Browser other questions tagged

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