473,757 Members | 8,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String search in ComboBox

Hi All,

Any suggestions appreciated.
I am trying to do a string search through a comboBox list,
have created a simple test which is listed below. The problem
I am having is, every time a key is pressed it echoes the char
back to the combo twice. For example, if I type D in the combo
I get back Dd. The following app works OK if I use KeyPress
instead of KeyDown/KeyUp on my computer, but if I run it on
another computer (with .NET framework, without Visual Studio)
it does the doubling up thing as well.

?
Thx Regards
Steve
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace KeyTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.ComboBox comboBox1;
private System.Windows. Forms.TextBox textBox1;
private string sAccumulatedCha rs;
private int iFoundIndex = 0;
private System.Windows. Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.comboBox1 = new System.Windows. Forms.ComboBox( );
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// comboBox1
//
this.comboBox1. Items.AddRange( new object[] {
"Joe Bloggs",
"Fred Flintstone",
"Barney Rubble",
"George Jetson",
"Sylvester" ,
"Tom & Jerry",
"Donald Duck",
"Mickey Mouse",
"Goofy",
"Bugs Bunny"});
this.comboBox1. Location = new System.Drawing. Point(8, 8);
this.comboBox1. Name = "comboBox1" ;
this.comboBox1. Size = new System.Drawing. Size(232, 21);
this.comboBox1. TabIndex = 0;
this.comboBox1. Text = "Joe Bloggs";
this.comboBox1. KeyDown += new
System.Windows. Forms.KeyEventH andler(this.com boBox1_KeyDown) ;
this.comboBox1. KeyUp += new
System.Windows. Forms.KeyEventH andler(this.com boBox1_KeyUp);
this.comboBox1. Leave += new System.EventHan dler(this.combo Box1_Leave);
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(8, 80);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(232, 20);
this.textBox1.T abIndex = 1;
this.textBox1.T ext = "";
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(80, 152);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 2;
this.button1.Te xt = "Exit";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(248, 189);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.button1,
this.textBox1,
this.comboBox1} );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}
#endregion

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

private void comboBox1_KeyDo wn(object sender,
System.Windows. Forms.KeyEventA rgs e)
{
// alpha, space
if((e.KeyValue > 33 && e.KeyValue < 123) || e.KeyValue == 32)
{
sAccumulatedCha rs += Convert.ToChar( e.KeyValue);
iFoundIndex = comboBox1.FindS tring(sAccumula tedChars, 0);
}
else if(e.KeyValue == 8) // backspace
{
// if sAccumulatedCha r is empty, select 1st item
if (sAccumulatedCh ars.Length == 0)
{
iFoundIndex = 0;
}
else
{
// remove last char of accumulated string
sAccumulatedCha rs = sAccumulatedCha rs.Substring(0,
sAccumulatedCha rs.Length - 1);
iFoundIndex = comboBox1.FindS tring(sAccumula tedChars, 0);
}
}
if(e.KeyValue == 13)
{
comboBox1.Selec tedIndex = iFoundIndex;
textBox1.Focus( );
}
if(iFoundIndex >= 0)
{
comboBox1.Selec tedIndex = iFoundIndex;
selectText();
}
}

private void comboBox1_Leave (object sender, System.EventArg s e)
{
if (comboBox1.Sele ctedIndex >= 0)
{
textBox1.Text = comboBox1.Selec tedItem.ToStrin g();
comboBox1.Selec tionStart = 0;
sAccumulatedCha rs = "";
}
}

private void selectText() {
if(sAccumulated Chars.Length > 0)
{
comboBox1.Selec tionStart = sAccumulatedCha rs.Length;
}
else
{
comboBox1.Selec tionStart = 0;
}
comboBox1.Selec tionLength = comboBox1.Text. Length -
sAccumulatedCha rs.Length;
}

private void button1_Click(o bject sender, System.EventArg s e)
{
Application.Exi t();
}

private void comboBox1_KeyUp (object sender,
System.Windows. Forms.KeyEventA rgs e)
{
e.Handled = true;
}
}
}


Nov 13 '05 #1
0 12285

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3692
by: Lauren Wilson | last post by:
A2K I have a combo box that shows the content of a last name field from a table. I use it to locate records in the table form. I can open the form and locate several names quickly by placing the cursor inside the drop-down list and typing the first few letters of the name I want to find. Then, for no apparent reason, the list just stops responding to subsequent searches for names that I KNOW are in the list. The list contains no...
2
12884
by: John Tyce | last post by:
When a button is clicked, a date is inserted or added into a combo box like this : ComboBox.Items.Add(string) or ComboBox.Items.Insert(0,string); Either way, the new string does not show up in the ComboBox. I get no errors or problems, it just will not work. At load time I am retrieving dates from an Oracle database and adding them to the ComboBox with out any problems. However, once the application is up and running I cannot add to the...
3
1396
by: Marina | last post by:
I would like to create a search for that is based on the CaseFiles query and have no idea how to go about this. I would like the user to be able to search by CaseNumber, FirstName, LastName, or ClientID. Any help would be much appreciated. Marina
3
2207
by: Elainie | last post by:
I would like to search a form with many fields on it, with out using the search facility through access. Througth a drop down list if possible.... How would I go about this? How could I also create this from a top bar menu option too...? Elaine
5
1966
by: Deano | last post by:
Perhaps this has been asked before but there might be some up to date thinking about this. I really need a better search function for my asset register. I allow assets to be entered and tracked over a period of time. There can be quite similar assets and simply choosing from the combo box is not enough. I need something more sophisticated. How hard is it to get a listing that grows as I type in a search term, character by...
1
6801
by: sathyan8294 | last post by:
i am using dotnet 2003.i am doing my project in vb.net windows application.i have two form in my project.first form is datagrid .In second form label,textboxes and combobox are avilable to save the entering values in datagrid of first form through this 2nd form.my problem is,i want to search a value using combobox. for example, in first form, there are 4 columns named as name,age,rollno,address in datagrid. values are ramu for namecolumn,24...
15
2607
by: Academia | last post by:
I want a string of characters that will sort after all strings of Roman letters. Something that will display using a font like the one this note is written with. Is there such a thing? I tried using a string starting with { because in ASCII it comes after the letters but that sorts before the letters.
14
5785
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
Is there any known implementation of search as you type (in a db) in c#. Say for argument sake, as I type in the 1st name the number of names in my combobox decrease. -- L. A. Jones
0
1874
by: Brandon | last post by:
Hi there... I got a WPF project that I am trying to select a ComboBoxItem in a ComboBox based on a string from the selected ListView item in the project... This ComboBox is unbound now and the ListView is grouped(Group section headings are the strings that populate the ComboBox Items) and bound to a XML provider. Later I will bound the ComboBox to the same XML provider as the ListView...
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.