Here is an example to find the value in ListBox according to the text of Text box.
means when you type any character or word in textbox the matched value will be select in list box-
this 4 lines of code will remove your all headache to apply the incremental search option on listbox-
on TextBox's TextChangedEvent code like this-
private void textBox1_TextChanged(object sender, EventArgs e)
{
int index = listBox1.FindString(this.textBox1.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}
}
Monday, June 14, 2010
Subscribe to:
Post Comments (Atom)
Thanks a lot, spared me a lot of time!
ReplyDeleteIt Works.....Thank you so much
ReplyDeleteThis code working fine in windows form... plz i want the code for this problem in windows phone 7 c#..
ReplyDeleteon TextBox's TextChangedEvent code like this-
private void textBox1_TextChanged(object sender, EventArgs e)
{
int index = listBox1.FindString(this.textBox1.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}
}
This code is working fine in windows form.. plz, i want the code in c# windows phone 7 for this same problem..
ReplyDeleteon TextBox's TextChangedEvent code like this-
private void textBox1_TextChanged(object sender, EventArgs e)
{
int index = listBox1.FindString(this.textBox1.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}
}
Hi if you are working on the mobile application development platform for window 7. Then please make sure that you are binding the control listbox through observablecollecton. Apply filter on the observablecollection through linq ...as following
ReplyDeleteprivate void textBox1_TextChanged(object sender, EventArgs e)
{
int index = observablecollection = observablecollection.where(obcoltypeobj=>obcoltypeobj.fielname.startswith(this.textBox1.Text)).ToList();
}
now the above will show the filtered data in the listbox... hope you are well aware of the linq to collections.
pincode lookup:-
ReplyDeleteusing System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Xml;
namespace WindowsPhoneApplication1
{
public partial class Chennai : PhoneApplicationPage
{
public Chennai()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
XElement ob = XElement.Load("Chennai.xml");
var p = from temp in ob.Descendants("Pincode")
select new Pincode
{
PropArea = (string)temp.Element("Area"),
PropCode = (int)temp.Element("Code"),
};
lst_area.ItemsSource = p;
}
private void lst_area_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Pincode ob = (Pincode)lst_area.SelectedItem;
tb_pincode.Text = ob.PropCode.ToString();
}
private void txt_area_TextChanged(object sender, TextChangedEventArgs e)
{
//int index = observablecollection = observablecollection.where(obcoltypeobj => obcoltypeobj.fielname.startswith(this.txt_area.Text)).ToList();
}
}
}
what code should I write.. I dont knw.. please, solve this problem and help me to complete this incremental search of textbox.. I hope u will help.. windows phone 7 c#
windows phone 7 this coding gives error.. how to resolve this.
ReplyDeleteusing System;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
listBox1.Items.AddRange(new object[] { "abc", "abd", "ac", "baa", "bab", "bb" });
textBox1.TextChanged += SearchListbox;
}
void SearchListbox(object sender, EventArgs e) {
int len = textBox1.Text.Length;
foreach (string item in listBox1.Items)
if (len <= item.Length && item.Substring(0, len) == textBox1.Text) {
listBox1.SelectedItem = item;
return;
}
listBox1.SelectedIndex = -1;
}
}
}
which kind of error ??
DeleteHi..
ReplyDeleteI know how to bind the listbox with xml in windows phone 7..
similar to that how can i bind the autocompletebox with xml..
I want full sample code for that..
plz...
please share me the code ..
DeleteThanks this code works fine...
ReplyDeleteThis piece of code is very smart and working very fine. Thanks.
ReplyDeleteDickson
Hi,
ReplyDeleteI have a page which displays a listbox of Objects.There is a search page which should display the objects only if they contain the textbox string. How to do it.
Here is the xaml code
For Textbox
For Listbox
Then in C# side
private void searchText_TextChanged(object sender, TextChangedEventArgs e)
{
// How to search the entered textstring in the listbox and display the listbox objects only if the search string exist
}
good information of search box really nice thank you..
ReplyDeleteI cant get listbox1."FindString" method
ReplyDeleteCant find FindString method in listbox. Pl. Help.
ReplyDeleteExactly what I needed!
ReplyDeleteThanks.