Combo Box allow multiple characters

G

Guest

Currently, when the user presses a letter key the combo box will go to the
first item in the collection of the combo box starting with that letter. For
example user presses on "R", and "R2F" appears in the combo box, then the
user presses on "H", "HIF" appears.

I want the user to be able to enter 2 or 3 characters and the combo box will
step through as the user enters the letters. For example user presses on
"R", "R2F" appears, then the user presses "H", "RH" appears.
 
G

Guest

Hi Mike,
I would inherit from the standard combo box and add the functionality like
in the code below:

using System;
//using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
class SearchableComboBox : ComboBox
{
private string _searchString;

public SearchableComboBox()
: base()
{
_searchString = "";
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
_searchString += e.KeyChar.ToString();
this.SelectBestMatch();
}

public void ResetSearchText()
{
_searchString = "";
}

private void SelectBestMatch()
{
for(int index=0; index<this.Items.Count; index++)
{
string currentItem = (string)this.Items[index];

if (currentItem.StartsWith(_searchString))
{
this.SelectedIndex = index;
return;
}
}
}
}
}


Hope that helps
Mark R Dawson
http://www.markdawson.org
 
K

Kevin Yu [MSFT]

Hi Cadel,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need the ComboBox to search in the
collection automatically for the item you have typed, just as the IE
address bar does. If there is any misunderstanding, please feel free to let
me know.

As far as I know, this is not supported by the .NET framework. IMO, you can
try to sub class the ComboBox and handle the KeyPress event to search
through the collection. There are a lot of people trying to do this. I
suggest you also search through google groups, and there might be solution.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

I copied your code in but no change in the combo box. I typed in "RH", show
"R2F", then I typed in "H", shows "HHH". I want to type in R then H and see
"RH".

I modifed
//private System.Windows.Forms.ComboBox cboPrivilege;
private SearchableComboBox cboPrivilege;

also
//this.cboPrivilege = new System.Windows.Forms.ComboBox();
this.cboPrivilege = new SearchableComboBox();

I also debugged the code, stepping through I noticed _searchString does not
get cleared out when focus leaves combo box.



Mark R. Dawson said:
Hi Mike,
I would inherit from the standard combo box and add the functionality like
in the code below:

using System;
//using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
class SearchableComboBox : ComboBox
{
private string _searchString;

public SearchableComboBox()
: base()
{
_searchString = "";
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
_searchString += e.KeyChar.ToString();
this.SelectBestMatch();
}

public void ResetSearchText()
{
_searchString = "";
}

private void SelectBestMatch()
{
for(int index=0; index<this.Items.Count; index++)
{
string currentItem = (string)this.Items[index];

if (currentItem.StartsWith(_searchString))
{
this.SelectedIndex = index;
return;
}
}
}
}
}


Hope that helps
Mark R Dawson
http://www.markdawson.org



Mike L said:
Currently, when the user presses a letter key the combo box will go to the
first item in the collection of the combo box starting with that letter. For
example user presses on "R", and "R2F" appears in the combo box, then the
user presses on "H", "HIF" appears.

I want the user to be able to enter 2 or 3 characters and the combo box will
step through as the user enters the letters. For example user presses on
"R", "R2F" appears, then the user presses "H", "RH" appears.
 
G

Guest

Hi Mike L,
I did not automatically reset the text on lost focus, you will notice
there is a method called ResetSearchText, I just provided you with some
proof of concept code, the polishing is up to you :)

I tried this code on my computer and it works fine, I am not sure why it
is not working for you, you need to like you did replace all ComboBox
references inside the System.Windows.Forms namespace with the new class, that
should be enough.


Mark R Dawson
http://www.markdawson.org



Mike L said:
I copied your code in but no change in the combo box. I typed in "RH", show
"R2F", then I typed in "H", shows "HHH". I want to type in R then H and see
"RH".

I modifed
//private System.Windows.Forms.ComboBox cboPrivilege;
private SearchableComboBox cboPrivilege;

also
//this.cboPrivilege = new System.Windows.Forms.ComboBox();
this.cboPrivilege = new SearchableComboBox();

I also debugged the code, stepping through I noticed _searchString does not
get cleared out when focus leaves combo box.



Mark R. Dawson said:
Hi Mike,
I would inherit from the standard combo box and add the functionality like
in the code below:

using System;
//using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
class SearchableComboBox : ComboBox
{
private string _searchString;

public SearchableComboBox()
: base()
{
_searchString = "";
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
_searchString += e.KeyChar.ToString();
this.SelectBestMatch();
}

public void ResetSearchText()
{
_searchString = "";
}

private void SelectBestMatch()
{
for(int index=0; index<this.Items.Count; index++)
{
string currentItem = (string)this.Items[index];

if (currentItem.StartsWith(_searchString))
{
this.SelectedIndex = index;
return;
}
}
}
}
}


Hope that helps
Mark R Dawson
http://www.markdawson.org



Mike L said:
Currently, when the user presses a letter key the combo box will go to the
first item in the collection of the combo box starting with that letter. For
example user presses on "R", and "R2F" appears in the combo box, then the
user presses on "H", "HIF" appears.

I want the user to be able to enter 2 or 3 characters and the combo box will
step through as the user enters the letters. For example user presses on
"R", "R2F" appears, then the user presses "H", "RH" appears.
 
Joined
Oct 15, 2012
Messages
1
Reaction score
0
Just go to ComboBox properties and set the following things,
1.AutoCompleteSource = ListItems
2.AutoCompleteMode = Append

:bow:
 

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