关于combox记录登录信息

这段代码展示了如何在登录过程中使用ComboBox组件来保存和加载用户的登录历史。当用户成功登录后,其用户名会被添加到ComboBox的AutocompleteCustomSource中。同时,提供了`savehistory`和`loadhistory`方法,分别在程序启动时加载历史记录到ComboBox,并在关闭时保存当前的ComboBox记录到'history.txt'文件中。
摘要由CSDN通过智能技术生成




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();

}

}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值