📄 frmpicture.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using DataAccess;
using BusinessRule;
namespace Forms
{
/// <summary>
/// frmPicture 的摘要说明。
/// </summary>
public class frmPicture : System.Windows.Forms.Form
{
//备份本图书原始封面图片地址,以便数据库更新失败时数据回滚
private string TeaOldPic;
//存放教师基本信息
private Teacher teacher;
private int teaID;
private AdminTeacher Ateacher = null;
private MasterTeacher Mteacher = null;
private PrelectTeacher Pteacher = null;
private System.Windows.Forms.Button btnChange;
private System.Windows.Forms.Button btnUpdate;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.OpenFileDialog openFileDialog;
private System.Windows.Forms.PictureBox picTeacher;
private System.Windows.Forms.TextBox txtTeacherPic;
private System.Windows.Forms.Label lblTeacherPic;
private System.Windows.Forms.Label lblTeacher;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//构造方法
public frmPicture()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
Post.EventSendLoginedID += new DataAccess.Post.SendMsgDelegate(Post_EventSendLoginedID);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnChange = new System.Windows.Forms.Button();
this.btnUpdate = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.picTeacher = new System.Windows.Forms.PictureBox();
this.txtTeacherPic = new System.Windows.Forms.TextBox();
this.lblTeacherPic = new System.Windows.Forms.Label();
this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
this.lblTeacher = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnChange
//
this.btnChange.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnChange.Location = new System.Drawing.Point(40, 72);
this.btnChange.Name = "btnChange";
this.btnChange.Size = new System.Drawing.Size(104, 32);
this.btnChange.TabIndex = 2;
this.btnChange.Text = "修改封面";
this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
//
// btnUpdate
//
this.btnUpdate.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnUpdate.Location = new System.Drawing.Point(144, 72);
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.Size = new System.Drawing.Size(104, 32);
this.btnUpdate.TabIndex = 3;
this.btnUpdate.Text = "更新封面";
this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
//
// btnCancel
//
this.btnCancel.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.btnCancel.Location = new System.Drawing.Point(248, 72);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(104, 32);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "取消";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// picTeacher
//
this.picTeacher.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.picTeacher.Location = new System.Drawing.Point(224, 120);
this.picTeacher.Name = "picTeacher";
this.picTeacher.Size = new System.Drawing.Size(112, 144);
this.picTeacher.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picTeacher.TabIndex = 5;
this.picTeacher.TabStop = false;
//
// txtTeacherPic
//
this.txtTeacherPic.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txtTeacherPic.Location = new System.Drawing.Point(144, 24);
this.txtTeacherPic.Name = "txtTeacherPic";
this.txtTeacherPic.Size = new System.Drawing.Size(224, 23);
this.txtTeacherPic.TabIndex = 1;
this.txtTeacherPic.Text = "";
//
// lblTeacherPic
//
this.lblTeacherPic.AutoSize = true;
this.lblTeacherPic.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lblTeacherPic.Location = new System.Drawing.Point(24, 24);
this.lblTeacherPic.Name = "lblTeacherPic";
this.lblTeacherPic.Size = new System.Drawing.Size(105, 22);
this.lblTeacherPic.TabIndex = 0;
this.lblTeacherPic.Text = "教师封面路径";
//
// lblTeacher
//
this.lblTeacher.BackColor = System.Drawing.SystemColors.HighlightText;
this.lblTeacher.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.lblTeacher.Font = new System.Drawing.Font("隶书", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lblTeacher.Location = new System.Drawing.Point(40, 136);
this.lblTeacher.Name = "lblTeacher";
this.lblTeacher.Size = new System.Drawing.Size(128, 112);
this.lblTeacher.TabIndex = 8;
this.lblTeacher.Text = "图书名称";
this.lblTeacher.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// frmPicture
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(392, 273);
this.Controls.Add(this.lblTeacher);
this.Controls.Add(this.picTeacher);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnUpdate);
this.Controls.Add(this.btnChange);
this.Controls.Add(this.txtTeacherPic);
this.Controls.Add(this.lblTeacherPic);
this.MaximizeBox = false;
this.Name = "frmPicture";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "图书封面查看";
this.Load += new System.EventHandler(this.frmPicture_Load);
this.ResumeLayout(false);
}
#endregion
//窗体加载事件
private void frmPicture_Load(object sender, System.EventArgs e)
{
//建立教师实体
teacher = new Teacher(teaID);
lblTeacher.Text = "教师编号:" + teacher.TeaID.ToString() + "\n姓名:\n" + teacher.TeaName;
TeaOldPic = teacher.TeaPicture;
if( TeaOldPic != "")
{
txtTeacherPic.Text = TeaOldPic;
try
{
picTeacher.Image = Image.FromFile(TeaOldPic);;
}
catch(Exception ex)
{
MessageBox.Show("路径" + TeaOldPic + "错误\n该路径下无法打开图片!","无效路径",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
//订阅“邮递员”
private void Post_EventSendLoginedID(object obj, object teacher, string formName)
{
teaID = (int)obj;
if(teacher.GetType().ToString().Equals("BusinessRule.AdminTeacher"))
{
this.Ateacher = (AdminTeacher)teacher;
}
else if(teacher.GetType().ToString().Equals("BusinessRule.MasterTeacher"))
{
this.Mteacher = (MasterTeacher)teacher;
}
else
{
this.Pteacher = (PrelectTeacher)teacher;
}
Post.EventSendLoginedID -= new DataAccess.Post.SendMsgDelegate(Post_EventSendLoginedID);
}
//获得实例化好的用户实例
public Teacher User
{
get
{
if(Ateacher != null)
{
return Ateacher;
}
else if(Mteacher != null)
{
return Mteacher;
}
else
{
return Pteacher;
}
}
}
//“修改封面”按钮
private void btnChange_Click(object sender, System.EventArgs e)
{
openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "JPEG and GIF Images(*.jpg;*.gif)|*.jpg;*.gif";
if( openFileDialog.ShowDialog() == DialogResult.OK )
{
txtTeacherPic.Text = openFileDialog.FileName;
picTeacher.Image = Image.FromFile(txtTeacherPic.Text);
}
}
//“更新封面”按钮
private void btnUpdate_Click(object sender, System.EventArgs e)
{
try
{
teacher.TeaPicture = txtTeacherPic.Text.Trim();
Ateacher.UpdateTeacherInfo(teacher);
MessageBox.Show("数据库更新成功!\n教师图片已经保存","成功",
MessageBoxButtons.OK,MessageBoxIcon.Information);
this.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + "\n数据库更新失败!","失败",
MessageBoxButtons.OK,MessageBoxIcon.Error);
//数据回滚
teacher.TeaPicture = TeaOldPic;
}
}
//“取消”按钮
private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -