-1

I have several comboboxes and all of them are manually entered text. After the combox selection couldn't lose focus. I used:

this.ActiveControl = null;

It works but the focus is going to the first control always.

Could you please suggest to fix this issue?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
nav100
  • 2,923
  • 19
  • 51
  • 89
  • So you want all controls to lose focus? And when exactly do you want this to happen? Your question is far from clear. Maybe expand on a it for us -even a code sample. – Mike Perrenoud Apr 26 '13 at 20:15
  • 2
    I think its impossible to not have focus on any control. There always must be a control that is focused in winforms, but im not sure. Maybe you have to create invisible control and focus it. Vote up if im right. – Kamil Apr 26 '13 at 20:15
  • 1
    If I only I had such dedication. – Anthony Pegram Apr 26 '13 at 20:17
  • I click on the combobox and choose an item. Then I click the form but still can't lose the focus. I use mouse wheel to scroll the whole form. – nav100 Apr 26 '13 at 20:17
  • See [Panel not getting focus](http://stackoverflow.com/a/3562449/719186) – LarsTech Apr 26 '13 at 20:42

1 Answers1

0

Create a hidden control and set focus to that control. This way it will appear that nothing is focused. Something like this:

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
    ActiveControl = HiddenControl;
}

Here is a Visual Studio 2012 project: http://www.cameronjtinker.com/downloads/FocusTest.zip

Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
  • do you want me to try creating hidden control for each combobox next to it? – nav100 Apr 26 '13 at 20:24
  • No, you just need one hidden control and then you can assign it after your form `MouseClick` event or `ComboBox`es `SelectedIndexChanged` events. – Cameron Tinker Apr 26 '13 at 20:25
  • Is textbox OK for hidden control? – nav100 Apr 26 '13 at 20:32
  • Any control is fine for the hidden control as you won't be able to see it on the form. – Cameron Tinker Apr 26 '13 at 20:34
  • I set the visible property to false. I used above code ActiveControl = textbox6; It doesn't work. I have the same problem. – nav100 Apr 26 '13 at 20:43
  • 4
    You cannot set the focus to a hidden control. Setting to a control that's off the window is okay. Give it a Location with a large negative X property value. – Hans Passant Apr 26 '13 at 21:04
  • 1
    I have tested the code in my answer in both Visual Studio 2010 and Visual Studio 2012. You can indeed set the active control of the form to a hidden control. – Cameron Tinker Apr 27 '13 at 03:18