How to convert a ComboBox from String to Int?

2

I need to converge a ComboBox to which I call cmbmes . The reality is that it does not work with Convert.ToInt32() or with Int.Parse() . The error that gives me in specific is System.FormatException .

What I want to achieve is that when I select an Item (Number) of the ComboBox, it tells me in a TextBox the Month.

The code is this:

    private void Form1_Load(object sender, EventArgs e)
    {
        //Numero de Meses del Año
        for (int i = 1; i <= 12; i++)
        {
            cmbmes.Items.Add(i);
        }

        //Para seleccionar el Mes
        int numero;                       
        numero = int.Parse(cmbmes.Text);

        if (numero == 1)
        {
            txtmes.Text = "Enero";
        }
        else if (numero == 2)
        {
            txtmes.Text = "Febrero";
        }

    }

If you know another method, I would really appreciate it.

    
asked by Jatniel Nuñez 30.05.2018 в 17:13
source

3 answers

8

Although it seems that you have solved it, I am going to add an answer in case someone is in the same situation and does not want to reinvent the wheel. In .net there is the GetMonthName(int) method that returns your name of the month by passing an integer with the month number. In your case the code would be reduced to:

//Hay que añadir este using
using System.Globalization;
...
private void cmbmes_SelectedIndexChanged(object sender, EventArgs e)
{
    txtmes.Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(int.Parse(cmbmes.Text));
}

If you need the first letter of the month to be uppercase, just add below txtmes.text = ... this line:

txtmes.Text = txtmes.Text.First().ToString().ToUpper() + txtmes.Text.Substring(1);
    
answered by 30.05.2018 / 18:06
source
0
int numero;

private void Form1_Load(object sender, EventArgs e)
    {
        //Numero de Meses del Año
        for (int i = 1; i <= 12; i++)
        {
            cmbmes.Items.Add(i);
        }
    }

private void cmbmes_SelectionChangeCommitted(object sender, EventArgs e)
{
    //Para seleccionar el Mes                      
        numero = Convert.ToInt32(cmbmes.Text);

        if (numero == 1)
        {
            txtmes.Text = "Enero";
        }
        else if (numero == 2)
        {
            txtmes.Text = "Febrero";
        }
}

Try this ... I have not touched c# for a while but it's something like that .. I'll explain a bit about the code

In the load event of form we add the items to your combobox

And then in the event SelectionChangeCommitted of your combobox we collect the number that we have selected from this, and we only compare and display in your textbox the month depending on the number ...

    
answered by 30.05.2018 в 17:35
-1

but finally I can find the answer without error.

    private void Form1_Load(object sender, EventArgs e)
    {
        //Numero de Meses del Año
        for (int i = 1; i <= 12; i++)
        {
            cmbmes.Items.Add(i);
        }
    }

    private void txtmes_TextChanged(object sender, EventArgs e)
    {           
    }

    private void cmbmes_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Convert.ToInt32(cmbmes.Text)==1)
        {
            txtmes.Text = "Enero";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 2)
        {
            txtmes.Text = "Febrero";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 3)
        {
            txtmes.Text = "Marzo";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 4)
        {
            txtmes.Text = "Abril";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 5)
        {
            txtmes.Text = "Mayo";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 6)
        {
            txtmes.Text = "Junio";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 7)
        {
            txtmes.Text = "Julio";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 8)
        {
            txtmes.Text = "Agosto";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 9)
        {
            txtmes.Text = "Septiembre";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 10)
        {
            txtmes.Text = "Octubre";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 11)
        {
            txtmes.Text = "Noviembre";
        }
        else if (Convert.ToInt32(cmbmes.Text) == 12)
        {
            txtmes.Text = "Diciembre";
        }
    }
}

}

    
answered by 30.05.2018 в 17:53