目录
-
登陆界面:
-
设计中的注意事项:
- 窗体设计密码显示在属性->行为->PasswordChar中设置;
- 设置一个全局的用户变量,设置全局名称和密码,用来储存textbox中输入的用户名和密码;
- 用事件TextChanged或Leave获取输入内容;使用Leave事件是保证输入完结束后和保存;使用TextChanged事件是在输入过程中进行操作,效率不高、易卡顿;
- 使用登录按钮检查密码是否匹配,并反馈登陆状态;在一个窗口中弹出另一个窗口:ShowDialog();
- 出现报错:“vs2019 未能找到任何适合于指定的区域性或非特定区域性的资源。”注意域名和文件名等名称要一致;
-
“Opening.h”
- 头文件引入”MySQLDbConn.h”; “User.h”; “SignIn.h”
- 引进全局变量 usName, usPasswd, userConnect, member, logstatus;
- 组件声明
private: System::Windows::Forms::Label^ label1; //欢迎词(英文) private: System::Windows::Forms::Label^ label2; //欢迎词(中文) private: System::Windows::Forms::TextBox^ textBox1; //用户名输入框 private: System::Windows::Forms::TextBox^ textBox2; //用户密码输入框 private: System::Windows::Forms::Label^ label3; //“用户名” private: System::Windows::Forms::Label^ label4; //“密码 private: System::Windows::Forms::Button^ button1; //“登录”按钮 private: System::Windows::Forms::Button^ button2; //“注册”按钮
-
事件方法
- 单击“登录”按钮
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { //单击登录按钮事件:检查密码是否正确,显示登陆状态。 if (usPasswd == member.GetUserPasswd()) { System::Windows::Forms::MessageBox::Show("欢迎!", "登陆成功!"); Opening::Close(); logstatus = true; } else { System::Windows::Forms::MessageBox::Show("错误!用户名或密码错误,请重新输入!", "登录失败!"); } }
-
焦点离开用户名输入框
private: System::Void textBox1_Leave(System::Object^ sender, System::EventArgs^ e) { //Leave事件保证输入完整后在储存输入内容 usName = (char*)(Marshal::StringToHGlobalAnsi(this->textBox1->Text)).ToPointer(); //执行选择的SQL语句 string sql = "select * from user where name = '" + usName + "'"; //string转char* const char* Sql = nullptr; Sql = sql.c_str(); //查找失败,引导注册。 userConnect.ExecuteQuerySql(Sql); //判断用户是否存在 if (userConnect.GetRow() == NULL) { System::Windows::Forms::MessageBox::Show("错误!用户不存在,请点击\"注册\"按钮进行注册!", "登录失败!"); } else { char** userinfo; userinfo = userConnect.GetRow(); const char* wl = "wordlist"; //从数据库中提取用户信息为member对象赋值; member.SetUser(userinfo[0], userinfo[1], userinfo[3], userinfo[2], userinfo[4]); } }
-
单击“注册”按钮
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { SignIn form; form.ShowDialog(); }
-
焦点离开密码输入框
private: System::Void textBox2_Leave(System::Object^ sender, System::EventArgs^ e) { //当textbox中的text值发生改变时,记录输入的内容,并存储在usPasswd中 usPasswd = (const char*)(Marshal::StringToHGlobalAnsi(this->textBox2->Text)).ToPointer(); }
- 单击“登录”按钮
-
成果展示
- 登陆提醒
-
登陆界面
-
当用户输入的用户名不存在时,会有注册引导;
-
登陆成功;
- 登陆提醒
-
-
注册界面
-
“SginIn.h”
- 头文件引入”MySQLDbConn.h”; “User.h”
- 引入全局变量userConnect, member
- 组件声明:
private: System::Windows::Forms::TextBox^ textBox1; //用户名输入框 private: System::Windows::Forms::Label^ label1; //“用户名” private: System::Windows::Forms::Label^ label2; //“密码” private: System::Windows::Forms::TextBox^ textBox2; //密码输入框 private: System::Windows::Forms::Label^ label3; //“再确认一次” private: System::Windows::Forms::TextBox^ textBox3; //二次确认框 private: System::Windows::Forms::Button^ button1; //“确认”按钮 private: System::Windows::Forms::Label^ label4; //“两次密码输入不一致”提示
-
事件方法:
-
焦点离开用户名输入框
private: System::Void textBox1_Leave(System::Object^ sender, System::EventArgs^ e) { String siName = (const char*)(Marshal::StringToHGlobalAnsi(this->textBox1->Text)).ToPointer(); //读取输入的名称 string sqlSelect = "select * from user where name = \"" + siName + "\""; const char* Sql = nullptr; Sql = sqlSelect.c_str(); userConnect.ExecuteQuerySql(Sql); char** userinfo = new char*; userinfo = userConnect.GetRow(); //判断用户是否已存在 if (userinfo) { System::Windows::Forms::MessageBox::Show("用户已存在,请尝试登陆!", "用户已存在"); } else { member.SetUserName(siName); string sqlCreate = "create table if not exists " + siName + "(year int default 2020 not null, month int default 8 not null, day int default 1 not null);"; const char* sql = sqlCreate.c_str(); if (!userConnect.ExecuteSql(sql)) { System::Windows::Forms::MessageBox::Show("信息创建失败,请重试!", "注册失败!"); } sqlCreate = "create table if not exists " + siName + "finish(id int, word varchar(10), chinese varchar(100), type varchar(10));"; sql = sqlCreate.c_str(); if (!userConnect.ExecuteSql(sql)) { System::Windows::Forms::MessageBox::Show("信息创建失败,请重试!", "注册失败!"); } sqlCreate = "create table if not exists " + siName + "newword(id int, word varchar(10), chinese varchar(100), type varchar(10));"; sql = sqlCreate.c_str(); if (!userConnect.ExecuteSql(sql)) { System::Windows::Forms::MessageBox::Show("信息创建失败,请重试!", "注册失败!"); } } }
-
焦点离开密码输入框
private: System::Void textBox2_Leave(System::Object^ sender, System::EventArgs^ e) { string siPasswd = (const char*)(Marshal::StringToHGlobalAnsi(this->textBox2->Text)).ToPointer(); member.SetUserPasswd(siPasswd); }
-
焦点离开二次确认输入框
private: System::Void textBox3_Leave(System::Object^ sender, System::EventArgs^ e) { string cmPasswd = (const char*)(Marshal::StringToHGlobalAnsi(this->textBox3->Text)).ToPointer(); //输入不一致提示 if (member.GetUserPasswd() != cmPasswd) { this->label4->Visible = TRUE; } else { this->label4->Visible = FALSE; } }
-
单击“确认”按钮
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if (this->textBox2->Text == this->textBox3->Text) { string sqlInsert = "insert into user(name, password) values('" + member.GetUsername() + "', '" + member.GetUserPasswd() + "');"; const char* Sql = nullptr; Sql = sqlInsert.c_str(); if (userConnect.ExecuteSql(Sql)) { sqlInsert = "insert into " + member.GetUsername() + "(year, month, day) values(2020, 1, 1);"; const char* Sqli = nullptr; Sqli = sqlInsert.c_str(); userConnect.ExecuteSql(Sqli); System::Windows::Forms::MessageBox::Show("注册成功!请重新登录!", "注册成功!"); SignIn::Close(); } else { System::Windows::Forms::MessageBox::Show("注册失败,请重试!", "注册失败!"); } } }
-
-
-
成果展示
-
密码输入有保障,两次密码不一致时会有提示:
-
当用户输入的用户名存在时,会有登录引导
-
注册后数据库的结果
-
下期预告:计划设定窗口