📄 userinfomanage.h
字号:
this->ButtonAdd->UseVisualStyleBackColor = true;
this->ButtonAdd->Click += gcnew System::EventHandler(this, &UserInfoManage::button1_Click);
//
// UserLevel
//
this->UserLevel->Location = System::Drawing::Point(110, 88);
this->UserLevel->Name = L"UserLevel";
this->UserLevel->Size = System::Drawing::Size(122, 21);
this->UserLevel->TabIndex = 5;
//
// UserPass
//
this->UserPass->Location = System::Drawing::Point(110, 55);
this->UserPass->Name = L"UserPass";
this->UserPass->PasswordChar = '*';
this->UserPass->Size = System::Drawing::Size(122, 21);
this->UserPass->TabIndex = 4;
//
// UserName
//
this->UserName->Location = System::Drawing::Point(110, 24);
this->UserName->Name = L"UserName";
this->UserName->Size = System::Drawing::Size(122, 21);
this->UserName->TabIndex = 3;
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(31, 91);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(53, 12);
this->label3->TabIndex = 2;
this->label3->Text = L"用户级别";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(31, 58);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(53, 12);
this->label2->TabIndex = 1;
this->label2->Text = L"用户密码";
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(31, 27);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(53, 12);
this->label1->TabIndex = 0;
this->label1->Text = L"用户名称";
//
// UserInfoManage
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(440, 411);
this->Controls->Add(this->groupBox2);
this->Controls->Add(this->groupBox1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
this->KeyPreview = true;
this->MaximizeBox = false;
this->Name = L"UserInfoManage";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"用户信息管理";
this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &UserInfoManage::UserInfoManage_KeyDown);
this->Load += gcnew System::EventHandler(this, &UserInfoManage::UserInfoManage_Load);
this->groupBox1->ResumeLayout(false);
this->groupBox2->ResumeLayout(false);
this->groupBox2->PerformLayout();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void UserInfoManage_Load(System::Object^ sender, System::EventArgs^ e) {
DataManage.ConStr ="Data Source=.;Initial Catalog=BookManage;User ID=sa";
DataManage.ConnectDataBase();
DataManage.Command->CommandText = "select * from UserInfo";
DataManage.DataReader = DataManage.Command->ExecuteReader();
int colnum = DataManage.DataReader->FieldCount;
int currow = 0;
//添加列
for (int col = 0; col<colnum;col++)
{
UserList->Columns->Add(DataManage.DataReader->GetName(col),110,System::Windows::Forms::HorizontalAlignment::Center);
}
//设置列文本
UserList->Columns[0]->Text = "用户名称";
UserList->Columns[1]->Text = "密码";
UserList->Columns[2]->Text = "用户级别";
//添加数据
while (DataManage.DataReader->Read())
{
UserList->Items->Add(DataManage.DataReader->GetString(0)); //添加行
for (int i = 1; i<colnum;i++)
{
UserList->Items[currow]->SubItems->Add(DataManage.DataReader->GetString(i));
}
currow+=1;
}
DataManage.DataReader->Close();
}
//添加用户信息
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (UserInfoIsNull())
{
MessageBox::Show("用户信息不能为空");
return;
}
if (UserIsExist(UserName->Text->Trim()))
{
MessageBox::Show("用户名称不能重复");
return;
}
String ^ sql= String::Format("Insert into UserInfo values ( '{0}', '{1}','{2}')",UserName->Text->Trim(),UserPass->Text->Trim(),UserLevel->Text->Trim());
DataManage.Command->CommandText = sql;
try
{
DataManage.Command->ExecuteNonQuery();
RefreshUserInfo();
ClearText();
MessageBox::Show("操作成功");
}
catch(...)
{
MessageBox::Show("操作失败");
}
}
private: System::Void UserInfoManage_KeyDown(System::Object^ sender, System::Windows::Forms::KeyEventArgs^ e) {
if (e->KeyValue==13)
{
if (ActiveControl->GetType()->Name=="TextBox")
{
Control^ temp = GetNextControl(ActiveControl,true) ;
temp->Focus();
}
}
}
private: System::Void UserList_DoubleClick(System::Object^ sender, System::EventArgs^ e) {
}
//修改按钮单击事件
private: System::Void ButtonUpdate_Click(System::Object^ sender, System::EventArgs^ e) {
if (UserInfoIsNull())
{
MessageBox::Show("用户信息不能为空");
return;
}
if (CurRow== -1)
{
MessageBox::Show("请在表格中选择需要修改的数据");
return;
}
String^ updatecondition = UserList->Items[CurRow]->Text;
if (MessageBox::Show("确实要修改用户名称为"+updatecondition+"的数据吗?","提示",MessageBoxButtons::YesNo)==::DialogResult::Yes)
{
if (updatecondition != UserName->Text->Trim())
{
if (UserIsExist(UserName->Text->Trim()))
{
MessageBox::Show("该用户已经存在");
return;
}
}
String^ sql = String::Format("update UserInfo set username = '{0}',password = '{1}',userlevel = '{2}' where username = '{3}'",UserName->Text->Trim(),UserPass->Text->Trim(),UserLevel->Text->Trim(),updatecondition);
try
{
DataManage.Command->CommandText = sql;
DataManage.Command->ExecuteNonQuery();
ClearText();
RefreshUserInfo();
MessageBox::Show("修改成功");
}
catch(...)
{
MessageBox::Show("操作失败");
}
}
}
private: System::Void UserList_Click(System::Object^ sender, System::EventArgs^ e) {
CurRow = UserList->SelectedItems[0]->Index;
if (CurRow!= -1)
{
UserName->Text = UserList->SelectedItems[0]->Text;
UserPass->Text = UserList->SelectedItems[0]->SubItems[1]->Text;
UserLevel->Text = UserList->SelectedItems[0]->SubItems[2]->Text;
}
}
//删除用户信息
private: System::Void ButtonDelete_Click(System::Object^ sender, System::EventArgs^ e) {
if (CurRow != -1)
{
String^ deletecondition = UserList->Items[CurRow]->Text;
if (MessageBox::Show("确实要删除员工名称为"+deletecondition+"的员工信息吗?","提示",MessageBoxButtons::YesNo)==::DialogResult::Yes)
{
String^ sql = String::Format("delete UserInfo where username = '{0}'",deletecondition);
try
{
DataManage.Command->CommandText = sql;
DataManage.Command->ExecuteNonQuery();
ClearText();
RefreshUserInfo();
MessageBox::Show("删除成功");
}
catch(...)
{
MessageBox::Show("操作失败");
}
}
}
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -