views:

394

answers:

2

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));
A: 

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.
Andrey
A: 

Hi, I have the same problem, I have a custom ComboBox to avoid the behavior of the comboBox, everything was working fine, but for any strange reason in a few scnearios I have this problem, so the ArgumentOutOfRangeException appears. It seems that something installed in the computer produces the invalid parameter.

I can use the a try catch but this going to affect the performance of the app. Any ideas? thank you..

luisnike19

MSDN Forum - ComboBox Resize Oddity

Luisnike19
I don't see why .net try/catch should be a hit in performance of your app. I am not experienced in .net, though. I feel like usually .net code is full of those. Alternatively you can try checking suggestions provided in my own reply to this topic - check that your combobox type is appropriate for using SelectionStart/SelectionLength. In one application that had the same issue - I was able to avoid exception by using ".Select" method instead of SelectionStart/SelectionLength properties. you can also check what applications inject their DLLs into your app, though not much you can do with that
Andrey
@Luisnike19: If you have a follow up question you should post it as a new question,not as an answer to an old question.In the top right is an "Ask Question" button to do so.
sth
It's nice that someone else had seen this behavior :) if in your case the problem is caused by the hook, as I described, then I know only one workaround - install your hook of the same type but don't invoke other hooks if the message is for your combobox. Ugly, but does it's dirty work.
Andrey
BTW, I agree that putting dozens of try/catches is bad idea ...
Andrey