2014-10-29 82 views
-1

我想添加文件夹中的图像并将其列在下拉列表中。像我的应用程序有文件夹名称标志包含所有标志图像和他们的国家名称。我如何将它们添加到下拉菜单中。将文件夹中的图像添加到下拉列表

+0

尝试描述什么你到目前为止已经尝试过。 – jesperlndk 2014-10-29 07:23:30

回答

0

尝试使用System.IO.Directory.GetFilesSystem.IO.Path.GetFileName

http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx

喜欢的东西(没试过)

// Process the list of files found in the directory. 
string [] files = Directory.GetFiles(yourDirectory); 
foreach(string file in files) { 
    string language = Path.GetFileName(file); 
    ddlFlags.Items.Add(new ListItem(language, file)); 
} 

下一次,描述什么是你提高你的问题到目前为止尝试过,那么帮助你会更容易。

+0

参数1:无法从'字符串'转换为'Microsoft.Office.Tools.Ribbon.RibbonDropDownItem'我得到这个错误 – user2583182 2014-10-29 10:07:50

+0

列表 dirList =新列表(); (“*。*”,SearchOption.AllDirectories);}}。 string [] files = Directory.GetFiles(@“D:\ ANITA \ NepaAddintool V1.1 \ NepaAddintool \ Flag \ allflags”); (文件中的字符串文件) { string language = Path.GetFileName(file); Countryflag.Items.Add(language); } – user2583182 2014-10-29 10:11:57

0

您应该包含System.IO命名空间并将ImageList添加到您的表单。将其ImageSize设置为适合您的图像。

然后使用下面的代码来完成剩下的工作!它将文件夹中的所有文件加载到ImageListComboBoxItems中。请注意,它不会加载文件名,而是加载FileInfo对象,以便它可以轻松显示没有路径的名称。还要注意的是在它有一个CombBox显示图像是owner-drawn,正如你可以看到它非常直截了当..

下面是代码使用&研究:

using System.IO; 
    //.. 

    // load whereever you like 
    // e.g. in the From.Load event or after InitializeComponent(); 

    var images = Directory.GetFiles(yourImageFolder, "*.jpg"); 
    foreach (string file in images) 
    { 
     imageList1.Images.Add(file, new Bitmap(file)); 
     comboBox1.Items.Add(new FileInfo(file)); 
    } 
    comboBox1.DrawMode = DrawMode.OwnerDrawFixed; 
    comboBox1.DrawItem += comboBox1_DrawItem; 
    comboBox1.ItemHeight = imageList1.ImageSize.Height; 


    void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
    FileInfo FI = (FileInfo)comboBox1.Items[e.Index]; 
    e.Graphics.DrawImage(imageList1.Images[FI.FullName], e.Bounds.Location); 
    e.Graphics.DrawString(FI.Name, Font, Brushes.Black, 
      e.Bounds.Left + imageList1.ImageSize.Height + 3, e.Bounds.Top + 4); 
    } 
相关问题