Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello!

I need some help with editing a code snippet.

the code is following:

C++
System::String^ basefolder = "Champions\\" + comboBox1->SelectedItem->ToString() + "\\Skins\\";
				 for each (String^ file in Directory::GetFiles(basefolder,"*.jpg"))
				 {
					 if (pictureBox6->Image != nullptr) 
					 {
						 delete pictureBox6->Image;
						 pictureBox6->Image = nullptr;
					 }
					 comboBox2->Items->Add(Path::GetFileNameWithoutExtension(file));
				 }


With this code, I get the jpg files without extension into combobox.

My question is following,
would I do to get the 1st item in the list into the text field?
Posted
Updated 11-Oct-13 8:19am
v3
Comments
Sergey Alexandrovich Kryukov 10-Oct-13 18:29pm    
What's wrong with just reading MSDN documentation?!
—SA

I usually work this out with basic string operations -at times the most reliable.

You are using .Net, so you'll need this on top:
using namespace System;


Create a string called "MyString" and asign it the value of the string in question which in your case will be Champions\Item\Skins\1.jpg

Now, you only need "1".
We'll need only 3 string operations from the .Net Framework which are:
1. SubString
2. LastIndexOf
3. IndexOf

Substring gives you the string starting from the zero-based position you indicate.
SubString(2) on the word "Bike" gives out "ke".
SubString(2,1) in the work "Bike" gives out "k". The second optional argument is the amount of characters you want from the starting point.

LastIndexOf returns the zero-based index of the last occurence of a string within another.

IndexOf returnes the zero-based first occurence of a string within another


Therefore you need to do:

C++
//remember MyString already has Champions\Item\Skins\1.jpg
MyString = MyString->Substring( MyString->LastIndexOf("\\") + 1 ); /* remember \ is an escape sequence that's why you need to double it. Or perhaps not, try it single as well*/

/* By this point MyString should be only "1.jpg" */

MyString = MyString->Substring(0, MyString->IndexOf(".") );
/*Now MyString should be only 1 */
 
Share this answer
 
Comments
Xakzi 11-Oct-13 14:16pm    
Thank you for your answer, thought I used another way to get the file names into combobox I need another help :)

I have edited my previous post for my next help, hope you can help me there :)

edit:: Solved my question thanks for the help :)
Please use: http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx[^].

But there is another link you really need much more: Microsoft Q209354.

—SA
 
Share this answer
 
Comments
Xakzi 10-Oct-13 18:58pm    
Thanks, not really necessary for the 2nd link..

as I can code the most basic stuff, I have tried to use that code from the msdn link but can't seem to make it work quite well.. doesn't seem like I will get much help from here with the comment you are giving out.. yes my aspect about this forum and getting help from here is, sorry for my language, sh*t.

Giving out that kind of link doesn't help the next person at all.
Homero Rivera 10-Oct-13 19:35pm    
LOL!! LOL!! Buddy, take it easy! That was fun after all! I wouldn't take it personal, and I've really been trolled in my time. You are invited to look from the funny side.
Take care!
Xakzi 10-Oct-13 20:27pm    
Well.. Maybe I overreacted :p
I can say that I was not expecting that, thought it was pretty funny because he actually made me read the first part into the middle until I got it :p
Sergey Alexandrovich Kryukov 10-Oct-13 22:48pm    
This is great. I invite you to look from the funny side too.
So, as I actually answered your question in full, will you accept it formally (green "Accept" button).
In all cases, your follow-up questions are welcome.
—SA
Homero Rivera 11-Oct-13 0:19am    
Man! That was awesome! I just played that on someone here in the office!! HAHAHAHA!
Solved the coding :)

My solution got to this.

C#
System::String^ basefolder = "Champions\\" + comboBox1->SelectedItem->ToString() + "\\Skins\\";
                 for each (String^ file in Directory::GetFiles(basefolder,"*.jpg"))
                 {
                     comboBox2->Items->Add(Path::GetFileNameWithoutExtension(file));
                     //Load file name without extension / path.
                 }

                 comboBox2->SelectedIndex = 0; //displays first item in combobox 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900