473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.Collections;
using System.ComponentModel;
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 sAccumulatedChars;
private int iFoundIndex = 0;
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

//
// 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.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// 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.KeyEventHandler(this.comboBox 1_KeyDown);
this.comboBox1.KeyUp += new
System.Windows.Forms.KeyEventHandler(this.comboBox 1_KeyUp);
this.comboBox1.Leave += new System.EventHandler(this.comboBox1_Leave);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 80);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(232, 20);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 152);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Exit";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 189);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1,
this.comboBox1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void comboBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
// alpha, space
if((e.KeyValue > 33 && e.KeyValue < 123) || e.KeyValue == 32)
{
sAccumulatedChars += Convert.ToChar(e.KeyValue);
iFoundIndex = comboBox1.FindString(sAccumulatedChars, 0);
}
else if(e.KeyValue == 8) // backspace
{
// if sAccumulatedChar is empty, select 1st item
if (sAccumulatedChars.Length == 0)
{
iFoundIndex = 0;
}
else
{
// remove last char of accumulated string
sAccumulatedChars = sAccumulatedChars.Substring(0,
sAccumulatedChars.Length - 1);
iFoundIndex = comboBox1.FindString(sAccumulatedChars, 0);
}
}
if(e.KeyValue == 13)
{
comboBox1.SelectedIndex = iFoundIndex;
textBox1.Focus();
}
if(iFoundIndex >= 0)
{
comboBox1.SelectedIndex = iFoundIndex;
selectText();
}
}

private void comboBox1_Leave(object sender, System.EventArgs e)
{
if (comboBox1.SelectedIndex >= 0)
{
textBox1.Text = comboBox1.SelectedItem.ToString();
comboBox1.SelectionStart = 0;
sAccumulatedChars = "";
}
}

private void selectText() {
if(sAccumulatedChars.Length > 0)
{
comboBox1.SelectionStart = sAccumulatedChars.Length;
}
else
{
comboBox1.SelectionStart = 0;
}
comboBox1.SelectionLength = comboBox1.Text.Length -
sAccumulatedChars.Length;
}

private void button1_Click(object sender, System.EventArgs e)
{
Application.Exit();
}

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


Nov 13 '05 #1
0 12211

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

Similar topics

4
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...
2
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...
3
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...
3
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...
5
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...
1
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...
15
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...
14
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
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.