C # - If in ComboBox Exercise Caelum

0

Good afternoon, I've been trying to resolve this exercise in Caelum's C # handout for a few hours, but I think it's a long way from solving it. I would like to know how to use an IF within a ComboBox to make it distinguish between a checking account and a savings account at the time of creating the account. Here is the code I have been able to produce so far. This is the code for the registration form only. In the main form everything is ok so far hehe ... This is the exercise question itself: In the registration form, add a combo box (called comboType) that allows the choice of the type of account 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);

            } 
        }
    }

}
    
asked by anonymous 27.12.2017 / 17:22

1 answer

0

As maniero quoted, it's really complicated that there ... kkkk, you have to understand what each code does. Try to help you:

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

Now, in the "Register" button, there you have to create the account, right?

then you should do:

  

Considering that position 0 will always refer to the current account, and position 1 will be savings, and that 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 for you to try to understand, without getting into the other problems that the code has

    
27.12.2017 / 18:18