HI,
I have a list-box where items are loaded from database , when user types anything in text-box, list-box should search the starting characters entered and display it in List box.?
For example :
As soon as User types "A" in text-box ,then all the records starting from "A" should be displayed in List-box.
When user selects the record and press enter,that record should be entered in Gridview.
And also when user clicks the record from mouse,that record should be entered in Gridview.
I don't want to use text-box auto complete mode
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Nov 24, 2009, 7:18 PM EST
Here i coded what you want :)
please check "do you like this answer" check box if it helps you :)
List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
list.Clear();
foreach (String str in listBox1.Items)
{
list.Add(str);
}
}
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(txtSearch.Text.Trim()) == false)
{
listBox1.Items.Clear();
foreach (string str in list)
{
if (str.StartsWith(txtSearch.Text.Trim()))
{
listBox1.Items.Add(str);
}
}
}
else if(txtSearch.Text.Trim() == "")
{
listBox1.Items.Clear();
foreach (string str in list)
{
listBox1.Items.Add(str);
}
}
}
Lalit MRank #1,186 · 1.5K reputationPosted Nov 24, 2009, 7:50 PM EST
--------------------------------------
listBox2.ClearSelected(); searchString2 = textBox2.Text; listBox2.SelectionMode = SelectionMode.MultiExtended; int x = -1; if (searchString2.Length != 0) { do { x = listBox2.FindString(searchString2, x); if (x != -1) { if (listBox2.SelectedIndices.Count > 0) { if (x == listBox2.SelectedIndices[0]) return; } listBox2.SetSelected(x, true); } } while (x != -1); } Also Try this code --------- 1. Use DataTable as a datasource for the listbox,set valuemember (eg Id) and displaymeber property (eg Name) as well. 2. Store that datatable in your program as global variable. 2. Add a keypress event handler for textbox and in keypress event and in that handler filter global datatable using Select method eg. DataRow[] drArr=dtGlobal.Select("Name='" + txt.Text + "'"); DataTable dtNew = new DataTable(); for (int i = 0; iroy himanshu98 reputationPosted Nov 24, 2009, 9:47 PM EST
still now iam not getting out put.i entered a letter in text box but items are not displayed in list box.i have one doubt. how to bound the list box to data base.plz help me