- Joined
- Mar 25, 2013
- Posts
- 4,237
- Solutions
- 23
- Reaction
- 10,687
- Points
- 2,301
Here's the function that we are going to use:
First of all, take note yung functions na gagamitin natin nasa ibaba:
First:
//declare this as global variable
List<string> allItems = new List<string>();
LOAD ITEMS:
METHOD 1:
HERE'S THE TEXTBOX SEARCH:
//REFER TO SEARCH ITEMS FUNCTION 2
METHOD 2:
TEXTBOX SEARCH:
//REFER TO SEARCH ITEMS FUNCTION 1
FUNCTIONS:
LOAD ITEMS TO LISTBOX FROM FILE DIALOG:
ADD ITEMS TO LISTBOX
SEARCH ITEMS FUNCTION 1
SEARCH ITEM FUNCTION 2:
First of all, take note yung functions na gagamitin natin nasa ibaba:
First:
//declare this as global variable
List<string> allItems = new List<string>();
LOAD ITEMS:
C#:
private void button1_Click(object sender, EventArgs e)
{
var items = LoadItemsToListBox();
//add items to listbox
AddListBoxItems(items.ToArray());
//store the items from listbox
allItems.AddRange(listBox1.Items.Cast<string>().ToList());
}
METHOD 1:
HERE'S THE TEXTBOX SEARCH:
//REFER TO SEARCH ITEMS FUNCTION 2
C#:
private void textBox1_TextChanged(object sender, EventArgs e)
{
//clear the items first when start typing
listBox1.Items.Clear();
//here we search the items from allItems
var items = SearchItem(allItems, textBox1.Text);
AddListBoxItems(items);
if (listBox1.Items.Count > 0)
{
listBox1.SetSelected(0, true); //select the result item
}
//now if the searchbox is empty, we can restore the old items from allitems
if (string.IsNullOrEmpty(textBox1.Text))
{
listBox1.SelectedItems.Clear();
AddListBoxItems(allItems);
}
}
METHOD 2:
TEXTBOX SEARCH:
//REFER TO SEARCH ITEMS FUNCTION 1
C#:
private void textBox1_TextChanged(object sender, EventArgs e)
{
SearchItem(textBox1.Text);
}
FUNCTIONS:
LOAD ITEMS TO LISTBOX FROM FILE DIALOG:
C#:
private IEnumerable<string> LoadItemsToListBox()
{
using (var ofd = new OpenFileDialog() {
Filter =
"Audio Files (*.mp3, *.mp4, *.wmv, *.m4a) | *.mp3; *.mp4; *.wmv; *.m4a" +
"|Video Files (*.mp4, *.mkv, *.ts, *.flv, *.m4v) | *.mp4; *.mkv; *.ts; *.flv, *.m4v" +
"|Image Files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png" +
"|Text Files (*.txt) | *.txt" +
"|All Files (*.*) | *.*",
Multiselect = true,
RestoreDirectory = true,
//AutoUpgradeEnabled = false //kung gusto mo old windows xp style enable mo
})
{
List<string> items = new List<string>();
if (ofd.ShowDialog() == DialogResult.OK)
{
var fileName = ofd.FileNames;
foreach(var name in fileName) {
//store the names to lists
items.Add(Path.GetFileName(name));
}
}
return items;
}
}
ADD ITEMS TO LISTBOX
C#:
private void AddListBoxItems(IEnumerable<string> items)
{
listBox1.BeginUpdate();
listBox1.Items.AddRange(items.ToArray()
.Where(x => !listBox1.Items.Cast<string>()
.Any(item => x.Equals(item, StringComparison.InvariantCultureIgnoreCase))).ToArray());
listBox1.EndUpdate();
CountListBoxItems();
}
SEARCH ITEMS FUNCTION 1
C#:
private void SearchItem(string searchItem) {
//clear existing items
listBox1.Items.Clear();
for (int i = listBox1.Items.Count - 1; i >= 0; i--) {
if (listBox1.Items[i].ToString().Contains(searchItem)) {
listBox1.SetSelected(i, true);
}
else {
listBox1.Items.RemoveAt(i);
}
}
if (string.IsNullOrEmpty(textBox1.Text)) {
listBox1.SelectedItems.Clear();
AddListBoxItems(allItems);
}
}
SEARCH ITEM FUNCTION 2:
C#:
//here's the function. you can make static for global use
private IEnumerable<string> SearchItem(IEnumerable<string> items, string searchString)
{
return items.Cast<string>().ToList().Where(x => x.Contains(searchString)).ToArray();
}
Last edited: