C# - If in Combobox Exercise Caelum

Asked

Viewed 404 times

0

Good afternoon guys, I’m already a few hours trying to solve this C# workbook exercise of Caelum, but I believe I am far from solved. I would like to know how to use an IF within a Combobox to make it distinguish between a current account and a savings account at the time of account creation. It follows the code that I have been able to produce so far. This is the code only of the registration form. In the main form everything is ok so far hehe... This is the question itself of exercise: In the registration form, add a combo box (called comboTipoConta) that allows the choice of the account type that will be registered. thank you in advance.

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

namespace Banco1
{

    public partial class FormCadastroConta : Form
    {
        private Form1 formPrincipal;

        public ComboBox tipoConta { get;  set; }

        public FormCadastroConta(Form1 formPrincipal)
        {
            this.formPrincipal = formPrincipal;
            InitializeComponent();
            comboTipoConta.Items.Add("Corrente");
            comboTipoConta.Items.Add("Poupança");
        }
        public FormCadastroConta()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void botaoCadastro_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void comboTipoConta_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(comboTipoConta.Items.Add("Corrente")))
            {
                ContaCorrente novaConta = new ContaCorrente();
                novaConta.Titular = new Cliente(textoTitular.Text);
                novaConta.Numero = Convert.ToInt32(textoNumero.Text);
                this.formPrincipal.AdicionaConta(novaConta);

            }
            else if (Convert.ToBoolean(comboTipoConta.Items.Add("Poupança")))
            {
                ContaPoupança novaConta = new ContaPoupança();
                novaConta.Titular = new Cliente(textoTitular.Text);
                novaConta.Numero = Convert.ToInt32(textoNumero.Text);
                this.formPrincipal.AdicionaConta(novaConta);

            } 
        }
    }

}
  • 2

    I doubt I can help without knowing exactly what it is to do, but anyway I see several errors in this code, many that seem correct, which are the most dangerous.

  • give a discount to start now kkkkk what was not clear pq da to know what I have to do

1 answer

0


As Father Niero said, it’s very complicated there... kkkk, you have to understand what makes each code. Try to help you:

comboTipoConta_SelectedIndexChanged is an event, that is, this code will be executed when the Index selected from the combo is changed. Is this what you need? No! rsrs can remove this code...

Now, on the "Register" button, then you have to create the account, correct ?

then you should do:

Whereas position 0 will always refer to the current account, and position 1 will be savings, and there will only be these two positions:

 private void botaoCadastro_Click(object sender, EventArgs e)
 {
      if (comboTipoConta.SelectedIndex ==0)
      {
         //cadastra conta corrente
      }
      else
      {
         //cadastra conta poupança
      }

     this.Close();
 }

It’s very simple, more so that you try to understand, without getting into the other problems that the code has

  • Before it was on the sign up button but then I took it because this way it started to work a little bit well. But I saw that I was in the rsrs... by what you showed I could understand. I’ll try here. Thanks a lot man.

  • 1

    It worked, as soon as I saw the comboTipoConta.Selectedindex =0 remembered that they always start from scratch... If it weren’t for you, you’d be screwed. Valeuuuuuu.

  • if solved your problem, even if punctual, please check the answer please =]

Browser other questions tagged

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