public FormStudentDetail(StudentExt studentExt) { InitializeComponent(); this.label2.Text = studentExt.studentName; if (studentExt.img.Length != 0) { this.pictureBox1.Image = (Image)SerializeObjectToString.DeserializeObject(studentExt.img); } this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; studentExt1 = studentExt; }
/// <summary> /// 点击提交按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { // 检验数据 if (this.textBox1.Text.Trim().Length == 0) { MessageBox.Show("姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!this.radioButton1.Checked && !this.radioButton2.Checked) { MessageBox.Show("性别不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (this.comboBox1.SelectedIndex == -1) { MessageBox.Show("专业不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } string dataOfImage = null; if (this.pictureBox1.Image != null) { dataOfImage = SerializeObjectToString.SerializeObject(this.pictureBox1.Image); } Student student = new Student() { studentName = this.textBox1.Text.Trim(), sex = this.radioButton1.Checked ? "男" : "女", birthday = Convert.ToDateTime(this.dateTimePicker1.Text), classId = Convert.ToInt32(this.comboBox1.SelectedValue), img = dataOfImage }; try { studentService.insert(student); // 询问是否继续添加,如果是,就清空 DialogResult dialogResult = MessageBox.Show("是否继续添加", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dialogResult == DialogResult.OK) { foreach (Control item in this.Controls) { if (item is PictureBox) { PictureBox pictureBox = (PictureBox)item; pictureBox.Image = null; } else if (item is TextBox) { TextBox textBox = (TextBox)item; textBox.Text = ""; } else if (item is RadioButton) { RadioButton radioButton = (RadioButton)item; radioButton.Checked = false; } else if (item is ComboBox) { ComboBox comboBox = (ComboBox)item; comboBox.SelectedIndex = -1; } } } } catch (Exception ex) { MessageBox.Show("插入数据发生异常" + ex.Message, "警告"); } }