添加查询窗体
public Condition Con=null;
public int A = 0;
public FormSearch(Condition c,int a1 ) //c=con a1= a
{
InitializeComponent();
this.Con = c; //c= con = this.Con
this.A = a1; //
this.textBox1.Text = this.Con.name; // 把上一次的搜索条件添加到输入框上
this.textBox2.Text = this.Con.age;
this.textBox3.Text = this.Con.tel;
if (this.Con.sex == "男")
{
this.radioButton1.Checked = true;
}else if (this.Con.sex == "女")
{
this.radioButton2.Checked = true;
}
else
{
this.radioButton3.Checked = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Con.name = this.textBox1.Text.Trim();
this.Con.age = this.textBox2.Text.Trim();
this.Con.tel = this.textBox3.Text.Trim();
this.Con.sex = this.radioButton1.Checked?"男":(this.radioButton2.Checked ? "女" : "");
this.Con.currentPage = 1;
this.DialogResult = DialogResult.OK;
this.Close();
}
在学生管理系统窗体添加 flowlayotPanel
查询学生按钮事件
//查询学生
private void button2_Click(object sender, EventArgs e)
{
mask.Visible = true;
int a = 10;//值类型
FormSearch search = new FormSearch(con,a); //传递查询对象,根据的传递是引用对象,在新窗体
//修改参数时候,其实就是修改了con本身了
DialogResult r = search.ShowDialog();
mask.Visible = false;
if(r == DialogResult.OK)
{
GetStudents();
//MessageBox.Show(con.name + a);
//显示对应条件按钮
ShowBtn();
}
}
// 显示查询按钮组
public Dictionary<string, string> GuoJiHua = new Dictionary<string, string>()
{
{"name","姓名" },
{"age","年龄"},
{"sex","性别" },
{"tel","手机" },
};
public void ShowBtn()
{
this.flowLayoutPanel2.Controls.Clear(); // 清除panel2的控件
Type type = this.con.GetType();// 获取对象的类型 ,进而获取对象里面属性字段等等
PropertyInfo[] infos = type.GetProperties(); //获取对象所有属性
//[{name:'zs' },{ age: '10' }]
Console.WriteLine(infos[0]);
foreach (PropertyInfo info in infos) // 遍历所有属性的属性信息
{
//info.Name
//info.Name != "currentPage" 如果属性名不是currentPage
//info.GetValue(this.con) != null 对象属性值不为空
//info.GetValue(this.con).ToString().Length!=0
if (info.Name != "currentPage" && info.Name != "sort" && info.GetValue(this.con) != null&& info.GetValue(this.con).ToString().Length!=0)
{
Label l = new Label();
l.Text = $"{GuoJiHua[info.Name]}:{info.GetValue(this.con)}";
l.BackColor = Color.Green;
l.TextAlign = ContentAlignment.MiddleCenter;
l.Padding = new Padding(3);
l.Height = 35;
l.Margin = new Padding(0, 3, 0, 0);
l.AutoSize = false;
this.flowLayoutPanel2.Controls.Add(l);
Button btn = new Button();
btn.Text = "×";
btn.Padding = new Padding(3);
btn.Tag = info.Name;
btn.Click += Btn_Click2;
btn.Width = 35;
btn.Height = 36;
this.flowLayoutPanel2.Controls.Add(btn);
}
}
}
private void Btn_Click2(object sender, EventArgs e)
{
string s =((Button)sender).Tag.ToString();
switch (s)
{
case "name":
this.con.name = "";
break;
case "age":
this.con.age = "";
break;
case "sex":
this.con.sex = "";
break;
case "tel":
this.con.tel = "";
break;
default:
break;
}
GetStudents();
ShowBtn();
}
数据库打开
学生数据库的位置搜索cmd回车
输入“npm start” 显示打开数据库就是成功了
"爱是真的世界就是真的"