469,360 Members | 1,774 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

C# ComboBox hijacks the focus

I have searched high and low for a solution to this incredibly vexing problem and am at the brink of insanity/desperation/severe upsettedness. I have a problem with ComboBoxes in .NET. Once I click on the ComboBox (to choose from the dropdown list), the control stays focused/selected/active with no apparent way of deactivating it. I've created a small program that demonstrates my problem. If you comment out the lines pertaining to the comboBox1, you'll notice that the form successfully receives all keyboard input and displays it in label1. But when you add the code for comboBox1, there's no way to get the focus off of the control, so the keyboard events are never raised for the Form and nothing works at all. So the big question is how to I get the keyboard focus away from the ComboBox? As you can see, I've tried a few different methods already.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace WindowsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         ComboBox comboBox1;
  14.         Label label1;
  15.  
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.  
  20.             comboBox1 = new ComboBox();
  21.             comboBox1.Items.Add("one");
  22.             comboBox1.Items.Add("two");
  23.             comboBox1.Items.Add("three");
  24.             comboBox1.Location = new Point(10, 12);
  25.             comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
  26.             this.Controls.Add(comboBox1);
  27.  
  28.             label1 = new Label();
  29.             label1.Location = new Point(139, 15);
  30.             label1.Text = "a";
  31.             this.Controls.Add(label1);
  32.         }
  33.  
  34.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  35.         {
  36.             comboBox1.SelectNextControl(this, true, true, false, false);
  37.             this.Focus();
  38.             //this.SelectNextControl(this, true, true, false, false);
  39.             //comboBox1.SelectNextControl(this, true, true, false, false);
  40.             //this.Select();
  41.         }
  42.  
  43.         private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  44.         {
  45.             label1.Text = e.KeyChar.ToString();
  46.         }
  47.  
  48.         private void Form1_Load(object sender, EventArgs e)
  49.         {
  50.             comboBox1.SelectedIndex = 0;
  51.         }
  52.  
  53.     }
  54. }
  55.  
Nov 13 '08 #1
5 12791
Curtis Rutland
3,256 Expert 2GB
Try this.Focus();
That should bring the focus to the form rather than the control. I don't have time to test it out right now, but let us know if it works.
Nov 13 '08 #2
Plater
7,872 Expert 4TB
The combobox is probably the only control able to actually receive a focus transfer, so you can't remove focus from it by normal means.
If the this.focus() doesn't work, you can try a few other sneaky tricks.

Create a mock label that looks like a button (do not use a button, as it will then take the focus) use the onclick even of the label to disable/enable your combobox. If the combox is disabled, it cannot have the focus and focus will then be forced to another control (most likely the form itself)
You may have to tweek that idea a bit to get it to work.

I also think there is a setting somewhere that tells the form to *always* receive the keyboard events, regardless of if it has focus or not
Nov 13 '08 #3
The combobox is probably the only control able to actually receive a focus transfer, so you can't remove focus from it by normal means.
If the this.focus() doesn't work, you can try a few other sneaky tricks.

Create a mock label that looks like a button (do not use a button, as it will then take the focus) use the onclick even of the label to disable/enable your combobox. If the combox is disabled, it cannot have the focus and focus will then be forced to another control (most likely the form itself)
You may have to tweek that idea a bit to get it to work.

I also think there is a setting somewhere that tells the form to *always* receive the keyboard events, regardless of if it has focus or not
If I try

label1.Focus()

I can get the focus off of the ComboBox, but it still doesn't give my Form the ability to receive keyboard input again. Is there any way to actually give the focus back to a Form?

(And no, this.Focus() and this.Select() do not work.)

Thanks for your help.
Nov 13 '08 #4
Plater
7,872 Expert 4TB
Hmm if this.Focus() didn't work (assuming in context "this" was refering to the form) then I don't know of any other method besides the other 2 ways I mentioned
Nov 13 '08 #5
flagg
1
Try setting TabIndex, i.e.

this.comboBox1.TabIndex = 1;
this.label1.TabIndex = 2;
Mar 17 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by djefanten | last post: by
1 post views Thread by Ed Stewart | last post: by
reply views Thread by | last post: by
2 posts views Thread by mfleet1973 | last post: by
10 posts views Thread by Doug Bell | last post: by
reply views Thread by jereviscious | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.