ComboBox - Searching the List Box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having trouble using the ComboBox in my app. I am attempting to use the
text from the ComboBox, to search the list box portion. However: In my
"TextChanged" event: I cannot get the value of the entered text. Below is
my code.

private void cbEdit_TextChanged(object sender, System.EventArgs e)
{
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.SelectionStart;
 
If your cbEdit Combobox does not have its DropDownStyle property set to
DropDownList, it should works.
but if your cbEdit Combobox has its DropDownStyle property set to
DropDownList, no TextChanged event would be fired.
Use the following lines instead :

private void cbEdit_SelectedIndexChanged(object sender, System.EventArgs e)
{
int iIndex = cbEdit.SelectedIndex;
string sText = cbEdit.Items[iIndex];
 
Ludovic:

Thank you for your response: but I am trying to get the value of the
entered text, not the value that has been selected. I want to capture the
value of the text when a user types in a value, not selects it from the drop
down list.

Thanks
Andy

Ludovic SOEUR said:
If your cbEdit Combobox does not have its DropDownStyle property set to
DropDownList, it should works.
but if your cbEdit Combobox has its DropDownStyle property set to
DropDownList, no TextChanged event would be fired.
Use the following lines instead :

private void cbEdit_SelectedIndexChanged(object sender, System.EventArgs e)
{
int iIndex = cbEdit.SelectedIndex;
string sText = cbEdit.Items[iIndex];
.
.
.
}

Hope it helps,

Ludovic SOEUR.


Andy said:
I am having trouble using the ComboBox in my app. I am attempting to use the
text from the ComboBox, to search the list box portion. However: In my
"TextChanged" event: I cannot get the value of the entered text. Below is
my code.

private void cbEdit_TextChanged(object sender, System.EventArgs e)
{
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.SelectionStart;
.
.
.
}

my sText value is always "", even when I enter a value.

Any help would be appreciated
 
I tried the following lines and it works perfectly. Maybe you should provide
your source code to see what's wrong.

using System;
public class Form1 : System.Windows.Forms.Form {
static void Main() {System.Windows.Forms.Application.Run(new Form1());}
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox1;
public Form1() {
comboBox1 = new System.Windows.Forms.ComboBox();
comboBox1.Items.AddRange(new object[] {"abc","def","ghi","jkl"});
comboBox1.Location = new System.Drawing.Point(0, 0);
comboBox1.Size = new System.Drawing.Size(300,20);
comboBox1.TextChanged += new
System.EventHandler(this.comboBox1_TextChanged);
Controls.Add(this.comboBox1);
textBox1 = new System.Windows.Forms.TextBox();
textBox1.Location = new System.Drawing.Point(0, 30);
textBox1.Size = new System.Drawing.Size(300, 20);
Controls.Add(this.textBox1);
ClientSize = new System.Drawing.Size(320, 60);
}
private void comboBox1_TextChanged(object sender, System.EventArgs e) {
textBox1.Text=comboBox1.Text;
}
}

Hope it helps,

Ludovic SOEUR.
 
Ludovic:

Here is my code. I declared my combo box using the GUI as cbEdit. I have
the dropdownstyle set to DropDown. The sText Variable is always "", even if
I enter a value in the ComboBox textbox on the Windows Form.

Here is my event handler declaration for the text changed event:
this.cbEdit.TextChanged += new System.EventHandler(this.cbEdit_TextChanged);

Here is my Method:

private void cbEdit_TextChanged(object sender, System.EventArgs e)
{
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.SelectionStart;
if(iPosition != 0)
{
bFound = false;
for(int i = 0; i < cbEdit.Items.Count; i++)
{
if(cbEdit.Items.ToString().Substring(0, iPosition).ToUpper()
==
sText.Substring(0, iPosition).ToUpper())
{
cbEdit.Text = cbEdit.Items.ToString();
cbEdit.SelectionStart = iPosition;
bFound = true;
break;
}
}
if(!bFound)
{
cbEdit.Text = sText.Substring(0, iPosition) +
sText.Substring(iPosition + 1);
cbEdit.SelectionStart = iPosition - 1;
}
}
}
 
There was a little mistake in your source code.
You attempt to modify Text attribute while TextChanged event is being
working...so you fire an other TextChanged event.You just have to add a var
(isTextChanging for example), set it to true at the beginning of the event
and false at the end, and to exit the function is this var is set to true.
You will be sure that the event is fired only once.

As I don't know you code, I created a new project with only one combobox.
Everything works properly. But there a bug in your source code (you use
Substring without checking if it's possible). In the example below, the
application crashes when it happens. If your application is multithreaded or
the combobox is being shown when dragging an element, no exception would be
thrown and that might explain why your Text attribute stays empty.

Have a look to the following lines that work on my computer :

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.ComboBox cbEdit;
public Form1() {
InitializeComponent();
}
#region Windows Form Designer generated code
private void InitializeComponent() {
this.cbEdit = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// cbEdit
//
this.cbEdit.Items.AddRange(new object[] {"abc","def","ghi","jkl"});
this.cbEdit.Location = new System.Drawing.Point(8, 16);
this.cbEdit.Name = "cbEdit";
this.cbEdit.Size = new System.Drawing.Size(256, 21);
this.cbEdit.TabIndex = 0;
this.cbEdit.Text = "comboBox1";
this.cbEdit.TextChanged += new
System.EventHandler(this.cbEdit_TextChanged);
//
// Form4
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.cbEdit);
this.Name = "Form4";
this.Text = "Form4";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main() {
Application.Run(new Form1());
}
bool isTextChanging=false;
private void cbEdit_TextChanged(object sender, System.EventArgs e) {
if (isTextChanging) return; //No recursion
isTextChanging=true;
bool bFound = false;
string sText = cbEdit.Text;
int iPosition = cbEdit.SelectionStart;
if(iPosition != 0) {
bFound = false;
for(int i = 0; i < cbEdit.Items.Count; i++) {
if(cbEdit.Items.ToString().Substring(0,
iPosition).ToUpper() ==
sText.Substring(0, iPosition).ToUpper()) {
cbEdit.Text = cbEdit.Items.ToString();
cbEdit.SelectionStart = iPosition;
bFound = true;
break;
}
}
if(!bFound) {
cbEdit.Text = sText.Substring(0, iPosition) +
sText.Substring(iPosition + 1);
cbEdit.SelectionStart = iPosition - 1;
}
}
isTextChanging=false;
}
}

Hope it helps,
Ludovic SOEUR.
 
Back
Top