473,806 Members | 2,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# ComboBox hijacks the focus

6 New Member
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 13573
Curtis Rutland
3,256 Recognized Expert Specialist
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 Recognized Expert Expert
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
latortugamorada
6 New Member
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 Recognized Expert Expert
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 New Member
Try setting TabIndex, i.e.

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

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

Similar topics

0
1382
by: djefanten | last post by:
Hi, I have a windows form with a combobox on it (it has the DropDown as DropDownStyle, which means you can type free text in it OR you can select from a list). At the moment that combobox receives focus, I need to display a small form on the side of the dropped down combobox with a keyboard simulated on it. Ok, I managed to get another form floating on the side, using a good article on vbaccelerator.com, I can get the input from the...
1
11365
by: Ed Stewart | last post by:
Hello. I'm creating an application that requires user input into a ComboBox, and I'd like to have the cursor appear in this emtpy ComboBox when the application starts. I had a plain TextBox, and the cursor started there automatically; to implement a history feature, I've moved to a ComboBox, but now the user needs to click on it before typing. How do I set focus on this item? I've tried adding a null string and calling comboBox.Focus()...
5
2851
by: jaYPee | last post by:
i have successfully added a combobox to my datagrid by setting their datasource from one of my table. here's my code... Dim grdColStyle6 As New DataGridComboBoxColumn() With grdColStyle6 .MappingName = "MajorID" 'must be from the grid table... .HeaderText = "Major" .Width = 120 .ColumnComboBox.DataSource =
2
2010
by: Xavier | last post by:
Hello, I'm working on VS 2003 with the framework 1.1 SP1. I'm trying to insert a combobox on a datagrid . If i use a combobox with the property dropdownstyle = dropdownlist it's OK but if I change this property to dropdown, when I try to activate my combobox (MyCombo.focus) the focus is on the next cell. Thanks for your time Regards
0
1482
by: | last post by:
Hi All. I have a problem with combobox, what I want is when combobox gets focus I need it to show the dropdown list t.This is fine if the user selects the combobox via keystrokes but when the combobox is selected via mouse click it draws the dropdown list twice. As I am trapping the enter event, I can see thats is called as well some other ? the style is set to dropdownlist as they must select one from the list I assume I must trap a...
2
3140
by: mfleet1973 | last post by:
Hello (again), Within my datagrid I add a control to the datagridtextboxcolumn when the underlying textbox receives focus as follows: Combobox1.Size = New Size(Datagrid1.GetCurrentCellBounds().Width + 3, combobox1.Size.Height) sender.Controls.Add(Combobox1) This works great. The problem is that when focus is set on the
10
2040
by: Doug Bell | last post by:
Hi I am still having problems with Tabbing through a DataGrid with a DataGridComboBox Column. I need to allow the User to Type the value into the ComboBox so consequently its ComboBoxStyle is set to DropDown. This causes the Tabbing to work incorrectly. Even though I am consuming the Windows Message WM_KEYUP, it still Tabs through the ComboBox Column on to the next Column. I found if I do not give the keyboard focus ie remark out...
4
3921
by: mkaszub | last post by:
Hi, I have a problem with combobox. On my form is many comboboxes and when I select same option and use my mouse roll, combobox change to next value. How to disable mouse roller? I tried to change focus on another combo but it isn't good solution because you again cannot scroll page but change option in combobox. I also tried to change focus to parent -> combobox.parent.focus() but if combo is on bottom of the page view is centered and I...
0
1145
by: jereviscious | last post by:
Hi all I've added a combobox to the datagrid control using the following method dataGridTextBoxColumn7.TextBox.Controls.Add(comboControl); The problem is that when I tab to the Column that contains the combobox, the focus is on the underlying textbox in the datagrid.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10369
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.