Hello!

I am coding in windows form application c++/CLR

I need help with coding a way to read everything in a folder, put the names of the files and put the names in a combobox.

These files are for now images (.jpeg).

And I would also like that, when I load these images name into combobox, that the extension does not appear in the combobox.

I have searched a lot and couldn't find anything useful myself :/

Or I am really bad at searching haha.

I really hope you can help me with this.

Sincerely~

Recommended Answers

Searching MSDN can be a pain sometimes. When you're using CLR most of the documentation for .net will have a C++ tab in the examples that will show c++ code for that method. Here's the one for the GetFiles method in the …

Jump to Post

Most likely you'll have to iterate through the array and extract the filename. the Path Class has a GetFileNameWithoutExtension method that will work well for this.

Jump to Post

All 8 Replies

Searching MSDN can be a pain sometimes. When you're using CLR most of the documentation for .net will have a C++ tab in the examples that will show c++ code for that method. Here's the one for the GetFiles method in the DirectoryInfo class.

One thing I've noticed is that sometime the code examples for c++ don't makle it to new pages, so try loading pages from older framework versions.

Once you have an array of Files you can iterate through them to extract the file name and add each one to the combobox.

I found this kind of code.. it works, but can't make the text in the combobox to appear as I want it.

This is the code:

array<String^> ^files = System::IO::Directory::GetFiles("Champions\\" + comboBox1->SelectedItem->ToString() + "\\Skins\\"); // reading names of specific folder
comboBox2->Items->Clear(); //Removes the previous items 
if(comboBox2->Text != nullptr)  // Deletes previous text selected
{
    delete comboBox2->Text;
    comboBox2->Text = nullptr;
}
comboBox2->Items->AddRange(files); //adding file names from directory to combobox

But the text in 2nd combobox appears as:
Champions\Name\Skins\1.jpg

How could I change it so it only says 1 in the second combobox?

Thanks for your answer..

I have looked into the snippets and tried to add them to my favor.. can't say I succeeded.. Is it possible you could help me with that?

Here's some code that should help:

            System::String^ basefolder = "myfolderpath";
            for each (String^ file in Directory::GetFiles(basefolder,"*.jpg"))
            {
                comboBox1->Items->Add(Path::GetFileNameWithoutExtension(file));
            }
            if(comboBox1->Items->Count > 0)
                comboBox1->Text = comboBox1->Items[0]->ToString();

Basically set the basefolder to the path you want to search and all the .jpg file names without extensions will be added to the combobox. I added code to make the displayed text equal the first item.

commented: Really helpful +1 +0

Ohh.. wow.. I could add WithoutExtension like that? o.o Didn't even cross my mind haha..
But here is the deal.. can't put a basefolder, since I have a lot of folders with different images..

I pick an item in combobox1.
depending on the item, I also load the folder (with the same name as the item in combobox1) which contains images that I want to write to combobox2.
it is possible to do something like:

System::String^ basefolder = ""Champions\\" + comboBox1->SelectedItem->ToString() + "\\Skins"

Well, I could try it myself in an hour or so, but wouldn't hurt to ask anyway, and if that is not the way, if I don't figure it out, how would I do it?

wops, wrote wrong thing in my previous comment, but I tested it and got it working, made some tweaks as well.
Thank you a lot!

Sorry but last question, if you could help me with that :p

What code should I add to make the first item also appear in the combobox as text, only the first item.. the rest can be in the combobox list (Items).

Oh, and it is not necessarily numbers in the jpg file's name.

Solved my own question, thanks for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.