2017-04-26 125 views
-2

我有一個文件,它有三種類型的數據,第一種是金屬類型,第二種是顏色,第三種是顏色數量,但是它們在文本文件中出現的次數發生了變化(最後兩種類型的數據),使用列表框存儲不同類型的數據

 led, black, 0.50, blue, 0.75, green, 0.60 
    copper,blue, 0.48, red, 0.88, pink, 0.33 
    steel, red, 0.65, black, 0.55 
    iron, white, 0.5 
    copper, black, 1, red, 0.80 
    steel, red, 0.62, yellow, 0.50 
    copper, blue, 0.48 

我需要一個流讀取器讀取該數據,並將其存儲在適當的數據結構。然後可以在我的應用程序的WPF的不同部分使用,例如,金屬類型將被放置在一個列表框中,另外兩個將被放置在另一個列表框中。

 StreamReader reader = new StreamReader(File.OpenRead("paint.txt")); 
     string line; 
     while ((line = reader.ReadLine()) != null) 
     { 

      string[] items = line.Split(','); 
      cat1 = (items[2]); 
      string path = null; 
      ; 

      string[] led = new string[items.Length]; 
      string[] copper = new string[items.Length]; 


      for (int i = 0; i < items.Length; i++) 
      { 
       led[i] = items[i]; 
       copper[j] = items[i] 


       foreach (string paint in items) 
       { 
        if (paint.StartsWith("led")) /// paint 
        { 
         path = paint; 
         led[i] = paint; 

        } 

        if (paint.StartsWith("copper")) /// paint 
        { 
         path = paint; 
         led[i] = paint; 

        } 
       } 

      } 

     } 

} 

我做了什麼至今

+0

你爲什麼不首先,讓我們瞭解您已經對自己試過..所有第二,你可以在值在'列表'測試通過例如 創造者的一個實例存儲'var myTypes = new List (){string,string,double,Color,douuble,string,double}' – MethodMan

+0

使用單獨的'List objects'作爲'ListBox.ItemsSource',參見'https://wpf.2000things。 com /'好教程。 – AnjumSKhan

+0

我認爲這裏的主要問題是分類不同的金屬,雙色和顏色列表中的數據,而不僅僅是將它們加載到單個列表或ItemsSource –

回答

0

這是非常簡單的。在流讀取器中讀取文件。用逗號分割流,並創建一個字符串列表。應用以下過濾器方法對Metal,Color和Double中的數據進行分類。在這裏,我使用了System.Drawing的引用來檢查顏色名稱。

private void ClassifyData() 
     { 
      double dblValue = 0; 
      foreach (var item in RandomData) 
      { 
       if (double.TryParse(item, out dblValue)) 
       { 
        dblList.Add(dblValue); 
       } 
       else if (CheckColor(item)) 
       { 
        ColorList.Add(item); 
       } 
       else 
       { 
        MetalList.Add(item); 
       } 
      } 
     } 

     private bool CheckColor(string colorName) 
     { 
      Color c = Color.FromName(colorName); 
      if (c.IsKnownColor) 
       return true; 
      return false; 
     } 
相關問題