面向对象课程设计日志(四)

目录

登陆界面:

设计中的注意事项:

“Opening.h”

成果展示

注册界面

“SginIn.h”

成果展示


  1. 登陆界面:

    1. 设计中的注意事项:

      1. 窗体设计密码显示在属性->行为->PasswordChar中设置;
      2. 设置一个全局的用户变量,设置全局名称和密码,用来储存textbox中输入的用户名和密码;
      3. 用事件TextChanged或Leave获取输入内容;使用Leave事件是保证输入完结束后和保存;使用TextChanged事件是在输入过程中进行操作,效率不高、易卡顿;
      4. 使用登录按钮检查密码是否匹配,并反馈登陆状态;在一个窗口中弹出另一个窗口:ShowDialog();
      5. 出现报错:“vs2019 未能找到任何适合于指定的区域性或非特定区域性的资源。”注意域名和文件名等名称要一致;
    2. “Opening.h”

      1. 头文件引入”MySQLDbConn.h”; “User.h”; “SignIn.h”
      2. 引进全局变量 usName, usPasswd, userConnect, member, logstatus;
      3. 组件声明
        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; //“注册”按钮
        

         

      4. 事件方法

        1. 单击“登录”按钮
          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("错误!用户名或密码错误,请重新输入!", "登录失败!");
          		}
          }
          

           

        2. 焦点离开用户名输入框

          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]);
          	}
          }
          

           

        3. 单击“注册”按钮

          private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
          	SignIn form;
          	form.ShowDialog();
          }
          

           

        4.  焦点离开密码输入框

          private: System::Void textBox2_Leave(System::Object^ sender, System::EventArgs^ e) {
          	//当textbox中的text值发生改变时,记录输入的内容,并存储在usPasswd中
          	usPasswd = (const char*)(Marshal::StringToHGlobalAnsi(this->textBox2->Text)).ToPointer();
          }

           

    3. 成果展示

      1. 登陆提醒

         

      2. 登陆界面

         

      3. 当用户输入的用户名不存在时,会有注册引导;

         

      4. 登陆成功;

         

  2. 注册界面

    1. “SginIn.h”

      1. 头文件引入”MySQLDbConn.h”; “User.h”
      2. 引入全局变量userConnect, member
      3. 组件声明:
        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; //“两次密码输入不一致”提示
        

         

      4. 事件方法:

        1. 焦点离开用户名输入框

          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("信息创建失败,请重试!", "注册失败!");
          		}
          	}
          }
          

           

        2. 焦点离开密码输入框

          private: System::Void textBox2_Leave(System::Object^ sender, System::EventArgs^ e) {
          	string siPasswd = (const char*)(Marshal::StringToHGlobalAnsi(this->textBox2->Text)).ToPointer();
          	member.SetUserPasswd(siPasswd);
          }
          

           

        3. 焦点离开二次确认输入框

          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;
          	}
          }

           

        4. 单击“确认”按钮

          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("注册失败,请重试!", "注册失败!");
          		}
          	}
          } 
          

           

  3. 成果展示

    1. 密码输入有保障,两次密码不一致时会有提示:

       

    2. 当用户输入的用户名存在时,会有登录引导

       

    3. 注册后数据库的结果

       

下期预告:计划设定窗口

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值