【发布时间】:2012-01-31 20:19:52
【问题描述】:
对于 ComboBox,当我设置 SelectionLength = 0 时,出现错误:
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()
这不是在Clear() 之后,也不是绑定控件。
这段代码中(不那么)有趣的地方:
//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
在我们的代码中,我们没有指定 SelectionStart,但是当我看到上面包含的代码时,它已经有了 -1470366488 的值。我假设这是在 ComboBox 执行
时使用的ComboBox.Select(Int32 start, Int32 length)
调用,通过设置 SelectionLength 触发。我假设 SelectionStart 用于开始参数和 viola,我们在上面显示了 InvalidExceptionArgument。
这是在调试代码中。样式是DropDownStyle,其他一切看起来都没什么特别的,但在调试器中我看到SelectionStart 属性是-1470366488。
这段代码已经存在几年了,今天我在测试调试版本时第一次遇到这个异常。我正在选择我想用 SelectedIndex = 2 行显示的项目,然后在设置 SelectionLength 时出现异常 有什么解释吗?
【问题讨论】:
-
我认为问题在于您对
Selection的解释 - 您是要选择组合框中的文本,还是要Select一个项目? -
嗯。根据例外情况,您为值指定
-1470366488,而不是 0。您显示的代码实际上不包括您设置SelectedIndex属性的行,所以我不知道是什么出错了。SelectionStart与SelectedIndex无关;这可能是混乱的根源吗? -
davisoa - 我正在尝试确保在文本框中没有选择任何文本。
-
Cody - 查看帖子以获取更新,谢谢。
-
davisoa - 我解决了问题中的问题,现在应该更有意义了。谢谢,