📄 cuserform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 个人通讯录
{
/// <summary>
/// Summary description for CUserForm.
/// </summary>
public class CUserForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBoxPW;
private System.Windows.Forms.TextBox textBoxUser;
private System.Windows.Forms.Label labelPW;
private System.Windows.Forms.Label labelUser;
private System.Windows.Forms.Button buttonOk;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.TextBox textBoxConfirm;
private System.Windows.Forms.Label labelConfirm;
private MainForm mainForm;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public CUserForm(MainForm pForm)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
mainForm = pForm;
textBoxUser.Text = mainForm.currentUser;
textBoxUser.SelectAll();
}
public CUserForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.textBoxPW = new System.Windows.Forms.TextBox();
this.textBoxUser = new System.Windows.Forms.TextBox();
this.labelPW = new System.Windows.Forms.Label();
this.labelUser = new System.Windows.Forms.Label();
this.buttonOk = new System.Windows.Forms.Button();
this.buttonCancel = new System.Windows.Forms.Button();
this.textBoxConfirm = new System.Windows.Forms.TextBox();
this.labelConfirm = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBoxPW
//
this.textBoxPW.Location = new System.Drawing.Point(80, 48);
this.textBoxPW.Name = "textBoxPW";
this.textBoxPW.PasswordChar = '*';
this.textBoxPW.Size = new System.Drawing.Size(120, 21);
this.textBoxPW.TabIndex = 14;
this.textBoxPW.Text = "";
//
// textBoxUser
//
this.textBoxUser.Location = new System.Drawing.Point(80, 16);
this.textBoxUser.Name = "textBoxUser";
this.textBoxUser.Size = new System.Drawing.Size(120, 21);
this.textBoxUser.TabIndex = 13;
this.textBoxUser.Text = "";
//
// labelPW
//
this.labelPW.Location = new System.Drawing.Point(16, 48);
this.labelPW.Name = "labelPW";
this.labelPW.Size = new System.Drawing.Size(64, 23);
this.labelPW.TabIndex = 12;
this.labelPW.Text = "密码";
this.labelPW.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// labelUser
//
this.labelUser.Location = new System.Drawing.Point(16, 16);
this.labelUser.Name = "labelUser";
this.labelUser.Size = new System.Drawing.Size(64, 23);
this.labelUser.TabIndex = 11;
this.labelUser.Text = "用户名";
this.labelUser.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// buttonOk
//
this.buttonOk.Location = new System.Drawing.Point(240, 24);
this.buttonOk.Name = "buttonOk";
this.buttonOk.Size = new System.Drawing.Size(64, 24);
this.buttonOk.TabIndex = 15;
this.buttonOk.Text = "确定";
this.buttonOk.Click += new System.EventHandler(this.buttonOk_Click);
//
// buttonCancel
//
this.buttonCancel.Location = new System.Drawing.Point(240, 64);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(64, 24);
this.buttonCancel.TabIndex = 16;
this.buttonCancel.Text = "取消";
this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click);
//
// textBoxConfirm
//
this.textBoxConfirm.Location = new System.Drawing.Point(80, 80);
this.textBoxConfirm.Name = "textBoxConfirm";
this.textBoxConfirm.PasswordChar = '*';
this.textBoxConfirm.Size = new System.Drawing.Size(120, 21);
this.textBoxConfirm.TabIndex = 18;
this.textBoxConfirm.Text = "";
//
// labelConfirm
//
this.labelConfirm.Location = new System.Drawing.Point(16, 80);
this.labelConfirm.Name = "labelConfirm";
this.labelConfirm.Size = new System.Drawing.Size(64, 23);
this.labelConfirm.TabIndex = 17;
this.labelConfirm.Text = "确认密码";
this.labelConfirm.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// CUserForm
//
this.AcceptButton = this.buttonOk;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(322, 119);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBoxConfirm,
this.labelConfirm,
this.buttonCancel,
this.buttonOk,
this.textBoxPW,
this.textBoxUser,
this.labelPW,
this.labelUser});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "CUserForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "修改用户信息";
this.Load += new System.EventHandler(this.CUserForm_Load);
this.ResumeLayout(false);
}
#endregion
private void CUserForm_Load(object sender, System.EventArgs e)
{
}
private void buttonOk_Click(object sender, System.EventArgs e)
{
DialogResult result;
// 弹出确认信息
result = MessageBox.Show(this,"确定要修改用户信息吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
// 确认要修改
if(result == DialogResult.Yes)
{
// 不允许用户名为空
if (textBoxUser.Text =="")
{
MessageBox.Show(this,"用户名不能为空,请重新输入!","错误",MessageBoxButtons.OK);
return;
}
// 检查用户名和密码
if (textBoxPW.Text != textBoxConfirm.Text)
{
MessageBox.Show(this,"输入密码和确认密码不正确,请重新输入!","错误",MessageBoxButtons.OK);
return;
}
// 检查用户名是否和数据库中的用户冲突
string commandstring;
commandstring = "SELECT * FROM Users WHERE 用户名='" + textBoxUser.Text + "'";
// 使用SqlCommand
SqlCommand myCommand;
myCommand = new SqlCommand();
myCommand.Connection = mainForm.myConnection;
myCommand.CommandText = commandstring;
// 执行ExecuteReader()返回DataReader类
SqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader();
if (mainForm.currentUser == textBoxUser.Text.Trim())
{
myDataReader.Read();
}
if (myDataReader.Read())
{
MessageBox.Show(this,"该用户名在数据库中已经存在,请重新输入!","错误",MessageBoxButtons.OK);
myDataReader.Close();
return;
}
myDataReader.Close();
// 修改User表的用户信息
string sUpdate;
sUpdate = "UPDATE Users SET 用户名='" + textBoxUser.Text.Trim() + "', 密码='" +
textBoxPW.Text.Trim() + "' WHERE 用户名='" + mainForm.currentUser + "'";
myCommand.CommandText = sUpdate;
myCommand.ExecuteNonQuery();
// 修改Records表的用户信息
sUpdate = "UPDATE Records SET 用户名='" + textBoxUser.Text.Trim() +
"' WHERE 用户名='" + mainForm.currentUser + "'";
myCommand.CommandText = sUpdate;
myCommand.ExecuteNonQuery();
mainForm.currentUser = textBoxUser.Text;
this.Close();
}
}
private void buttonCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -