Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将文本文件中的某个值仅获取到组合框中?_C#_Combobox - Fatal编程技术网

C# 如何将文本文件中的某个值仅获取到组合框中?

C# 如何将文本文件中的某个值仅获取到组合框中?,c#,combobox,C#,Combobox,因此,我需要有关如何只向组合框添加特定信息的帮助。我和我的团友们正在制作一个狗狗收容所计划,在那里你可以买一只狗或捐赠一只狗(顺便说一句,不是真正的狗狗收容所,只是为了好玩而创建的)。因此,当用户决定购买一只狗时,他/她可以从组合框中选择一个品种(顺便说一句,这是成功制作的)。然后,用户单击next按钮,以便用户可以从组合框内的列表中选择一个(问题是,组合框的项目将从文本文件中获取) 例如,用户选择一只斗牛犬作为品种,然后单击“下一步”。然后,下一个窗口将显示一个组合框,其中列出了文本文件中所有

因此,我需要有关如何只向组合框添加特定信息的帮助。我和我的团友们正在制作一个狗狗收容所计划,在那里你可以买一只狗或捐赠一只狗(顺便说一句,不是真正的狗狗收容所,只是为了好玩而创建的)。因此,当用户决定购买一只狗时,他/她可以从组合框中选择一个品种(顺便说一句,这是成功制作的)。然后,用户单击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
  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));
*(tagNumber品种+Dog名称=价格) **一条狗信息=一行,不知道文本的结构发生了什么

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

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
  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));
string location=@“C:\\Users\\LMCPENA98\\Desktop\\MilleniumPaws\\bin\\Debug\\Files.txt”;
字符串[]temp=File.ReadAllLines(位置);
int[]标记编号=新的int[temp.Length];
字符串[]品种=新字符串[临时长度];
字符串[]名称=新字符串[临时长度];
十进制[]价格=新的十进制[温度长度];
for(int i=0;i
如何在组合框中仅显示名为Mary和Cayne的两只斗牛犬(包括标签号和价格)?

试试:

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

…除非我误解了。

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

然后过滤并表示

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

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

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.
string location=@“C:\\Users\\LMCPENA98\\Desktop\\MilleniumPaws\\bin\\Debug\\Files.txt”;
字符串[]temp=File.ReadAllLines(位置);
//现在我们将从文本文件中获取所有的狗
listOfDogs=temp
.Select(line=>line.Split(“-”、“+”、“=”)//选择一个包含每行各部分的数组数组
.Select(parts=>newdog{tagNumber=int.Parse(parts[0]),
品种=零件[1],
名称=零件[2],
Price=Decimal.Parse(parts[3])}//我们正在将这些parts数组转换为Dog类的一个实例
.ToList();//将其列为列表
//现在你有你的狗名单了
//你可以得到所有的品种(例如,如果你需要它们作为组合框),它们将使用hashset,这样所有相等的字符串都将消失
HashSet hsofdogdogrides=新的HashSet(listOfDogs.Select(dog=>dog.bride));
//之后,你可以用这些信息做很多你想做的事情,这样会更舒服。

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