2016-10-04 3 views
0

何らかの理由で、私のElseステートメントはifステートメントが実行されていても常に実行されています。Elseステートメントは常に実行されています

string line; 
string[] columns = null; 
while ((line = sr.ReadLine()) != null) 
{ 
    columns = line.Split(','); 
    if (columns.Contains(tboxName.Text)) 
    { 
     rtBoxResults.Text = ((columns[0] + " " + columns[1] + " " + columns[2] + " " + columns[3])); 
    } 
    else 
    { 
     MessageBox.Show("No Hotels Found."); 
     break; 
    } 

それはすべての行がtboxNameが含まれているため、whileループのファイル内のすべての行を検索し、されていないため、このですか?

whileループを使用せずにcolumn [0]のすべての値を返すにはどうすればよいですか?

+0

とのようなものを試してみてください:whileループは、任意の行が一致していなかったかどうかを追跡するためにboolを使用して、完了した後ので、あなたは、このチェックを行うことができる場合分割された値? – Sherlock

答えて

1

私が正しく理解していれば、がない場合はメッセージボックスを表示したいのファイルの行にtboxName.Textが含まれていますか?

 string line; 
     string[] columns = null; 
     bool foundHotels = false; 
     while ((line = sr.ReadLine()) != null) 
     { 
      columns = line.Split(','); 
      if (columns.Contains(tboxName.Text)) 
      { 
       rtBoxResults.Text = ((columns[0] + " " + columns[1] + " " + columns[2] + " " + columns[3])); 
       foundHotels = true; 
      } 
     } 

     if(!foundHotels) 
     { 
      MessageBox.Show("No Hotels Found.");    
     } 
+0

ありがとうございます私のために働いています。 – Ash

1

この

string[] columns = null; 
     var isHotels = false; 
     while ((line = sr.ReadLine()) != null) 
     { 
      columns = line.Split(','); 
      if (columns.Contains(tboxName.Text)) 
      { 
       rtBoxResults.Text = ((columns[0] + " " + columns[1] + " " + columns[2] + " " + columns[3])); 
       isHotels = true; 
      } 
     } // while loop ends 
     if (!isHotels) 
     { 
      MessageBox.Show("No Hotels Found."); 
      break; 
     } 
あなたのテキストボックスの値をトリミングした
関連する問題