PC Review


Reply
Thread Tools Rate Thread

Combo Box allow multiple characters

 
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      26th Sep 2005
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.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFyayBSLiBEYXdzb24=?=
Guest
Posts: n/a
 
      27th Sep 2005
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" wrote:

> 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.
>

 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      27th Sep 2005
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."

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBM?=
Guest
Posts: n/a
 
      27th Sep 2005
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" wrote:

> 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" wrote:
>
> > 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.
> >

 
Reply With Quote
 
=?Utf-8?B?TWFyayBSLiBEYXdzb24=?=
Guest
Posts: n/a
 
      27th Sep 2005
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" wrote:

> 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" wrote:
>
> > 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" wrote:
> >
> > > 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.
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does Autoexpand in combo box not work with accented characters? David Anderson Microsoft Access Forms 4 30th Nov 2007 11:04 PM
How many characters can be displayed in a combo box? =?Utf-8?B?Sm9obiBPc3dhbGQ=?= Microsoft Access Form Coding 4 2nd Aug 2007 09:02 PM
Combo box changin multiple bound combo boxes jamesfridman Microsoft Access Forms 0 10th Jun 2007 05:47 PM
Combo box maximum characters =?Utf-8?B?a29yeQ==?= Microsoft Access VBA Modules 0 1st Apr 2006 02:27 AM
In Excel find characters when multiple characters exist w/i a cel =?Utf-8?B?dGVhY2hlci1kZWJ1cmc=?= Microsoft Excel Worksheet Functions 1 5th Dec 2005 10:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:44 PM.