Lindsay Morsillo
Lindsay Morsillo

Reputation: 530

ComboBox.SelectionLength = 0 throws InvalidArgument Exception

For a ComboBox, when I set the SelectionLength = 0, I get the error:

InvalidArgument=Value of '-1470366488' is not valid for 'start'.
Parameter name: start
Stack Trace: 
at System.Windows.Forms.ComboBox.Select(Int32 start, Int32 length)
at System.Windows.Forms.ComboBox.set_SelectionLength(Int32 value)
at MyCompany.Odin.WebClient.STComplexView.loadViewFormats()

This isn't after a Clear(), nor is it a bound control.

The (not-so) interesting things in this code:

//Adding Items to the combo box (6 in total)
// ...
viewFormatComboBox.Items.Add(appResMgr.GetString("STR_6X2_HEXAXIAL"));
viewFormatComboBox.SelectedIndex = 2;
viewFormatComboBox.SelectionLength = 0;   //<<<< The exception is thrown here

Nowhere in our code do we specify SelectionStart, but it's already got the -1470366488 value when I get to the code I included above. I assume this is used when the ComboBox does a

ComboBox.Select(Int32 start, Int32 length) 

call, triggered by setting SelectionLength. I assume that SelectionStart is used for the start argument and viola, we have the InvalidExceptionArgument show above.

This is in debug code. The style is DropDownStyle, everything else looks to be unremarkable, but in the debugger I see that the SelectionStart property is -1470366488.

This code has been in place for a couple of years, and I run into this exception today for the first time when testing a debug build. I am selecting the item I want displayed with the SelectedIndex = 2 line, then I get the exception when setting SelectionLength Any explanations?

Upvotes: 0

Views: 3960

Answers (3)

Dan Hannaway
Dan Hannaway

Reputation: 11

In the case of a Combo Box Style of DropDownList the Combo Box Control does not really have a 'Text' box entity. So setting that object's SelectionStart or SelectionLength has unpredictable results, as mentioned above. As mentioned in other posts (Weird behavior caused by using .Net ComboBox properties SelectionStart & SelectionLength in "DropDownList" mode , http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/thread/80B562C9-981E-48E6-8737-727CE5553CBB ) it is also suggested not to use these properties, I see why. But testing for that style and not setting the Start value fixed our issue.

If Not Me.DropDownStyle.Equals(Windows.Forms.ComboBoxStyle.DropDownList) Then Me.SelectionStart = Me.Text.Length End If

Hope this helps someone in the future...

Upvotes: 1

Steven P
Steven P

Reputation: 1996

Here's an explanation of to what rather than why.

The setter for SelectionLength calls:

this.Select(this.SelectionStart, value);

The first line checks arg validity

 if (start < 0)
      {
        throw new ArgumentOutOfRangeException("start", System.Windows.Forms.SR.GetString("InvalidArgument", (object) "start", (object) start.ToString((IFormatProvider) CultureInfo.CurrentCulture)));
      }

As you pointed out, your SelectionStart as a value of -1470366488. The question is why? The getter for SelectionStart calls:

int[] wParam = new int[1];
System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef((object) this, this.Handle), 320, wParam, (int[]) null);
return wParam[0];

Msg 320 (0x140) is CB_GETEDITSEL. According to the docs, that should return:

Gets the starting and ending character positions of the current selection in the edit control of a combo box.

Clearly it's not. It's returning -1470366488. Why? Who knows. I would guess CB_GETEDITSEL is returning an error, which is not checked and wParam[0] is undefined and the framework is just blindly using it.

Perhaps explicitly setting SelectionStart (which sends a CB_SETEDITSEL) before setting SelectionLength will minimise the chance it could happen.

Upvotes: 1

ChrisF
ChrisF

Reputation: 137148

From the exception and call stack it looks like the simplest solution would be to insert:

viewFormatComboBox.SelectionStart = 0;

before

viewFormatComboBox.SelectionLength = 0;

to ensure that it has a valid value.

Upvotes: 2

Related Questions