类
添加三个类
MaskPanel窗体(设置遮罩)
遮罩的用于当打开添加窗体或查看窗体的时候,给学生管理系统上添加遮罩层(让窗体不可点击);
public class MaskPanel: Panel // 继承于panel,MaskPanel具备panel特性
{
private const int WS_EX_TRANSPARENT = 0x20;
//设置控件基本信息的属性
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
//ExStyle 扩展样式
cp.ExStyle = cp.ExStyle | WS_EX_TRANSPARENT;
return cp;
}
}
// 自定义控件 在子类重写父类的OnPaint方法,绘制的方法,显示到窗体会自动调用该方法
//Paint绘制
// base 基类 父类
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); // 调用父类的绘制方法,生产基本特点的panel
//var brush = new SolidBrush(Color.FromArgb(50/100,this.BackColor
// 定义一个画笔,画笔的颜色,参数1透明度,参数2是panel的默认颜色
using (var brush = new SolidBrush(Color.FromArgb(35*255/100,this.BackColor)))
{
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
}
public MaskPanel()
{
//设置自定义控件的绘制方式
SetStyle(ControlStyles.Opaque, true);
}
}
Student类
public class Student
{
public string _id { get; set; }
public string name { get; set; }
public string age { get; set; }
public string tel { get; set; }
public string sex { get; set; }
}
public class Data {
public List<Student> data { get; set; }
public int currentPage { get; set; }
public int maxPage { get; set; }
}
Condition类
public class Condition
{
//查询实体类
public string name { get; set; }
public string age { get; set; }
public string sex { get; set; }
public string tel { get; set; }
public int currentPage { get; set; } = 1; //页数查询
public int sort { get; set; } = 0;//排序查询(-1,或 1)
}
添加按钮事件
添加Add窗体
起名FormAdd
添加控件
回到管理系统窗体,双击添加设置点击事件:
//添加学生信息
private void button1_Click(object sender, EventArgs e)
{
mask.Visible = true; // 显示遮罩
FormAdd f1 = new FormAdd();
DialogResult r = f1.ShowDialog(); //模态弹出,以对话框的方式进行弹出,代码执行会被阻塞,后续代码不会执行,
//直到把对话框关闭掉 或者确定对话框的结果
mask.Visible = false;
if (r == DialogResult.OK)
{
//刷新界面
GetStudents();
}
// f1.Show();非模态弹出,弹出界面的时候,不会阻塞代码的执行
}
到添加窗体(baseURL是控制台的地址):
public string baseURL = "http://192.168.107.72:3000"; // 基础路径
// 添加学生
private async void button2_Click(object sender, EventArgs e)
{
string sex = this.radioButton1.Checked ? "男" : (this.radioButton2.Checked ? "女" : "");
string data = $"name={this.textBox1.Text.Trim()}&age={this.textBox2.Text.Trim()}&tel={this.textBox3.Text.Trim()}&sex={sex}";
string res = await HttpTest.PostAsyncWithURLString(baseURL + "/add", data);
string code = JsonConvert.DeserializeObject<JObject>(res)["code"].ToString();
string msg = JsonConvert.DeserializeObject<JObject>(res)["msg"].ToString();
DialogResult r = MessageBox.Show(msg, "是否继续添加", MessageBoxButtons.YesNoCancel);
if (r == DialogResult.Yes)
{
//继续添加
this.textBox1.Text = "";
this.textBox2.Text = "";
this.textBox3.Text = "";
this.radioButton1.Checked = false;
this.radioButton2.Checked = false;
this.textBox1.Focus();//第一个输入框获取焦点
}else if (r == DialogResult.No)
{
this.DialogResult = DialogResult.OK; //把对话框结构设置ok
this.Close();// 关闭窗体
}
}
学生删除和编辑
dataGridView的事件
点击代码:
private async void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// CurrentRow 获取当前选择的行
//.Cells["ID"] 单元格 根据name属性获取单元格值
string id = this.dataGridView1.CurrentRow.Cells["_id"].Value.ToString();
// MessageBox.Show(id);
//获取删除列的索引
int shanchuIndex = this.dataGridView1.Columns["shanchu"].Index;
// 获取编辑列的索引
int bianjiIndex = this.dataGridView1.Columns["bianji"].Index;
// MessageBox.Show(shanchuIndex + "," + bianjiIndex);
if (e.RowIndex >= 0) //你点击了行
{
//e.ColumnIndex 当前列的索引
if (e.ColumnIndex == shanchuIndex)
{
// MessageBox.Show("我点击了删除");
DialogResult r = MessageBox.Show("确定要删除吗?", "删除提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (r == DialogResult.OK)
{
string res = await HttpTest.GetAsyncWithURLEncodingString(baseURL + "/delete", "id="+id);
string code = JsonConvert.DeserializeObject<JObject>(res)["code"].ToString();
MessageBox.Show(JsonConvert.DeserializeObject<JObject>(res)["msg"].ToString());
if (code == "0")
{
GetStudents() ;
}
}
}
else if (e.ColumnIndex == bianjiIndex)
{
mask.Visible = true; //显示遮罩层
FormEditor f1 = new FormEditor(this.list[e.RowIndex]); //在新页面创建一个带参数的构造函数 通过参数传递过去
DialogResult r = f1.ShowDialog();
mask.Visible = false;
if (r == DialogResult.OK)
{
GetStudents();
}
}
}
}
编辑窗体:
添加窗体 FormEditor
代码:
public FormEditor( Student s)
{
InitializeComponent();
this.S = s;
this.textBox1.Text = s.name;
this.textBox2.Text = s.age;
this.textBox3.Text = s.tel;
}
//修改的方法
public string baseURL = "http://192.168.107.72:3000"; // 基础路径
public Student S;
private async void button2_Click(object sender, EventArgs e)
{
string data = $"name={this.textBox1.Text.Trim()}&age={this.textBox2.Text.Trim()}&tel={this.textBox3.Text.Trim()}";
string res = await HttpTest.PostAsyncWithURLString(baseURL + "/update?id=" + this.S._id, data);
string msg = JsonConvert.DeserializeObject<JObject>(res)["msg"].ToString();
MessageBox.Show(msg);
this.DialogResult = DialogResult.OK;
this.Close();
}
刷新:按钮事件
private void button3_Click(object sender, EventArgs e)
{
GetStudents();
}
“世界是虚假的但爱是真的”