将文件夹中的文件导入组合框(仅文件名) [英] Getting files in folder into combobox (only the name of the files)

查看:123
本文介绍了将文件夹中的文件导入组合框(仅文件名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我需要一些编辑代码片段的帮助。



代码是以下:



Hello!

I need some help with editing a code snippet.

the code is following:

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





使用此代码,我将没有扩展名的jpg文件放入组合框中。



我的问题是关注,

我会把列表中的第一项放到文本字段中吗?



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?

推荐答案

我通常使用基本的字符串操作来完成这项操作 - 最可靠的时间。



你使用的是.Net,所以你需要这个:

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;





创建一个名为MyString的字符串,将问题字符串的值设为对象,在你的情况下为Champions \Item\Skins\1.jpg



现在,你只需要1 。

我们只需要.Net Framework中的3个字符串操作:

1.子串

2. LastIndexOf

3. IndexOf



Substring为您提供从您指定的从零开始的位置开始的字符串。

SubString(2)单词Bike给出ke。

SubString(2,1)在作品Bike中给出k。第二个可选参数是你想从起点开始的字符数量。



LastIndexOf返回另一个字符串中最后一次出现的从零开始的索引。 br />


IndexOf返回基于零的第一次出现在另一个内的字符串





因此你需要这样做:





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:

//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 */


请使用: http:// msdn .microsoft.com / zh-cn / library / system.io.path.getfilenamewithoutextension.aspx [ ^ ]。



但还有另外一个你真正需要的链接更多: Microsoft Q209354



-SA


解决了编码问题:)



我的解决方案得到了这个。



Solved the coding :)

My solution got to this.

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 


这篇关于将文件夹中的文件导入组合框(仅文件名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆