开发者

ListBox Winform question

I use OpenFileDialog class, to open and display a filename chosen.

      List<string> paths;
    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
         label1.Text = "Now you can save the file paths or remove them from the list above";
         pa开发者_Go百科ths.Add(openFileDialog1.FileName);
         listBox1.DataSource=paths ;//Only one file is displayed in the listbox
         Refresh();

    }

I want the user to choose several files and display all the files that he chose in the listbox that i have. The problem is that only one file path is displayed each time. What is funny, is that i thoguht that whenever i use pahts.Add,,new filename is added, but in reality it isnt so!?!


Try listBox1.DataSource = null; and then setting it to paths.

Most likely the ListBox is not refreshing because the data source 'has not changed'. We know that the content of the list has changed, however, from the ListBox's standpoint, the object is the same.

Another better option is to use a BindingList<string> which should result in the ListBox updating whenever items are added without any additional fiddling.


You have to set Multiselect in your file dialog to true and then use the FileNames property:

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
   string[] files = openFileDialog1.FileNames;
   paths.AddRange(files);
   listBox1.DataSource=paths;
   Refresh();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜