求助,winform登录窗口怎么实现记住密码? 本帖最后由 a470649690 于 2011-05-11 11:17:19 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加个文件判断是否选中了记住密码,比如ini,xml,txt 等 http://topic.csdn.net/u/20080831/10/337d2d10-d0b2-40d9-9bbb-1e56bf49556d.html 很久以前做过一个类似的功能是否自动登陆和是否记住密码我是这样做的:配置文件里 设置两个变量 一个是:isAutoLogon(bool)用来表示是否自动登陆另一个: autoLogonUser(string)存储自动登陆的用户名如果客户选择了记住密码,isAutoLogon=true; autoLogonUser=userName;当(isAutoLogon等于true){ txtUserName等于autoLogonUser; txtPassword等于(从数据库读取密码,where userName=autoLogonUser);} 进行序列化 登录成功后,把用户名和密码封装成实体类,利用BinaryFormat进行序列化保存在本地文件中等下次打开登录窗口,我进行反序列化。 这个东东有记住密码功能,可以看看源码去~[align=center]*****************************************本内容使用CSDN小秘书回复每天回帖即可得10分可用分!*****************************************[/align] 这种我看到之前有些帖子也讲过,但我想把密码存到bin文件里,密码是加密过的,存的时候是存了加密后的密码还有加密生成的key 以前做过,用ComboBox控件来显示用户名。窗体加载的时候,读取data.bin,把用户名加到ComboBox中。在ComboBox的SelectedValueChanged事件中,获取comboBox1.SelectedItem.ToString(),跟读取的用户名对比,取出密码。赋值给PassTextBox即可。其他跟正常登录一样。 是否保存密码(复选框)和用户名,密码保存到INI文件登陆时候,判断是否保存值,如果为真。读取INI中的用户名和密码。 读取INI或XML中的数据好了。对应一个账号一个节点。 C++是主修,C#不打算学太多,怕和C++混了............. using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace StudyWinform{ public partial class Form2 : Form { string dataPath = Application.StartupPath + "\\data.bin"; private List<UserInfo> userlist = new List<UserInfo>(); public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (chkIsSave.Checked) { if (cboxUsername.SelectedItem.ToString() != "") { UserInfo user = userlist.Find(t => t.UserName == cboxUsername.SelectedItem.ToString()); if (user != null) { user.Paaaword = tbPassword.Text; } File.WriteAllText(dataPath, ""); File.AppendAllText(dataPath, user.UserName + ";" + user.Paaaword + "\r\n", Encoding.UTF8); for (int i = 0; i < userlist.Count; i++) { if (userlist[i].UserName != user.UserName) { File.AppendAllText(dataPath, userlist[i].UserName + ";" + userlist[i].Paaaword + "\r\n", Encoding.UTF8); } } } } } private void cboxUsername_SelectedValueChanged(object sender, EventArgs e) { if (cboxUsername.SelectedItem.ToString() != "") { UserInfo user = userlist.Find(t => t.UserName == cboxUsername.SelectedItem.ToString()); if (user != null) { tbPassword.Text = user.Paaaword; } } } private void Form2_Load(object sender, EventArgs e) { string[] data = File.ReadAllLines(dataPath); for (int i = 0; i < data.Length; i++) { if (data[i] != "" && data[i].Contains(";")) { UserInfo user = new UserInfo(); user.UserName = data[i].Split(';')[0]; user.Paaaword = data[i].Split(';')[1]; cboxUsername.Items.Add(user.UserName); userlist.Add(user); } } } } public class UserInfo { public string UserName { get; set; } public string Paaaword { get; set; } }}namespace StudyWinform{ partial class Form2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.tbPassword = new System.Windows.Forms.TextBox(); this.cboxUsername = new System.Windows.Forms.ComboBox(); this.chkIsSave = new System.Windows.Forms.CheckBox(); this.btnLogin = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // tbPassword // this.tbPassword.Location = new System.Drawing.Point(59, 70); this.tbPassword.Name = "tbPassword"; this.tbPassword.Size = new System.Drawing.Size(121, 20); this.tbPassword.TabIndex = 0; this.tbPassword.UseSystemPasswordChar = true; // // cboxUsername // this.cboxUsername.FormattingEnabled = true; this.cboxUsername.Location = new System.Drawing.Point(59, 25); this.cboxUsername.Name = "cboxUsername"; this.cboxUsername.Size = new System.Drawing.Size(121, 21); this.cboxUsername.TabIndex = 1; this.cboxUsername.SelectedValueChanged += new System.EventHandler(this.cboxUsername_SelectedValueChanged); // // chkIsSave // this.chkIsSave.AutoSize = true; this.chkIsSave.Location = new System.Drawing.Point(13, 126); this.chkIsSave.Name = "chkIsSave"; this.chkIsSave.Size = new System.Drawing.Size(62, 17); this.chkIsSave.TabIndex = 2; this.chkIsSave.Text = "记住我"; this.chkIsSave.UseVisualStyleBackColor = true; // // btnLogin // this.btnLogin.Location = new System.Drawing.Point(105, 122); this.btnLogin.Name = "btnLogin"; this.btnLogin.Size = new System.Drawing.Size(75, 23); this.btnLogin.TabIndex = 3; this.btnLogin.Text = "登录"; this.btnLogin.UseVisualStyleBackColor = true; this.btnLogin.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(10, 28); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(43, 13); this.label1.TabIndex = 4; this.label1.Text = "用户名"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(10, 73); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(43, 13); this.label2.TabIndex = 5; this.label2.Text = "密 码"; // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(209, 160); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.btnLogin); this.Controls.Add(this.chkIsSave); this.Controls.Add(this.cboxUsername); this.Controls.Add(this.tbPassword); this.Name = "Form2"; this.Text = "Form2"; this.Load += new System.EventHandler(this.Form2_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox tbPassword; private System.Windows.Forms.ComboBox cboxUsername; private System.Windows.Forms.CheckBox chkIsSave; private System.Windows.Forms.Button btnLogin; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; }} 窗口名称 如何在winform中将datagrid中的数据导出到excel中! C#语言问题,请教高手!读写文本文件! 负载平衡下的session问题 winform 安装项目 如何在桌面建立快捷方式 马上结贴就走 用.NET二维数组 打印出杨辉三角 新人求教 dataset更新和插入数据库 我循环生产控件数组后怎么访问不了??? 从数据库取出一个记录,分别在WORD文档的固定位置打印,求解 求修改SQL语句 updatepanel控件中的htmltable提取不出动态添加的控件
是否自动登陆和是否记住密码我是这样做的:
配置文件里 设置两个变量
一个是:isAutoLogon(bool)用来表示是否自动登陆
另一个: autoLogonUser(string)存储自动登陆的用户名如果客户选择了记住密码,isAutoLogon=true; autoLogonUser=userName;
当(isAutoLogon等于true)
{
txtUserName等于autoLogonUser;
txtPassword等于(从数据库读取密码,where userName=autoLogonUser);
}
本内容使用CSDN小秘书回复
每天回帖即可得10分可用分!
*****************************************[/align]
窗体加载的时候,读取data.bin,把用户名加到ComboBox中。
在ComboBox的SelectedValueChanged事件中,获取comboBox1.SelectedItem.ToString(),
跟读取的用户名对比,取出密码。赋值给PassTextBox即可。
其他跟正常登录一样。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace StudyWinform
{
public partial class Form2 : Form
{
string dataPath = Application.StartupPath + "\\data.bin"; private List<UserInfo> userlist = new List<UserInfo>(); public Form2()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (chkIsSave.Checked)
{
if (cboxUsername.SelectedItem.ToString() != "")
{
UserInfo user = userlist.Find(t => t.UserName == cboxUsername.SelectedItem.ToString());
if (user != null)
{
user.Paaaword = tbPassword.Text;
}
File.WriteAllText(dataPath, "");
File.AppendAllText(dataPath, user.UserName + ";" + user.Paaaword + "\r\n", Encoding.UTF8);
for (int i = 0; i < userlist.Count; i++)
{
if (userlist[i].UserName != user.UserName)
{
File.AppendAllText(dataPath, userlist[i].UserName + ";" + userlist[i].Paaaword + "\r\n", Encoding.UTF8);
}
}
} }
} private void cboxUsername_SelectedValueChanged(object sender, EventArgs e)
{
if (cboxUsername.SelectedItem.ToString() != "")
{
UserInfo user = userlist.Find(t => t.UserName == cboxUsername.SelectedItem.ToString());
if (user != null)
{
tbPassword.Text = user.Paaaword;
}
}
} private void Form2_Load(object sender, EventArgs e)
{ string[] data = File.ReadAllLines(dataPath);
for (int i = 0; i < data.Length; i++)
{
if (data[i] != "" && data[i].Contains(";"))
{
UserInfo user = new UserInfo();
user.UserName = data[i].Split(';')[0];
user.Paaaword = data[i].Split(';')[1];
cboxUsername.Items.Add(user.UserName);
userlist.Add(user);
}
}
}
} public class UserInfo
{
public string UserName { get; set; }
public string Paaaword { get; set; }
}
}
namespace StudyWinform
{
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tbPassword = new System.Windows.Forms.TextBox();
this.cboxUsername = new System.Windows.Forms.ComboBox();
this.chkIsSave = new System.Windows.Forms.CheckBox();
this.btnLogin = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// tbPassword
//
this.tbPassword.Location = new System.Drawing.Point(59, 70);
this.tbPassword.Name = "tbPassword";
this.tbPassword.Size = new System.Drawing.Size(121, 20);
this.tbPassword.TabIndex = 0;
this.tbPassword.UseSystemPasswordChar = true;
//
// cboxUsername
//
this.cboxUsername.FormattingEnabled = true;
this.cboxUsername.Location = new System.Drawing.Point(59, 25);
this.cboxUsername.Name = "cboxUsername";
this.cboxUsername.Size = new System.Drawing.Size(121, 21);
this.cboxUsername.TabIndex = 1;
this.cboxUsername.SelectedValueChanged += new System.EventHandler(this.cboxUsername_SelectedValueChanged);
//
// chkIsSave
//
this.chkIsSave.AutoSize = true;
this.chkIsSave.Location = new System.Drawing.Point(13, 126);
this.chkIsSave.Name = "chkIsSave";
this.chkIsSave.Size = new System.Drawing.Size(62, 17);
this.chkIsSave.TabIndex = 2;
this.chkIsSave.Text = "记住我";
this.chkIsSave.UseVisualStyleBackColor = true;
//
// btnLogin
//
this.btnLogin.Location = new System.Drawing.Point(105, 122);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(75, 23);
this.btnLogin.TabIndex = 3;
this.btnLogin.Text = "登录";
this.btnLogin.UseVisualStyleBackColor = true;
this.btnLogin.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(10, 28);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(43, 13);
this.label1.TabIndex = 4;
this.label1.Text = "用户名";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(10, 73);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(43, 13);
this.label2.TabIndex = 5;
this.label2.Text = "密 码";
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(209, 160);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnLogin);
this.Controls.Add(this.chkIsSave);
this.Controls.Add(this.cboxUsername);
this.Controls.Add(this.tbPassword);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox tbPassword;
private System.Windows.Forms.ComboBox cboxUsername;
private System.Windows.Forms.CheckBox chkIsSave;
private System.Windows.Forms.Button btnLogin;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
}
}