By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

String search in ComboBox

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.