2017-03-15 4 views
-1

文字列が一致しない場合に文字列を入力する方法はありますか?文字列が見つからない場合、テキストボックスに文字列を再入力

code私は試していますが、ループに詰まり、テキストボックスに再度文字列を入力できません。

誰もが

にコードをこれを行うにはどのように私を導くことができるである:

public void userpass() 
     { 
      int us = 0; //for user pass 
      string readText2 = File.ReadAllText(pathuser); 
      using (StreamReader sr2 = new StreamReader(pathuser)) 
      { 

       string usernam = username.Text; 
       string line; 
       string[] lines = new String[500]; 

       while ((line = sr2.ReadLine()) != null) 
       { 
        lines[us] = line; 


        if (lines[us] == usernam && usernam != "") 
        { 
         check = 1; 
         MessageBox.Show(usernam); 
         Form2 f2 = new Form2(); 
         this.Hide(); 
         break; 
        } 
        us++; 
       } 

       if (lines[us-1] != usernam && usernam != null) 
       { 
        this.DialogResult = DialogResult.None; 
        DialogResult result = new DialogResult(); 
        result = MessageBox.Show("Invalid Username or Password?", "Retry", MessageBoxButtons.OK); 
        if (result == DialogResult.OK) 
        { 

         username.Clear(); 


        } 
       } 
      } 
+0

ユーザー名の値がテキストファイルに存在するかどうかを確認しようとしていますか? – CNuts

+0

はい、それが存在しない場合は、メッセージを表示してメインフォームに戻り、ユーザーがテキストを入力できるようにします。 – Mubi

+0

私はそれが役立つかどうかを教えてくれます。 – CNuts

答えて

0

わかりましたので、私は値がファイル内に存在する場合は、あなたの問題はほとんどチェックしていると思います。あなたはそれが存在しないかではないと、あなたはあなたが必要なものを行うことができますかどうかを確認するために、この機能を使用することができます。

public bool findValueInFile(string filePath, string value) 
{ 
    //Get all lines from the txt file 
    string[] lines = File.ReadAllLines(filePath);    

    //Go thru all the lines 
    foreach(string line in lines) 
    { 
     //If we find the line we're looking for 
     if (line.Equals(value)) 
     { 
      return true; 
     } 
    } 

    //If we haven't found anything return false 
    return false; 
} 

リターンに応じて、この関数を呼び出した後、あなたがメッセージボックスを表示するか、他の何かを行うことができます。

if(findValueInFile(yourPath, yourValue)) 
{ 
    // We found the value 
} 
else 
{ 
    // We didn't find the value 
} 
関連する問題