Morgan Walker Pizzini
Morgan Walker Pizzini

Reputation: 110

how i can empty listbox contents c#?

i have this code:

public void LoadData1(String link)
    {
        string temp="";
        int pos = 0;
        for (int x = 0, y = 0; x < link.Length; x++) //separate various code
            if (link[x] == 'y')
            {
                for (temp = ""; y < x; y++)
                    if(link[y]!='y')
                        temp += link[y];
                list[pos] = temp;
                pos++;
                y = x++;
            }
        string nota=list[0];
        for (int a = 0; a < list.Count() && list[a]!=null ; a++,nota=list[a])
        {
            string tempI = "";
            //immagine
            for (int x = 0; x < nota.Count(); x++)
            {
                if (nota[x] == 'p')
                    tempI += '+';
                else
                    tempI += nota[x];
            }

            //nome della pagina
            string temp1 = "";
            int c = tempI.Count();
            if (tempI.Count() > 2)
                if (tempI.ElementAt(2) == 'l') //sol
                {

                    for (int x = 0; x < c; x++)

                        if (x == 3 && tempI.ElementAt(3) == 'd')
                            temp1 += '#';
                        else
                            temp1 += tempI.ElementAt(x);
                }
                else
                {

                    for (int x = 0; x < c; x++)

                        if (x == 2 && tempI.ElementAt(2) == 'd')
                            temp1 += '#';
                        else
                            temp1 += tempI.ElementAt(x);
                }
            else
                temp1 = tempI;

            this.Temp.Add(new ImageText() { Text = temp1, linkImage = "/Accordi/"+tempI+".png" });
        }
        this.IsDataLoaded1 = true;
        this.Temp = new ObservableCollection<ImageText>();
    }

this fill a listbox composed of text and image. but i have this problem: when i fill the listbox with 3 element(for example) it's go all right, but when i call again the function for fill the listbox with 2 element, the listbox show the two element of call and at the third space show the element of my previous call!! how i can solve this problem? the listbox is binding by temp, and the string link contains a sequence of code like "DoyDodyRey"

Upvotes: 2

Views: 139

Answers (2)

Dendei
Dendei

Reputation: 571

cant you declare the list at the begining so its empty

List<string> yourlist = new List<string>();

and you dont need to use pos in lists you can just use

yourlist.add(temp)

first post in the list gets yourlist[0] and so on

hope this helps a little :)

Upvotes: 0

Flynn1179
Flynn1179

Reputation: 12075

You're setting the 'nth' element each time you load data, but you're doing nothing about what's already there, so if you had a third element on a previous call, and only two on a subsequent one, the third one will still be there.

It's simply a matter of clearing it first, or removing excess items afterwards. It's difficult to see from your code exactly what's going on, but I think a call to list.Clear() right at the top of the method will probably do the job.

If you want to simply remove excess items, I think you should be able to add while (list.Count() >= pos) list.Remove(pos); right after your first for loop.

Upvotes: 3

Related Questions