Find out how many are of age in Windows Forms application

I'm trying to do an exercise in C# using Windows Forms that how many are over 18 years old.The algorithm should read the age of 10 people. But when I run the program it lets me just type an age.

public partial class idade : Form
    {
        int qtde, id,i;
        public idade()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            while (i < 10)
            {
                i = i + 1;
                MessageBox.Show("Digite a idade");
                id = Convert.ToInt32(txtidade1.Text);

                if (id >= 18)
                {
                    qtde = qtde + 1;
                }
                MessageBox.Show("Existem" + qtde + "pessoas com mais de 18 anos");
            }
        }
    }
}
Author: Maniero, 2018-06-12

2 answers

The way you did, you might need to use an InputBox, but you can do it this way here:

public partial class idade : Form
{
   {
       int qtdTotal, qtde, id;
       public idade()
       {
           InitializeComponent();
           qtdTotal = 0;
           qtde = 0;
       }

       private void button1_Click(object sender, EventArgs e)
       {
          qtdTotal += 1;

          id = Convert.ToInt32(txtidade1.Text);
          if (id >= 18)
            qtde += 1;

          if(qtdTotal == 10)
          {
            MessageBox.Show("Existem" + qtde + "pessoas com mais de 18 anos");
            qtdTotal = 0;
          }
          else
          {
            MessageBox.Show("Digite a próxima idade");
          }
       }
   }
}

Here you type the age in the textbox and then click the button again until the count reaches 10

 0
Author: Matheus Ribeiro, 2018-06-12 17:54:27

Is trying to use console logic in a GUI application. It would have to ask for 10 different data entries or at least make some adaptation in this logic.

I can't do my best even because I don't have access to the entire code, but I would do something like this if I choose to have only one data input field:

public partial class IdadeForm : Form {
   private int Entrados = 1, Maiores;
   public idade() {
       InitializeComponent();
   }

   private void button1_Click(object sender, EventArgs e) {
       if (!int.TryParse(txtidade1.Text, ou var idade)) {
           MessageBox.Show("A idade digitada não é válida");
           return;
       }
       if (idade > 17) Maiores++;
       lblMaiores.Text = $"O maiores de idade contados são {Maiores}";
       lblIdade.Text = $"Digite a idade {++Entrados}:";
       if (Entrados == 10) //lógica que não permite mais entrar dados ou reset
    }
}

I put on GitHub for future reference .

The Count and control must be done outside the method, it belongs to the object form as a whole. In console it does not need because only one console can exist so the given general goes even.

If I don't do a conversion with verification like I did in the code it may break. I do not know if it is the best way to present the error, but for an exercise it is good.

In a real code probably the number of the age of majority would be in a constant, for ease of maintenance.

I made an assumption about having these two labels that I gave names better, but I don't even like them.

If nothing is done to stop typing, such as closing the form or disabling such of txtidade1, then it would be good to check if it has already reached 10 as the first line of this method, nor letting anything run.

 3
Author: Maniero, 2020-07-22 15:59:34