I do not know how you are filling your ComboBox
but here I show you an example of how you could do it.
private void llenarComboMeses(ComboBox combo)
{
List<string> lista_meses = new List<string>();
lista_meses.Add("Enero");
lista_meses.Add("Febrero");
lista_meses.Add("Marzo");
lista_meses.Add("Abril");
lista_meses.Add("Mayo");
lista_meses.Add("Junio");
lista_meses.Add("Julio");
lista_meses.Add("Agosto");
lista_meses.Add("Septiembre");
lista_meses.Add("Octubre");
lista_meses.Add("Noviembre");
lista_meses.Add("Diciembre");
//Limbiar los datos del combo, para no duplicarlos al volver a generarlos
combo.DataSource = null;
combo.Items.Clear();
//Recorrer la lista y asignar los valores al combo
foreach (string mes in lista_meses)
{
//Asignando los valores.
combo.Items.Add(mes);
}
}
To use the function like this:
llenarComboMeses(CmbMes);
Then to get the value of the selected month you can do it in the event SelectedIndexChanged
of your ComboBox
like this:
private void CmbMes_SelectedIndexChanged(object sender, EventArgs e)
{
if (CmbMes.SelectedIndex >= 0)
{
//Asignar el valor a tu variable.
mes = CmbMes.SelectedIndex + 1;
}
}
This is done: CmbMes.SelectedIndex + 1;
adding 1 because the index starts at zero therefore when adding one the index 1 will be equal to Enero
, then index 2 = Febrero
, etc ...