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.
Code:
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;
}
}
}
Comment