Hello, I am developing Bingo software for the Church that I attend.
Whenyoupressanynumberbutton,itwillappearinthePictureBoxontheside.Asthefollowingimageshows.
Now,noticethatthereisthe
Really the best way to do what you want is by using a stack. If you do not know what a data stack is, I suggest you eat by researching what a "stack" is. Here is a link to a video with a simple explanation Stack Data C # .
Below is the code for a small class to manipulate stack access and change the Enabled
property of the buttons, and then 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 PilotBoots.
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 suggest you make two changes to your code.
T
in this case can be int
, because the stack will be storing numbers (the numbers selected by the user). Well, let's go to the code then. First, declare a Stack object that will represent the history of numbers selected on your form as follows (remember to import the System.Collections.Generic
namespace with a using
directive at the top of the form's source code file):
private Stack<int> _historicoDeNumerosSelecionados = new Stack<int>();
The method to be called in the click event of each button should 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 constructor you should put the following code, which dynamically assigns 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));
}
Since the BotaoNumerico_Click
event will be assigned to all numeric buttons, you can now remove all button event declarations from button1_Click
to button75_Click
. With this refactoring we removed approximately 450 lines of duplicate code.
Now on the event of the Back button you do the reverse process based on the last number that was added in 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;
}
Hello, seeing your code I'll give you a suggestion, it would not be better if you used a label instead of the image and set the values of the numbers on it. Ex: in the click of button 1 you use
btn1 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo2.0\Img.jpg");
img_box.Image = btn1;
It would not be more feasible for you to use this way:
label1.Text = "1";
So you'll spend less time and your code will get cleaner and will give you less work because instead of putting in each event click the button a code of this
btn1 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo2.0\Img.jpg");
img_box.Image = btn1;
btn2 = Image.FromFile(@"C:\Users\Dudu\Desktop\Softwares\Bingo2.0\Img.jpg");
img_box.Image = btn2;
Place 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 undoing the last change, this can be done using the windows registry to save the last value. Example of a code I made to take the test:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMicrosoft.Win32;namespaceBingo2._0{publicpartialclassForm1:Form{Microsoft.Win32.RegistryKeykey;publicForm1(){InitializeComponent();}privatevoidbt1_Click(objectsender,EventArgse){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)
{
}
}
}
In case you want to leave the project link for you to download. link
Thank you ...