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. -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace WindowsApplication1
-
{
-
public partial class Form1 : Form
-
{
-
ComboBox comboBox1;
-
Label label1;
-
-
public Form1()
-
{
-
InitializeComponent();
-
-
comboBox1 = new ComboBox();
-
comboBox1.Items.Add("one");
-
comboBox1.Items.Add("two");
-
comboBox1.Items.Add("three");
-
comboBox1.Location = new Point(10, 12);
-
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
-
this.Controls.Add(comboBox1);
-
-
label1 = new Label();
-
label1.Location = new Point(139, 15);
-
label1.Text = "a";
-
this.Controls.Add(label1);
-
}
-
-
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
comboBox1.SelectNextControl(this, true, true, false, false);
-
this.Focus();
-
//this.SelectNextControl(this, true, true, false, false);
-
//comboBox1.SelectNextControl(this, true, true, false, false);
-
//this.Select();
-
}
-
-
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
-
{
-
label1.Text = e.KeyChar.ToString();
-
}
-
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
comboBox1.SelectedIndex = 0;
-
}
-
-
}
-
}
-
5 13424
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.
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
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.
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
Try setting TabIndex, i.e.
this.comboBox1.TabIndex = 1;
this.label1.TabIndex = 2;
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
| |