954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

comboBox Selected Item Problem

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 ?

capiono
Newbie Poster
22 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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 =.

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 

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?

capiono
Newbie Poster
22 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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.

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 

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.

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 

I did and the value was still -1

capiono
Newbie Poster
22 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

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

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 

wait I think you need
4. set { SelectedIndex = value; }
try it let me know.

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 

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

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

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 
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

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 

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.

capiono
Newbie Poster
22 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

thanks I will try this and let you know

capiono
Newbie Poster
22 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Yeah I know you could declare your months a a constant intiger. but that prob won't help your -1 result.
Hey here is a mention of a "property bag" much like your ViewState-->

http://www.codeproject.com/KB/recipes/propertybag.aspx

jamesonh20
Junior Poster in Training
66 posts since Dec 2009
Reputation Points: 22
Solved Threads: 6
 

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.

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: