private void btnok_click(object sender,eventargs e)
{
string username =this.cmbusername.text; //用户名文本框里的内容定义为username
string password =this.txtpassword.text; //道理同上
bool success =this.verify (username,password);
if (success)
{
//记录username
if (!this.cmbusername.autocompletecustomsource.contains(username))
{
this.cmbusername.autocompletecustomsource.add(username);
}
messagebox.show("登录成功!");
}
else
{
messagebox.show("用户名和密码不正确!");
}
}
private bool verify(string username,string password) //获取用户名和密码
{
return username== password; //todo,比较一致动作
}
private void savehistory()
{
string filename =system.io.path.combine(application.startuppath,"history.txt"); //应用启动路径下存储文件名为history。txt
system.io.steamwriter writer = new system.io.streamwriter(filename,false,encoding.deafault) //写入到history文件中个,文件格式txt
foreach (string name in this.cmbusername.autocompletecustomsource) //
//foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成。in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素。
该循环的运行过程如下:每一次循环时,从集合中取出一个新的元素值。放到只读变量中去,如果括号中的整个表达式返回值为true,foreach块中的语句就能够执行。一旦集合中的元素都已经被访问到,整个表达式的值为false,控制流程就转入到foreach块后面
的执行语句。
{
writer.writeline(name); //name内容写入到指定文件中
writer.flush();
writer.close();
}
}
private void loadhistory()
{
string filename =system.io.path.combine(application.startuppath,"history.txt");
if (system.io.file.exists(filename))
{
system.io.streamreader reader = new system.io.streamreader(filename,encoding.deafult);
string name =reader.readline();
while (name! = null)
{
this.cmbusername.autocompletecustumsource.add(name);
this.cmbusername.items.add(name);
name =reader.readline();
reader.close();
}
}
}
private void form1_load(object sender,eventargs e)
{
loadhistory();
}
private void form1_formclosed(object sender,formclosedeventargs)
{
savehistory();
}
}
}