This seems like it would be a common problem, so hopefully I am just doing
something wrong. The attached demo code will help explain the problem.
I simply want to have a text box in which I can type a value that will
result in a ListBox being searched, and have the found item be selected in
the ListBox.
In the demo, I populate one listbox using DataBinding with a DataTable. In
the other, I populate manually via the Items collection. When data binding
is used, the SelectedIndex is manipulated in string ways. To see what I
mean, do the following:
1. start the app (click ok on the messagebox).
2. enter "2001" in the databinding related field.
3. now hit the backspace key to delete the "1" in "2001". notice the
SelectedIndexChanged event is fired twice.
This does not happen in the non-databound scenario. If you set some
breakpoints, and watch the call stack (with non-user code enabled), you'll
see what is happening (but I don't know why). I've also used Reflector to
review the ListBox code.
I know the CurrencyManager comes into place when using DataBinding, but I'm
still not sure what is happening.
Any insight would be greatly appreciated.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication4
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.ListBox listBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
LoadListBox1();
LoadListBox2();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(96, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.TextChanged += new
System.EventHandler(this.textBox1_TextChanged);
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(24, 64);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(96, 173);
this.listBox1.TabIndex = 1;
this.listBox1.SelectedIndexChanged += new
System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(168, 40);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 4;
this.textBox2.Text = "";
this.textBox2.TextChanged += new
System.EventHandler(this.textBox2_TextChanged);
//
// listBox2
//
this.listBox2.Location = new System.Drawing.Point(168, 64);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(104, 173);
this.listBox2.TabIndex = 5;
this.listBox2.SelectedIndexChanged += new
System.EventHandler(this.listBox2_SelectedIndexChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 6;
this.label1.Text = "DataBind";
//
// label2
//
this.label2.Location = new System.Drawing.Point(168, 8);
this.label2.Name = "label2";
this.label2.TabIndex = 7;
this.label2.Text = "Items Add";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 245);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox2);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void LoadListBox1()
{
DataTable dt = new DataTable();
dt.Columns.Add("year");
for(int i=2000; i<=2004; i++)
{
dt.Rows.Add(new object[] {i});
}
listBox1.DataSource = dt.DefaultView;
listBox1.DisplayMember = "year";
}
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
FindIt(listBox1, textBox1.Text);
}
private void LoadListBox2()
{
for(int i=2000; i<=2004; i++)
{
listBox2.Items.Add(i.ToString());
}
}
private void textBox2_TextChanged(object sender, System.EventArgs e)
{
FindIt(listBox2, textBox2.Text);
}
private void FindIt(ListBox b, string s)
{
int index = b.FindStringExact(s);
if (index != -1)
b.SetSelected(index, true);
else
b.ClearSelected();
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs
e)
{
MessageBox.Show("ListBox1 SelectedIndex Changed");
}
private void listBox2_SelectedIndexChanged(object sender, System.EventArgs
e)
{
MessageBox.Show("ListBox2 SelectedIndex Changed");
}
}
}