C# 如果未找到字符串,请在文本框中重新输入字符串
如果字符串不匹配,如何在文本框中重新输入字符串 我正在尝试的代码是,但它阻塞在循环中,不允许我在文本框中再次输入字符串 有人能指导我怎么做吗 代码是:C# 如果未找到字符串,请在文本框中重新输入字符串,c#,loops,textbox,C#,Loops,Textbox,如果字符串不匹配,如何在文本框中重新输入字符串 我正在尝试的代码是,但它阻塞在循环中,不允许我在文本框中再次输入字符串 有人能指导我怎么做吗 代码是: public void userpass() { int us = 0; //for user pass string readText2 = File.ReadAllText(pathuser); using (StreamReader sr2 = new
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();
}
}
}
好的,我认为你的问题主要是检查文件中是否存在值。您可以使用此功能检查它是否存在,然后可以执行您需要的操作:
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;
}
根据返回值调用此函数后,可以显示messagebox或执行其他操作:
if(findValueInFile(yourPath, yourValue))
{
// We found the value
}
else
{
// We didn't find the value
}
您是否试图检查文本文件中是否存在用户名值?是的,如果不存在,则显示一条消息并返回主窗体,以便用户可以在itI中重新输入文本。我发布了一个答案,告诉我是否有帮助。