2017-07-20 16 views
0

一部のデータで検索する必要があります。まず、私のコードは29桁目を選択し、4桁の数字を保持しています(下の1721が表示されます)。以下の行を比較します。私は29行目の4桁の数字を検索して、行に検索された数字があれば表示できるメッセージを表示しました。それは簡単な部分でした。ここに私の質問です。番号を検索すると、ラベル(label9、label10、label11、label12)の最初の4桁、2桁目の3桁、3番目の6桁の4桁目をどのように表示させることができますか?私はcurrentLine.Substring(1, 4);しようとしたが、それは誤りであった:パート場合C#ハッシュテーブル検索

substring is null.

iが//検索でループを必要ですか?

たとえば、など私たちは、検索に1723を入れて、それがlabel9に1097を示さなければならない、それはlabel10に003を示さなければならないことを想定し、

データ:

1096:001:161207:085050:1721:001:F:000:0007  
1096:001:161207:085050:1721:001:F:000:0007   
1099:003:161207:085719:1722:001:F:000:0007  
1099:003:161207:085719:1722:001:F:000:0007  
1097:002:161207:085719:1723:001:F:000:0007  
1097:002:161207:085719:1723:001:F:000:0007  

コード:

私はここに解決策を見つけた
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    String currentItemIndex = "", currentItemData = "", currentLine = ""; 
    Hashtable hashtable = new Hashtable(); 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //Select File 
     openFileDialog1.ShowDialog(); 
     textBox1.Text = openFileDialog1.FileName; 
     //Select File 

     //Read And Split 
     FileInfo file = new FileInfo(openFileDialog1.FileName.ToString()); 
     StreamReader read = file.OpenText(); 

     currentLine = read.ReadLine(); 
     currentItemIndex = currentLine.Substring(23, 4); 
     currentItemData += currentLine; 
     do 
     { 
      currentLine = read.ReadLine(); 
      if (currentLine == null) 
      { 
       hashtable.Add(currentItemIndex, currentItemData); 
       break; 
      } 

      if (!currentItemIndex.Equals(currentLine.Substring(23, 4))) 
      { 
       hashtable.Add(currentItemIndex, currentItemData); 
       currentItemData = ""; 
       currentItemIndex = currentLine.Substring(23, 4); 
      } 


      currentItemData += currentLine; 
     } while (true); 
    } 


    private void button2_Click(object sender, EventArgs e) 
    { 
     //Search Start 
     string search = textBox2.Text; 
     if (hashtable.ContainsKey(search)) 
     { 
      MessageBox.Show("Found"); 
      label9.Text= 
      label10.Text= 
      label11.Text= 
     } 
     else 
     { 
      MessageBox.Show("NotFound"); } 
      //Search End 
     } 
    } 
} 
+0

ほかにも、 'Dictionary <,>'ではなく 'Hashtable'を使っている理由はありますか?非ジェネリックコレクションは、2005年以来、新しいコードのためにいくぶん古くなっています... –

+0

文字列を ':'に基づいて値の配列に分割する方がよいでしょう。 – JuanR

+0

実際に私は 'Dictionary'を使う方法がわかりません – Osakawa

答えて

0

は好奇心が強い人々のためにある:

 string search = textBox2.Text; 
     if (hashtable.ContainsKey(search)) 

     { 

      this.Size = new Size(324, 260); 


      string value1 = (string)hashtable[search]; 
      label9.Text= value1.Substring(0, 4); 
      label10.Text = value1.Substring(5, 3); 
      label11.Text = value1.Substring(9, 6); 
      label12.Text = value1.Substring(16, 6); 
      label13.Text = value1.Substring(23, 4); 
      MessageBox.Show("Found"); 

     } 
     else 
     { 
      MessageBox.Show("NotFound"); 

     }