Combobox Highlighting Issues

K

kcitrenbaum

If I have a Windows.Forms.ComboBox on a form and set the Text property
to a value that's in the Items list, the ComboBox appears highlighted
until the combobox losses focus through User action. Setting focus to
the combobox and then to another control in code doesn't fix this
problem.
What's up with this? It's quite annoying.
 
S

Sergey Poberezovskiy

did not quite get what you want - to see it highlighted
or not?

You can always use SelectionStart, SelectionLength
properties to control the selection.

HTH
 
M

Morten Wennevik

If your ComboBox is the first control on the form it will get Focus and
all text will be autoselected. You can remove the autoselection by
handling the GotFocus event

private void cb_GotFocus(object sender, EventArgs e)
{
cb.SelectionStart = cb.Text.Length;
}

However, this may confuse the user as autoselection in a ComboBox is
expected behaviour.

It this is not what you mean, please explain in some more detail :)
 
K

kcitrenbaum

I want them NOT highlighted, and the SelectionStart, SelectionLength
properties do not fix this problem
 
K

kcitrenbaum

It's not the first control on the form, it's every combobox on the
form. Am I the only one who has this issue?

Here's some code to demonstrate the problem:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ComboBoxTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox cmb1;
private System.Windows.Forms.ComboBox cmb2;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
cmb1.SelectedIndex = 0;
cmb2.Text = "itemnull";
textBox1.Focus();
//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmb1 = new System.Windows.Forms.ComboBox();
this.cmb2 = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// cmb1
//
this.cmb1.Dock = System.Windows.Forms.DockStyle.Top;
this.cmb1.Items.AddRange(new object[] {
"item1",
"item2",
"item3"});
this.cmb1.Location = new System.Drawing.Point(0, 22);
this.cmb1.Name = "cmb1";
this.cmb1.Size = new System.Drawing.Size(402, 24);
this.cmb1.TabIndex = 0;
this.cmb1.Text = "comboBox1";
//
// cmb2
//
this.cmb2.Dock = System.Windows.Forms.DockStyle.Top;
this.cmb2.Items.AddRange(new object[] {
"item1",
"item2",
"item3"});
this.cmb2.Location = new System.Drawing.Point(0, 46);
this.cmb2.Name = "cmb2";
this.cmb2.Size = new System.Drawing.Size(402, 24);
this.cmb2.TabIndex = 1;
this.cmb2.Text = "comboBox2";
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(402, 22);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(402, 358);
this.Controls.Add(this.cmb2);
this.Controls.Add(this.cmb1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}


Notice that when you open the form, the textbox is focused, but cmb1 is
also highlighted . cmb2 is not highlighted because the text isn't in
the item list. If you set cmb2 text to an item in the Items list, it
too will be highlighted
 
K

kcitrenbaum

Opps, set the TabOrder incorrectly in the example, but even with the
correct tab order the problem occurs
 
M

Morten Wennevik

I'm sorry, but for me all works as expected.

Setting TabOrder to 0 for textBox1, 1 for cmb1, and 2 for cmb2 causes the
TextBox to have focus with its text selected when the Form is displayed,
despite whatever text is set in either of the ComboBoxes.

The code seem to indicate that you have added the controls in the order of
cmb1, cmb2, textBox1 so unless you respecify TabOrder (in the designer or
inside InitializeComponent) you will see cmb1 focused.

For reference I tested on Framework 1.1 (non service packed) under Win98SE
and compiled manually with csc.exe.
 
K

kcitrenbaum

It's not a TabOrdering thing. If in the constructor I change
cmb1.SelectedIndex = 0;
cmb2.Text = "itemnull";

to

cmb2.SelectedIndex = 0;
cmb1.Text = "itemnull";

cmb2 becomes highlighted. textBox1 is also highlighted because it's
the control that actually has focus. I have no idea what's going on
and it's driving me nuts

I'm running Framework 1.1 on Xp SP2 but this problem occurs under 2000
as well.
 
M

Morten Wennevik

I'm afraid I have no idea what is causing this problem.
Some steps that may help you locate the problem is to

1) Determine that this problem is not unique to a certain project.
Try creating a whole new project where you do the same (not copy/paste)
If this works I suspect one of the project files has gone corrupt

2) Try compiling manually.
If this works and 1) does not, you may want to reinstall Visual Studio

3) Try compiling on another machine
If this works and 1) and 2) does not, you might want to reinstall .net

Good luck!
 
K

kcitrenbaum

I've had this on multiple projects, on multiple machines, with multiple
versions of visual studio. Everyone here in my office has the same
issue. I can't believe we're the only people having this issue.
 
M

Morten Wennevik

Maybe some unforseen Operating System/Language combo or something that all
of your machine has interferes with .net in some way? I have absolutely
no idea what or why, but I seem to remember someone having an issue with
certain programs in certain language version of an operating system.
 
N

n_none

Ok, seem to have found the problem. The Combobox's text must not be
set before the combobox is first visible. You must show the form
before the setting the text. I do believe this is a bug in the
framework.
 
N

n_none

I want it to act like a normal combobox. Select from the list OR type
whatever they want. My current workaround is to override the
OnVisibleChanged function and set the SelectionStart and
SelectionLength manually.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top