how to search listbox item using textbox in wpf [Resolved]

Posted by Sudhakar_A under WPF on 10/28/2013 | Points: 10 | Views : 19799 | Status : [Member] | Replies : 5
I have a listbox with three items named as

-sudhakar
-Amalraj
-Suresh

Above the listbox,I placed an textbox for search, whenever the user type in the textbox then the listbox should display only the entered text matching with item in listbox.

I mean if i type the letter "S" then the listbox should display only "sudhakar" and "Suresh". similar i fi type "Sud",the listbox should display only "sudhakar".

Please help me to sort out




Responses

Posted by: Bandi on: 10/28/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
The following code is working for me...
--Design Page

<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="82,11,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged"/>
<ListBox Height="100" HorizontalAlignment="Left" Margin="86,51,0,0" Name="listBox1" VerticalAlignment="Top" Width="600" >
<ListBoxItem>Sudhakar</ListBoxItem>
<ListBoxItem>Suresh</ListBoxItem>
<ListBoxItem>Chandu</ListBoxItem>
</ListBox>
</Grid>
</Window>


--Code behind
        List<string> list = new List<string>();
public MainWindow()
{
try
{
InitializeComponent();
}
catch (Exception exc)
{
MessageBox.Show("1. " + exc.Message);
}
try
{
//The rest of your constructor code
list.Clear();

foreach (ListBoxItem lbi in listBox1.Items)
{
//MessageBox.Show(lbi.Content.ToString());
list.Add(lbi.Content.ToString());
}

}
catch (Exception exc)
{
MessageBox.Show("2. " + exc.Message);
}
}

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text.Trim()) == false)
{
listBox1.Items.Clear();
foreach (string str in list)
{
if (str.StartsWith(textBox1.Text.Trim()))
{
listBox1.Items.Add(str);
}
}
}

else if (textBox1.Text.Trim() == "")
{
listBox1.Items.Clear();

foreach (string str in list)
{
listBox1.Items.Add(str);
}
}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/28/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://stackoverflow.com/questions/15627887/filter-listbox-items-with-a-textbox

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/28/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer
http://www.devcurry.com/2010/07/filter-data-in-wpf-listbox.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudhakar_A on: 10/29/2013 [Member] Starter | Points: 25

Up
0
Down
Thnk's a lot@chandu

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/29/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Welcome :)

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response