【问题标题】:change native behavior of ComboBox - comboBox is highlighted when changes its size更改 ComboBox 的本机行为 - 更改其大小时突出显示组合框
【发布时间】:2019-02-19 00:42:19
【问题描述】:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/e04e9308-bff5-4fbb-8cd6-0b2cd957aa68/?prof=required

根据另一个论坛,这不是 MS 问题,因为他们说这是 CombBox 的“本机”行为。

如果ComboBox有一个DropDownStyle=DropDown并且改变了它的大小,文本会被高亮,如果表单有很多ComboBox似乎是控件被选中了。

为了避免这个问题,有人建议覆盖 WndProc。 一切正常,直到只有一个客户报告了未处理的错误

System.ArgumentOutOfRangeException: InvalidArgument=Value of '-2136611475' is not valid for 'start'.
Parameter name: start
   at System.Windows.Forms.ComboBox.Select(Int32 start, Int32 length)
   at System.Windows.Forms.ComboBox.set_SelectionLength(Int32 value)...............


class ComboBoxEx : ComboBox
{
    const int WM_SIZE = 5;

    protected override void WndProc(ref Message m)
    {
        switch(m.Msg)
        {
            case WM_SIZE:
                string text = Text;
                base.WndProc(ref m);

                //The exception strangely is trown here
                SelectionLength = 0;

                Text = text;
                break;

            default:
                base.WndProc(ref m);
                break;
        }
    }
}

我不知道为什么只有一个客户会发生这种情况。

【问题讨论】:

  • 我不知道答案,但您可能应该为此添加一个 windows 窗体标签。可能有助于在那些花更多时间在这些问题上的人面前展示它。

标签: c#


【解决方案1】:

我想不出会导致这种情况的情况,但我有一个解决方法。我在反射器中检查了 set_SelectionLength(Int32 value):

this.Select(this.SelectionStart, value);

我不明白为什么 SelectionStart 突然变成负数,但您可以切断中间人并执行此解决方法代码:

this.Select(0, 0);

【讨论】:

  • 感谢 HuBeza,这个解决方法效果很好,很简单,而且是个好主意。
猜你喜欢
  • 2012-10-24
  • 1970-01-01
  • 2016-11-23
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
相关资源
最近更新 更多