ComboBox Control

L

lochuynh

Hi,
I have a comboBox with DropDowStyle is DropDown. In comboBox
contains like
THU1
THU2
ONE1
ONE2
When I type in comboBox a text is THU, after that i click on
button to shows list of items in ComboBox. Suddenly item thu which I typed
in the first time is changed to item THU1 which exits in comboBox at
Initialize.

Can you show me methods to fix this bug.

Many thanks and best regards
Loc Huynh,
 
M

Morten Wennevik

Hi Loc Huynh,

I'm not sure how to disable this feature. The problem is that if it finds a match beginning with what you have entered in the text box it will select that match.

This hack will give you what you want, but you need to strip away the newline character when you retrieve the combobox value for use elsewhere. It works by ensuring that a match isn't found by inserting a newline character.

// this event will fire when you open the dropdown list
private void comboBox1_DropDown(object sender, System.EventArgs e)
{
// if the first character in the textbox part is not a newline
if(comboBox1.Text[0] != '\n')
{
// add a newline character. This will be invisible to the user
comboBox1.Text = '\n' + comboBox1.Text;
// set cursor to the end, since changing the text resets the cursor
comboBox1.SelectionStart = comboBox1.Text.Length;
}
}


Happy coding!
Morten Wennevik [C# MVP]
 
M

Morten Wennevik

Oh yeah, and you would want to return if comboBox1.Text.Length == 0 or using comboBox1.Text[0] will cause an exception.

Happy coding!
Morten Wennevik [C# MVP]
 
A

Andrew Bingham

Unless I have emisunderstood you this is not a bug but the way (or at least
one of the ways) a combobox works ?

Look up "ComboBoxStyle Enumeration" in the .NET help file

cheers

Andrew

--
****************************************************************************
andrewbingham.com

tel 01223 514674 (Cambridge)
mobile 07970 161057
fax 07970 601283
email (e-mail address removed)

DISCLAIMER, PLEASE NOTE:
This communication is for the attention of the named recipient only
The content should not be passed on to any other person.
It is sent in good faith, in confidence, and without legal responsibility.

VIRUS CHECK
Emails and attachments are virus checked using Norton® AntiVirus®
2002 which is regularly updated. However it remains the recipients
responsibility to check emails and attachments sent, or forwarded,
from andrewbingham.com for viruses and macro viruses
****************************************************************************
 

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