2016-06-27 3 views
1

그래서 특정 정보 만 콤보 상자에 추가하는 방법에 대한 도움이 필요합니다. 내 동급생과 나는 개를 사거나 개를 기부 할 수있는 개 파운드 프로그램을 만들고 있습니다. (진짜 개 파운드가 아닌, 재미로 만들었습니다.) 그래서 사용자가 개를 사기로 결심했을 때 콤보 상자에서 품종을 선택할 수 있습니다 (성공적으로 작성되었습니다, btw). 그런 다음 사용자가 다음 단추를 클릭하여 사용자가 콤보 상자 안의 목록에서 하나를 선택할 수 있습니다 (문제는 콤보 상자의 항목이 텍스트 파일에서 가져 오는 것임).텍스트 파일의 특정 값만 콤보 상자로 가져 오려면 어떻게합니까?

예를 들어 사용자는 번트로 불독을 선택하고 "다음"을 클릭합니다. 그런 다음 다음 창에는 텍스트 파일 (dog -int-의 태그 번호, dog-string-and price-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 [email protected]"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)); 

메리와 Cayne이라는 두 개의 불독 만 콤보 상자에 표시 할 수 있습니까 (태그 번호 및 가격 포함) ??

+0

는 []를 사용해서 사용할 수 거기에서 당신은 당신이뿐만 아니라 .Contains 방법을 사용할 수있는 문자열이나 같이 IndexOf 방법을 사용할 수있는 문자열로 TEXTFILE의 라인을 읽어보세요 .. 있는지 확인하기 위해 시도가 텍스트 파일 레이아웃의 패턴이며 파일 구조를 모방 한 클래스를 만들고 그 방식으로 작업합니다. – MethodMan

답변

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

... 내가 오해하지 않았다면.

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

코드의 전체적인 구조 조정을 제안합니다. 이 같은 STH, 당신의 개를 위해 모든 데이터를 저장하기위한 클래스를 만드는 경우가 가장 좋은 것입니다 :

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 [email protected]"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. 
관련 문제