Find out how many are older in Windows Forms app

Asked

Viewed 462 times

0

I’m trying to do a C# exercise using Windows Forms that are older than 18 years. 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");
            }
        }
    }
}

2 answers

3

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

I can’t do the best I can even by not having access to the whole 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 in the Github for future reference.

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

If you do not perform a conversion with verification as 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 is good.

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

I made assumptions about having these two Labels I gave better names, but I don’t even like them.

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

0


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

  • puts...that dumb I didn’t even think that way...Thank you so much for the help...really mto grateful...sorry but I’m starting in programming.

  • It is not to be stupid, you as you are starting in programming have not yet possessed a good logic just... If the answer helped you don’t forget to mark it as Accepted, or the other answers!

  • yes helped me, I marked as accepted,once again thank you.

  • another question if I want to show 2 msgs showing the Qtde of older and younger than 18 years, I will have to create one more variable to store the Qtde of smaller...??

  • That’s right and how it’s starting, adopt a pattern for variable names, so you don’t get lost! Create for example, for integers, int iQtdMaiores, iQtdMenores, where "i" at the beginning of the name indicates that the variable is of the integer type

Browser other questions tagged

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