-1

I want to add images from folder and list it in dropdown . Like my application has folder name flags containing all the flags images and their country name. how do I add them to dropdown .

1
  • Try describing what you have tried so far. Commented Oct 29, 2014 at 7:23

2 Answers 2

0

Try using the System.IO.Directory.GetFiles and System.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

Something like (haven't tried it)

// 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));
}

Next time, improve your question by describing what you have tried so far, then it would be easier to help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Argument 1: cannot convert from 'string' to 'Microsoft.Office.Tools.Ribbon.RibbonDropDownItem' I get this error
List<string> dirList = new List<string>(); /// DirectoryInfo[] dir = new DirectoryInfo(@"D:\ANITA\NepaAddintool V1.1\NepaAddintool\Flag").GetDirectories(".", SearchOption.AllDirectories); string[] files = Directory.GetFiles(@"D:\ANITA\NepaAddintool V1.1\NepaAddintool\Flag\allflags"); foreach (string file in files) { string language = Path.GetFileName(file); Countryflag.Items.Add(language); }
0

You should include the System.IO namespace and add an ImageList to your form. Set its ImageSize to a nice size for you images.

Then use the code below to do the rest! It loads all files in a folder into both an ImageList and into the Items of a ComboBox. Note that it loads not the filenames but FileInfo objects, so that it can easily display the names without the path. Also note that to display images in a CombBox it has to be owner-drawn, which, as you can see it pretty straight-forward..

Here is the code to use & study:

  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);
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.