如何将文本文件中的特定值仅获取到组合框中?
本文关键字:获取 组合 文本 文件 | 更新日期: 2023-09-27 17:52:15
所以我需要帮助,关于如何我只添加一个特定的信息到一个组合框。我和我的伙伴们正在做一个狗收容所项目,你可以买一只狗或捐赠一只狗(顺便说一句,不是真正的狗收容所。只是为了好玩而创造的)。所以当用户决定购买一只狗时,他/她可以从组合框中选择一个品种(顺便说一句,这是成功的)。然后,用户单击next按钮,以便用户可以从组合框内的列表中选择一个(问题是,组合框的项目将从文本文件中获取)。
例如,用户选择牛头犬作为一个品种,然后点击"下一步"。然后下一个窗口将显示一个组合框,其中列出了文本文件中所有的斗牛犬(狗的标签号-int-,狗的名字-string-和价格-decimal-)
文本文件是这样的:
1-Chihuahua+YY=625.00
3-Boxer+Rad=875.00
25-Terrier+Micky=1500.00
10-Bulldog+Mary=1997.500
4-Pug+Charlie=562.50
6-Bulldog+Cayne=2062.50
* (tagNumber-Breed + nameOfTheDog =价格)**one dog info =一行,不知道文本的结构怎么了
,到目前为止的代码是这样的:
string location=@"C:''Users''LMCPENA98''Desktop''MilleniumPaws''bin''Debug''Files.txt";
string[] temp = File.ReadAllLines(location);
int[] TagNumber = new int[temp.Length];
string[] Breed = new string[temp.Length];
string[] Name = new string[temp.Length];
decimal[] Price = new decimal[temp.Length];
for (int i = 0; i < TagNumber.Length; i++)
{
TagNumber[i] = int.Parse(temp[i].Substring(0, temp[i].IndexOf("-")));
Breed[i] = temp[i].Substring(0, temp[i].IndexOf("+"));
Breed[i] = Breed[i].Substring(Breed[i].LastIndexOf("-") + 1);
Name[i] = temp[i].Substring(0, temp[i].IndexOf("="));
Name[i] = Name[i].Substring(Name[i].LastIndexOf("+") + 1);
Price[i] = decimal.Parse(temp[i].Substring(temp[i].LastIndexOf("=") + 1));
如何在组合框中只显示两只名为Mary和Cayne的斗牛犬(包括标签号和价格)??
首先以最方便的格式获取数据:
var data = File
.ReadLines(location)
.Select(line => Split('-', '+', '='))
.Select(items => new {
tagNumber = int.Parse(items[0]),
breed = items[1],
name = items[2],
price = Decimal.Parse(items[3])
});
则过滤掉并表示
myComboBox.Items.AddRange(data
.Where(item => (item.breed == "Bulldog") &&
((item.name == "Mary") || (item.name == "Cayne"))))
.Select(item => String.Format("tag: {0}; price: {1}", item.tagNumber, item.price)));
我已经使用了一个匿名类,但是如果这样的查询很频繁,您可能想要实现一个特殊的类来记录在文件中:
public class Dog {
public int TagNumber {get; private set}
...
public decimal Price {get; private set}
...
public static Dog Parse(String value) {...}
...
public override String ToString() {
return String.Fromat("tag: {0}; price: {1}", TagNumber, Price);
}
}
var data = File
.ReadLines(location)
.Select(line => Dog.Parse(line));
...
myComboBox.Items.AddRange(data
.Where(dog => (dog.Breed == "Bulldog") &&
((dog.Name == "Mary") || (dog.Name == "Cayne"))))
.Select(dog => dog.ToString()));
尝试:
if (Breed[i] == BreedChosen)
{
TagNumber[i] = int.Parse(temp[i].Substring(0, temp[i].IndexOf("-")));
Breed[i] = temp[i].Substring(0, temp[i].IndexOf("+"));
Breed[i] = Breed[i].Substring(Breed[i].LastIndexOf("-") + 1);
Name[i] = temp[i].Substring(0, temp[i].IndexOf("="));
Name[i] = Name[i].Substring(Name[i].LastIndexOf("+") + 1);
Price[i] = decimal.Parse(temp[i].Substring(temp[i].LastIndexOf("=") + 1));
}
…除非我理解错了。
我建议对代码进行整体重组。最好是创建一个类来保存狗的所有数据,例如:
public class Dog
{
public int tagNumber;
public string Breed;
public string Name;
public decimal Price;
public Dog()
{
tagNumber = 0;
Breed = "None";
Name = "Nameless";
Price = 0;
}
}
您可以在这样的构造函数中添加任何类型的功能,它非常适合您的需要并且易于使用。然后,你可以将你的狗加载到一个简单的列表中,并按照你喜欢的方式对数据进行排序。例子:
string location=@"C:''Users''LMCPENA98''Desktop''MilleniumPaws''bin''Debug''Files.txt";
string[] temp = File.ReadAllLines(location);
//Now we'll get all the dogs from our text file
List<Dog> listOfDogs = temp
.Select(line => line.Split('-', '+', '='))//selecting an array of arrays with the parts of each line
.Select(parts => new Dog{ tagNumber = int.Parse(parts[0]),
Breed = parts[1],
Name = parts[2],
Price = Decimal.Parse(parts[3])})//we're converting those arrays of parts to an instance of our Dog class
.ToList();//making it list
//Now you have your list of dogs
//You can get all the breeds (if you need them for a combobox e.g.). Will be using hashset, so that all the equal strings would be gone
HashSet<string> hsOfDogBreeds = new HashSet<string>(listOfDogs.Select(dog => dog.Breed));
//Afterwards you can do quite much everything you want with the info and it's more comfortable in this way.