如何在文本文件中只获取某个值到组合框?

时间:2016-06-27 16:10:56

标签: c# combobox

因此我需要有关如何仅向组合框添加特定信息的帮助。我和我的同学正在制作一个狗狗节目,你可以在那里买一条狗或捐一只狗(不是真正的狗狗,顺便说一下。只是为了好玩而创造)。因此,当用户决定购买狗时,他/她可以从组合框中选择一个品种(成功制作,顺便说一下)。然后,用户单击下一个按钮,以便用户可以从组合框内的列表中选择一个(问题是,组合框的项目将从文本文件中获取)。

因此,例如,用户选择斗牛犬作为品种然后点击“下一步”。然后下一个窗口将显示一个组合框,其中列出了文本文件中所有斗牛犬的狗(狗的标签号-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品种+ nameOfTheDog =价格) **一条狗信息=一行,不知道文本结构发生了什么

到目前为止的代码是这样的:

  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的两只斗牛犬(包括标签号和价格)??

3 个答案:

答案 0 :(得分:1)

首先,以最方便的格式获取数据:

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

答案 1 :(得分:0)

尝试:

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

......除非我误解了。

答案 2 :(得分:0)

我建议整个代码重组。最好是你创建一个类来保存你的狗的所有数据,如:

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.