📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Contact
{
public partial class MainForm : Form
{
private bool isAdd;
private int Fid;
public MainForm()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.enableInfo();
this.btnEdit.Enabled = false;
this.btnDel.Enabled = false;
this.isAdd = true;
}
/// <summary>
/// 使相关控件启用
/// </summary>
private void enableInfo()
{
this.clearInfo();
this.txtAddress.Enabled = true;
this.txtName.Enabled = true;
this.txtPhone.Enabled = true;
this.btnOK.Enabled = true;
this.btnRE.Enabled = true;
this.txtName.Focus();
}
/// <summary>
/// 修改相关控件状态
/// </summary>
private void disableInfo()
{
this.clearInfo();
this.txtAddress.Enabled = false;
this.txtName.Enabled = false;
this.txtPhone.Enabled = false;
this.btnOK.Enabled = false;
this.btnRE.Enabled = false;
this.btnAdd.Enabled = true;
this.btnDel.Enabled = true;
this.btnEdit.Enabled = true;
}
private void clearInfo()
{
this.txtName.Text = "";
this.txtPhone.Text = "";
this.txtAddress.Text = "";
}
private void btnEdit_Click(object sender, EventArgs e)
{
if (this.listView.SelectedItems.Count == 0)
{
MessageBox.Show("请点击选择需要修改的联系人!");
return;
}
this.enableInfo();
ListViewItem lvi = this.listView.SelectedItems[this.listView.SelectedItems.Count - 1];
this.txtName.Text = lvi.SubItems[1].Text;
this.txtPhone.Text = lvi.SubItems[2].Text;
this.txtAddress.Text = lvi.SubItems[3].Text;
Fid = Convert.ToInt32(lvi.SubItems[0].Text);
this.isAdd = false;
this.btnDel.Enabled = false;
this.btnAdd.Enabled = false;
}
private void btnDel_Click(object sender, EventArgs e)
{
int infoID = this.getSelectID();
if (infoID == 0)
{
MessageBox.Show("请选中信息先!");
return;
}
ContactDB cdb = new ContactDB();
try
{
cdb.deleteInfo(infoID);
this.getInfo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 从数据库加载所有好友信息
/// </summary>
private void getInfo()
{
ContactDB cdb = new ContactDB();
try
{
SqlDataReader reader = cdb.getReader();
this.listView.Items.Clear();
while (reader.Read())
{
string[] subItems = new string[]
{
reader.GetInt32(0).ToString(),
reader.GetString(1),
reader.GetString(2),
reader.GetString(3)
};
this.listView.Items.Add(new ListViewItem(subItems));
}
reader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 取得选中联系人的ID
/// </summary>
/// <returns></returns>
private int getSelectID()
{
int result;
if (this.listView.SelectedItems.Count == 0)
{
result = 0;
}
else
{
ListViewItem temp = this.listView.SelectedItems[this.listView.SelectedItems.Count - 1];
result = Convert.ToInt32(temp.SubItems[0].Text);
}
return result;
}
private void btnOK_Click(object sender, EventArgs e)
{
//在执行下面的操作之前你可以对用户提交的信息进行合法验证
//不合法的数据应该拒绝操作
ContactDB cdb = new ContactDB();
if (this.isAdd)
{
try
{
cdb.addInfo(this.txtName.Text, this.txtPhone.Text, this.txtAddress.Text);
MessageBox.Show("成功添加联系人。");
this.getInfo();
this.disableInfo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
cdb.changeInfo(Fid, this.txtName.Text, this.txtPhone.Text, this.txtAddress.Text);
MessageBox.Show("成功修改联系人信息。");
this.getInfo();
this.disableInfo();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btnRE_Click(object sender, EventArgs e)
{
this.disableInfo();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.getInfo();
}
private void btnAbout_Click(object sender, EventArgs e)
{
MessageBox.Show("联系人程序,用于文章演示。\nCopyRight 某某某", "演示程序", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -