C#/.NET - ComboBox Question

Associate
Joined
14 Oct 2008
Posts
416
I'm currently in the process of making a desktop application in C# but I'm having some problems getting a combo box working as I expected.

Basically, I've got a combo box filled with values. When the user presses a button it should do something with the value in the combo box and then automatically move to the next item. (I'm using SelectedIndex to do that).

Now, on the first click of the button, everything works OK and the combo box moves down one place (at least, it does visually)... but when it's clicked again, the program crashes (ArgumentOutOfRangeException). It's setting the IndexSelected value to -1, which according to the documentation means no item is selected. When I manually select the combo box again and click the item that (visually) is already selected... everything works fine.

So my question is, is this how combo boxes are supposed to work (not good for automated stuff) or am I doing something stupid? I've tried putting a bit in the code to give the combo box focus again (comboBox1.Focus()) but it doesn't seem to make any difference.

Sorry for waffling on a bit! Hope it made sense.

Thanks in advance.
 
Need to see your code from the click of the combobox to see your automation code.

Started out as a mess around and got a bit carried away, so apologies for it being pretty badly designed so far! :p Going back to redo it all soon but it'd be nice if I could get this sorted first.

Here's the code from the button click.

Code:
        private void CuePoint_Click(object sender, EventArgs e)
        {
            posnow = axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
            
            if (comboBox1.SelectedIndex == 0)
            {
                MessageBox.Show("The first track must start at 00:00:00, please set the cue point for track two instead.", "Invalid First Track Index", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if (tracklist.Lines.Count() == 0)
            {
                MessageBox.Show("Please paste a tracklist into the left hand box and select a track in the drop down box before using this feature.", "Paste Tracklist First", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if (cueTime.Count == 0)
            {
                foreach (String track in tracklist.Lines)
                {
                    cueTime.Add("00:00:00");
                }
                cueTime[comboBox1.SelectedIndex] = timeCode(posnow);
                firstCueSet = true;
            }
            else if (cueTime.Count > comboBox1.SelectedIndex)
            {
                cueTime[comboBox1.SelectedIndex] = timeCode(posnow);
                firstCueSet = true;
            }
            if (autoAdvance.Checked == true && comboBox1.SelectedIndex < cueTime.Count)
            {
                comboBox1.SelectedIndex++;
            }
            buildCue();
        }

If that doesn't help, let me know which other bits would be useful to see and I'll post them as well.
 
Nope, wasn't that :( It was happening even on the first few items of the combo box when there was about 10 values.

Cheers for spotting that anyway though, changed it.

Oops... just literally as I was typing this, I realised what it was :\ The combo box was being cleared and then refilled in another function to allow for dynamic changes, and it was being accessed after it had been cleared. Doh! God knows how I managed to miss that.
 
Last edited:
Back
Top Bottom