Make a filter in my listbox search by word

1

This is a program to show the xml in a listbox :

public partial class Form1 : Form
{
    List<string> list = new List<string>();

    public Form1()
    {
        InitializeComponent();
        LoadSprites();
        list.Clear();
        foreach (String str in listBox.Items)
        {
            list.Add(str);
        }

    }



    private void LoadSprites()
    {
        XDocument xdoc = XDocument.Load("sprites.xml");
        xdoc.Descendants("furnitype").Select(p => new
        {

            id = p.Attribute("classname").Value,
            name = p.Element("name").Value

        }).ToList().ForEach(p =>
        {
            string ci = p.name + "- " + p.id;
            listBox.Items.Add(ci);
            string name = p.name;
            string id = p.id;


        });

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

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

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

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

    private void textBox1_TextChanged(object sender, EventArgs e)
    {


    }
}

How can I search for the entire string? That is, do not start looking for the first letter.

    
asked by Perl 27.07.2016 в 20:39
source

2 answers

1

You should keep the data in a list at the level of the form and filter on these to then show the result in the listbox

Something like this

public class sprite
{
    public string id {get;set;}
    public string name {get;set;}
}

public partial class Form1 : Form
{
    private List<sprite> result = null;

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        LoadSprites();
    }

    private void LoadSprites()
    {
        XDocument xdoc = XDocument.Load("sprites.xml");
        result = xdoc.Descendants("furnitype")
                                    .Select(p => new sprite()
                                    {

                                        id = p.Attribute("classname").Value,
                                        name = p.Element("name").Value

                                    }).ToList();

        result.ForEach(p =>
        {
            listBox.Items.Add(p.name+"- "+ p.id);
        });

    }

    public void button1_Click(...)
    {
        if(result==null)
            return;

        var query = result.Where(x=> x.name.Contains(TextBox1.Text)).ToList(); 

        query.ForEach(p =>
        {
            listBox.Items.Add(p.name+"- "+ p.id);
        });
    }

}

As you will see, a class is created to have the data in the list at the form level, so you can use this data to filter it later when you enter something in the textbox

    
answered by 27.07.2016 в 21:05
0

Starting from the example of Leandro I have improved a little bit what it does:

Something like this:

public class Sprite
{
    public string id { get; set; }
    public string name { get; set; }
}

In the form:

public partial class Form1 : Form
{
    IList<Sprite> Sprites;

    public Form1() { InitializeComponent(); }
    private void Form1_Load(object sender, EventArgs e) { LoadSprites(); }

    private void LoadSprites()
    {
        XDocument xdoc = XDocument.Load("sprites.xml");
        Sprites = xdoc.Descendants("furnitype")
                           .Select(p => new sprite()
                           {
                               id = p.Attribute("classname").Value,
                               name = p.Element("name").Value
                           }).ToList();
        Sprites.ForEach(p =>
        {
            listBox.Items.Add(p.name+"- "+ p.id);
        });
    }

    private void TextBox1_TextChanged(...)
    {
        listBox.Items.Clear(); // O listBox.Clear(), no recuerdo.
        try { 
            var v = result.Where(x => x.name.StartsWith(TextBox1.Text)).ToList();
            v.ForEach(p => { listBox.Items.Add(p.name+"- "+ p.id); });
        } catch { Sprites.ForEach(p => { listBox.Items.Add(p.name+"- "+ p.id); }); }
    }
}

It is a somewhat dirty form but, it works, the event is activated when writing about TextBox , as the user is deleting or writing characters in TextBox , the records in the ListBox are changing.

    
answered by 27.07.2016 в 21:23