1 pièce(s) jointe(s)
Filtrer ListBox avec Enum Problème
Bonjour,
alors voila j'ai une listbox qui contient ma class car
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
public class _Car
{
public enum car
{
[Description("Acura CL 3.2 Type-S `01")] chs0 = 1,
[Description("Acura CL 3.2 Type-S `03")] chs1 = 2,
[Description("Acura INTEGRA Type-R `01")] chs2 = 3,
[Description("Acura NSX `04")] chs3 = 4,
[Description("Acura NSX `91")] chs4 = 5,
[Description("Acura NSX Coupe `97")] chs5 = 6,
[Description("Acura RSX Type-S `04")] chs6 = 7
}
} |
une autre class pour Description
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public static class EnumHelper
{
/// <summary>
/// Returns the description of an enum decorated with a DescriptionAttribute
/// </summary>
/// <param name="value">An Enumeration value</param>
/// <returns>The description of an enumeration value</returns>
public static string GetDescription(this System.Enum value)
{
var description = value.ToString();
var attribute = GetAttribute<System.ComponentModel.DescriptionAttribute>(value);
if (attribute != null)
{
description = attribute.Description;
}
return description;
}
public static T GetAttribute<T>(System.Enum value) where T : System.Attribute
{
var field = value.GetType().GetField(value.ToString());
var attribute = ((T[])field.GetCustomAttributes(typeof(T), false)).FirstOrDefault();
return attribute;
}
} |
donc je charge le tout comme ça
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public Form1()
{
InitializeComponent();
list();
}
public void list()
{
foreach (_Car.car value in Enum.GetValues(typeof(_Car.car)))
{
listBox1.Items.Add(value.GetDescription());
}
} |
ensuite je mais la textbox pour rechercher
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
private void textBox1_TextChanged(object sender, EventArgs e)
{
var itemList = listBox1.Items.Cast<string>().ToList();
if (itemList.Count > 0)
{
listBox1.Items.Clear();
listBox1.Items.AddRange(itemList.Where(i => i.Contains(textBox1.Text.Trim())).ToArray());
}
else if (textBox1.Text == "")
{
listBox1.Items.Clear();
list();
}
} |
est la il y a un problème quand j'efface des lettres il n'actualise pas la liste même quand la textbox est vide quelqu’un aurait une idée svp ?
Pièce jointe 630800
1 pièce(s) jointe(s)
Filtrer ListBox avec Enum Problème
Bonjour
Alors j'ai fait un breakpoint sur la condition
Pièce jointe 630875
Filtrer ListBox avec Enum Problème
Merci pour votre réponse donc j'ai regarder le code avec vos explication l'erreur vien donc de la condition j'ai
compléter le code comme ça
Code:
1 2
|
if (itemList.Count > 0 && textBox1.Text != "") |
et ça marche merci ;)