0
votes

say ive a listbox with items already present, say the items are

Ant
Adam    
Ball
Cat
Dog
Ear
Frog

above the listbox there is a textbox prompting the user to search

say if i enter in textbox ad then it must point to Adam if i remove a then it must point to Dog and must not use a button to search

also if i click on the item cat in the listbox, then next to the listbox there is a textbox it says Hi I'm Cat

I see that its not correct to use textbox to display Hi I'm Cat what shall i use to display info regarding the details of selected item.

Found solution myself

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int index = listBox1.FindString(textBox1.Text, -1);
        if (index != -1)
        {
            listBox1.SetSelected(index, true);
        }
    }
2
Found solution my self private void textBox1_TextChanged(object sender, EventArgs e) { int index = listBox1.FindString(textBox1.Text, -1); if (index != -1) { listBox1.SetSelected(index, true); } }Prasad MV

2 Answers

1
votes

you need to update your listbox on the "TextChanged" event of TextBox.

get the text written in the textbox inside this event, iterate over the items of listbox and do Text.Contains

protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
            var current = TextBox1.Text;
            foreach (ListItem item in ListBox1.Items)
            {
                if (item.Text.ToLower().Contains(current.ToLower()))
                    item.Selected = true;
            }
        }

this is how you can grab the selected item of listbox:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ListBox1.SelectedItem!=null)
            {
               Label1.Text =  ListBox1.SelectedItem.Text;
               //or you can dynamically create a label and add it to the page
               Label lbl = new Label();
               lbl.Text=ListBox1.SelectedItem.Text;
               MyContainer.Controls.Add(lbl);
               //where MyContainer is any server side container, HtmlContainerControl  
               //or HtmlControl 
            }
        }
0
votes

Does it have to be a listbox? If you use a ComboBox instead, you can use the AutoComplete feature. Set AutoCompleteMode to 'SuggestAppend' and AutoCompleteSource to 'ListItems' for example. A listbox does not have that functionality built in, so you would have to implement it yourself.

Also, for the label or textbox next to it to say, "Hi, I'm Cat".. or whatever.. use the SelectedIndexChanged event. In there you can do something like this:

myLabel.Text = "Hi, I'm " + myComboBox.SelectedItem.ToString()