I have property method in my window forms:

public int  selectMonthIndex
    {
        get { return selectMonthIndex = =monthSelection.SelectedIndex; }
        set 
        {  
            if (monthSelection.SelectedValue.ToString() == "February")
            {
                selectMonthIndex = 2;
            }
        }
     }

After the selections and the OnSelectedIndexChange was triggered the user enters information and click save. the save function is suppose to check for the selected item or index in order to pick the right DB to save to but the index or item is always null.
Please how do I fix this ?

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Hey im a newbie too. I'm not seeing enough info on what your doing. Did you create a savefiledialog? Also, I see that the above code basically says if the user selects a month and it is equal to february, it will return 2. Your above code will throw an error because of the space between the =.

Actually, this is the issue. I have a comboBox in my windows forms. the user select a month and enter the data for that month and click the save button. the comboBox current index determines where the data is saved.
However, when the save button is click and I try to get the combo.selectedIndex using a property:

public int  selectMonthIndex
        {
            get { return selectMonthIndex = monthSelection.SelectedIndex; }
            set { selectMonthIndex = value; }
        }

it returns (-1). How do I get the current index?

Look man I'm a beginnger, but i think your problem lies within line 4 inside the {}. I don't know the right answer but I think you need to reference the SelectedIndex in line 4 as well.

Hey try to select another month than Feb. and tell me the value it gives you. I might have a theory as to why you are getting -1.

Ok well my theory was wrong then... shoot!
I'm thinking the in line four you need a ToString() statement that references the SelectedIndex.

it's either that last code or something like this-->

set

    { 
        this.comboBox1.Text = value.ToString(); 
    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

public enum Test 
{ 
    One, Two, Three 
} 
 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
        InitializeComponent(); 
 
        this.comboBox1.DataSource = Enum.GetNames(typeof(Test)); 
    } 
 
    public Test Test 
    { 
        get  
        { 
            return (Test)Enum.Parse(typeof(Test), this.comboBox1.Text); 
        } 
        set 
        { 
            this.comboBox1.Text = value.ToString(); 
        } 
    } 
 
    private void button1_Click(object sender, EventArgs e) 
    { 
        MessageBox.Show(this.Test.ToString()); 
 
        this.Test = Test.Two; 
 
        MessageBox.Show(this.Test.ToString()); 
    }

Set the DropDownStyle of the ComboBox to DropDownList. This will ensure that only the elements already in the list can be selected (no need to check that the text actually is a valid value). Then if you use Enum.GetValues(typeof(BookType)) to fill the combobox then typeComboBox.SelectedItem property will be a value of BookType. So you can use this in the property getter and setter.

So to summarize. You don't have to bind the combobox to a list of text values as long as you use the DropDownList style. Use the SelectedItem property to get an item of the wanted type instead of checking the Text property.

Edit: You may have to check the SelectedItem property for null

thanks man, but both ideas didn't help.
If windows forms allowed ViewState like in asp.net web forms it will be must easier. Then you can store the value in the viewState when the ComboBox event is triggered.

First of all, unless you intend to programmatically set the selected index of the combobox, you dont need the set {} value in your property. You can create it as a read only property:

public int  selectMonthIndex        
{
get { return monthSelection.SelectedIndex; }      
}

The way a property works is that the get method should return a value and the set method assigns one. So if you wanted to keep the set method it would be something like set { monthSelection.SelectedIndex = value; } . 'value' is the value assigned to the set method: selectMonthIndex = 3 would result in the selectedIndex being 3.

This question has already been answered. Start a new discussion instead.