简体   繁体   中英

Listbox items “invisibles” when refreshing data (winform)

I have a problem with my listbox : if I change anything in the data (like adding an item or deleting one), the items become invisble but actually is still "here" (I can see it because I have made a sort of tooltip when my mouse go hover it).

The purpose of my listbox is to show a list of scene (like files) when the user select an folder with a combobox.

Here is my code :

public partial class FormScenario : Form
{
    BindingList<Folder> blFolders = new BindingList<Folder>();
    BindingList<Scene> blScenes = new BindingList<Scene>();

    public FormScenario(FormMainPanel fParent)
    {
        InitializeComponent();
        FParent = fParent;
    }

    public void Refresh_blScenes()
    {
        lb_scenes.DataSource = null;
        lb_scenes.Items.Clear();
        lb_scenes.DataSource = this.blScenes;
        lb_scenes.DisplayMember = "SceneName";

        //attempt to fix the bug ... not working
        lb_scenes.DrawMode = DrawMode.Normal;
        lb_scenes.DrawMode = DrawMode.OwnerDrawFixed;
    }

    public void Populate_Scene_Datas()
    {
        blScenes.Clear();
        Folder fol = (Folder)cb_listfolders.SelectedItem;
        if (fol != null){
            foreach (Scene sce in fol.ListScenes){
                this.blScenes.Add(sce);
            }
        }
    }
}

If it is useful, there is my objects :

class Folder
{
    public Int32 IdFolder { get; set; }
    public String FolderName { get; set; }
    public String FolderInfo { get; set; }

    // contains the list to show in the listbox 
    public List<Scene> ListScenes = new List<Scene>();
}

class Scene
{
    public Int32 IdScene { get; set; }
    public String SceneName { get; set; }
    public byte[] SceneDesc { get; set; }
    public byte[] SceneInfo { get; set; }
}

I use these functions in the following order :

Populate_Scene_Datas();
Refresh_blScenes();

if somebody give me a hint or a answer at this problem, it will be very helpfull !

Thanks you !

I have found a solution... but I can't explain why it is working. I'm posting this if someone encounter the same problem.

I have change this :

lb_scenes.DataSource = this.blScenes;

by :

lb_scenes.Items.AddRange(blScenes.ToArray());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM