Find out how many are older in Windows Forms application

0

I'm trying to do an exercise in C # using Windows Forms that 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");
            }
        }
    }
}
    
asked by anonymous 12.06.2018 / 19:20

2 answers

0

In the way you did, you might need to use an InputBox, but here's how you can do it:

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

    
12.06.2018 / 19:44
3

You are trying to use console logic in a GUI application. I'd have to ask for 10 different data inputs or at least make some adjustments to this logic.

I can not do my best even by not having access to the whole code, but I would do something like this if you choose to have only one 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
    }
}

The count and control must be made outside the method, it belongs to the form object as a whole. In console do not need because there can only be one console then the data is worth even the same.

If I do not do a conversion with verification like I did in the code it might break. I do not know if it's the best way to make a mistake, but for an exercise that's fine.

In a real code, the majority number would probably be in a constant state to facilitate maintenance.

I made a guess about having these two labels that gave better names, but I do not even like them.

If nothing is done to stop typing, such as closing the form or disabling it from txtidade1 , then it would be nice to check if you have already reached 10 as the first line of this method, nor let anything execute.

    
12.06.2018 / 20:23