جستجو در ListBox
یک برنامه ویندوزی ایجاد کرده و یک کنترل textBox و یک کنترل ListBox بر روی فرم قرار دهید. در خاصیت Load فرم و خاصیت textchanged کنترل textbox کدهای زیر را بنویسید :
using System; using System.Collections.Generic; using System.Windows.Forms; namespace SearchinListbox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Listitems = new List<string>(); private void Form1_Load(object sender, EventArgs e) { items.AddRange(new string[] { "Younes", "Yahya", "Soroush", "Siavash" }); foreach (string str in items) { listBox1.Items.Add(str); } } private void textBox1_TextChanged(object sender, EventArgs e) { listBox1.Items.Clear(); foreach (string str in items) { if (str.StartsWith(textBox1.Text, StringComparison.CurrentCultureIgnoreCase)) { listBox1.Items.Add(str); } } } } }
برنامه را اجرا و با نوشتن مثلا ya در داخل جعبه متن، نتیجه را مشاهده کنید.