【问题标题】:How do I get only a certain value in a text file to a combo box?如何仅将文本文件中的某个值获取到组合框?
【发布时间】:2016-06-27 16:10:56
【问题描述】:

所以我需要有关如何仅将特定信息添加到组合框的帮助。我和我的队友正在制作一个狗磅计划,您可以在其中购买狗或捐赠狗(不是真正的狗磅,顺便说一句。只是为了好玩而创建的)。因此,当用户决定购买一只狗时,他/她可以从组合框中选择一个品种(顺便说一句,它已成功制作)。然后,用户单击下一步按钮,以便用户可以从组合框内的列表中选择一个(问题是,组合框的项目将从文本文件中获取)。

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

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

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

【问题讨论】:

  • 尝试将文本文件中的行从那里读入一个 String[] ,然后你可以使用 SubString 或 IndexOf 方法,你也可以使用 .Contains 方法.. 试试看是否有是文本文件布局的一种模式,并创建一个模仿文件结构并以这种方式使用它的类..

标签: c# combobox


【解决方案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()));

【讨论】:

    【解决方案2】:

    试试:

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

    ...除非我误解了。

    【讨论】:

      【解决方案3】:

      我建议对代码进行整体重组。最好创建一个类来保存您的狗的所有数据,例如:

      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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多