本文介绍了使用.Net ComboBox属性引起的奇怪行为SelectionStart& “DropDownList”中的SelectionLength模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个示例应用程序,在DropDownList模式下的组合框中使用这样的处理程序:

  private void comboBox1_Leave ,EventArgs e)
{
comboBox1.SelectionStart = 0;
comboBox1SelectionLength = 0;
}

上面的代码表现不同,取决于应用程序是否装载了CALLWNDPROC钩子。如果应用程序有一个CALLWNDPROC钩子 - 上面的代码将抛出异常每当combobox失去焦点。没有钩子 - 这段代码不会抛出。



这些是异常描述的几行:

  System.ArgumentOutOfRangeException:InvalidArgument =值'-2136611475'对'start'无效。 
参数名:start
在System.Windows.Forms.ComboBox.Select(Int32开始,Int32长度)
在System.Windows.Forms.ComboBox.set_SelectionLength(Int32值)
at ComboCrash.Form1.comboBox1_Leave(Object sender,EventArgs e)in T:\tmp.proj\ComboCrash\ComboCrash\Form1.cs:line 32
在System.Windows.Forms.Control.OnLeave (EventArgs e)
在System.Windows.Forms.Control.NotifyLeave()
在System.Windows.Forms.ContainerControl.UpdateFocusedControl()

问题是:安装钩子后,可能是什么原因导致不同的行为?



PS1:我不是一个C#开发人员,但在我看来,文本选择的概念不适用于DropDownList组合框(因为他们没有一个文本框),是正确的吗?



PS2:安装钩子和钩子DLL的应用程序是用C ++编写的。
Hook函数很简单:

  return(CallNextHookEx(hook_handle,code,wParam,lParam) 


解决方案

提供一些:


  1. 如果您的组合框是DropDownList,那么使用SelectionStart和SelectionLength属性是不正确的。在这些情况下,CB_GETEDITSEL被发送到combobox的窗口 - 它不会返回任何可靠的(因为没有编辑控制来查询)。所以 - 只是不这样做(或用try-catch包含相应的代码)!或者 - 始终检查您的组合框的类型。

  2. 违反上述建议可能会导致(a)没有异常; (b)未处理的异常或异常程序终止(取决于JIT设置)。如果在您的环境中安装了系统范围的WH_CALLWNDPROC钩子,情况(b)非常稳定。

2011年2月
正如我在这里的评论中提到的,只有解决方法(read-dirty hack)是安装你自己的相同类型的钩子(WH_CALLWNDPROC),但不要调用其他钩子消息是为您的组合框。是的,很丑陋。


We have a sample application with such handler for a combobox in "DropDownList" mode:

    private void comboBox1_Leave(object sender, EventArgs e)
    {
        comboBox1.SelectionStart = 0;
        comboBox1.SelectionLength = 0;
    }

the code above behaves differently depending whether the application has CALLWNDPROC hook loaded or not. If application has a CALLWNDPROC hook in it - the code above would throw exception whenever combobox loses focus. Without the hook - that code doesn't throw.

these are few lines from exception description:

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)
   at ComboCrash.Form1.comboBox1_Leave(Object sender, EventArgs e) in T:\tmp.proj\ComboCrash\ComboCrash\Form1.cs:line 32
   at System.Windows.Forms.Control.OnLeave(EventArgs e)
   at System.Windows.Forms.Control.NotifyLeave()
   at System.Windows.Forms.ContainerControl.UpdateFocusedControl()

The question is: What might be the cause of that different behavior with a hook installed?

PS1: I am not a C# developer, but it seems to me that concept of textual selection is not applicable for DropDownList comboboxes (as they don't have a textbox), is it correct?

PS2: Application that installs the hook and a hook DLL are written in C++. Hook function is as simple as:

return (CallNextHookEx(hook_handle, code, wParam, lParam));
解决方案

ok, as there are no suggestions yet I'll provide some:

  1. it is not correct to work with SelectionStart and SelectionLength properties if your combobox is a DropDownList. In these cases CB_GETEDITSEL is sent to combobox's window - and it will not return anything reliable (because there is no edit control to query). So - just don't do that (or enclose corresponding code with try-catch)! Or - always check the type of your combobox.
  2. violation of the previous recommendation might result in (a) nothing unusual; (b) an unhandled exceptions or abnormal program termination (depending on JIT settings). Case (b) is very stable in case if there are system-wide WH_CALLWNDPROC hooks installed in your environment.

Update on February 2011As I mentioned somewhere in the comments here, only workaround (read - dirty hack) was to install your own hook of the same type (WH_CALLWNDPROC), but don't invoke other hooks if the message is for your combobox. Yes, it's ugly.

这篇关于使用.Net ComboBox属性引起的奇怪行为SelectionStart& “DropDownList”中的SelectionLength模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:39