C# winform开发一般开发格式
public partial class TestTemplate : Form
{
private static string path = "";
private static companys _com = null;
public EditCompany()
{
path = Img.path;
InitializeComponent();
GetData();//数据绑定
InitGroup();//初始编辑栏
}
#region 一、界面
/// <summary>
/// GroupBox重绘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GroupBox_Paint(object sender, PaintEventArgs e)
{
if (sender != null && sender is GroupBox)
{
System.Drawing.Graphics g = e.Graphics;
GroupBox gbx = sender as GroupBox;
//重绘圆角、颜色
GroupBoxEx.PaintBack(g, gbx);
}
}
/// <summary>
/// 图片重读
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
{
if (DataGridView1.Columns[e.ColumnIndex].Name.Equals("logo"))
{
if (string.IsNullOrEmpty(e.Value.ToString()))//空
{
e.Value = Img.noimg();
}
else
{
if (File.Exists(path + e.Value))
{
e.Value = Image.FromFile(path + e.Value);
}
else
{
e.Value = Img.noimg();
}
}
}
if (DataGridView1.Columns[e.ColumnIndex].Name.Equals("contacts"))
{
e.Value = e.Value.ToString().Trim();//nchar去空格
}
if (DataGridView1.Columns[e.ColumnIndex].Name.Equals("code"))
{
e.Value = e.Value.ToString().Trim();//char去空格
}
}
}
catch (Exception ex)
{
}
}
#endregion
#region 二、逻辑
/// <summary>
/// 上传图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnimg_Click(object sender, EventArgs e)
{
if (btnimg.Text == "选择")
{
Image _img = Img.LoadImg();
InitImg();
if (_img != null)
{
btnimg.Text = "上传";
picbox2.Image = _img;
}
}
else if (btnimg.Text == "上传")
{
laburl.Text = Img.SaveImg(picbox2);
picbox1.Image = Image.FromFile(path + laburl.Text);
if (string.IsNullOrEmpty(laburl.Text)) InitGroup();
btnimg.Text = "选择";
}
}
/// <summary>
/// 移除图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnrimg_Click(object sender, EventArgs e)
{
InitImg();
}
/// <summary>
/// 事件-删除、修改点击
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))
{
//说明点击的列是DataGridViewButtonColumn列
DataGridViewRow Row = DataGridView1.Rows[e.RowIndex];
int index_id = 0;
int index_name = 0;
//查看哪一列是ID
for (int i = 0; i < Row.Cells.Count; i++)
{
if (DataGridView1.Columns[i].Name == "ID") index_id = i;
if (DataGridView1.Columns[i].Name == "name") index_name = i;
}
//选中-->到编辑区
if (DataGridView1.Columns[e.ColumnIndex].Name == "UpCol" && e.RowIndex >= 0)
{
int id = Int32.Parse(Row.Cells[index_id].Value.ToString());
string name = Row.Cells[index_name].Value.ToString();
UpGroup(id, name);//状态切换
}
//删除
if (DataGridView1.Columns[e.ColumnIndex].Name == "DelCol" && e.RowIndex >= 0)
{
int id = Int32.Parse(Row.Cells[index_id].Value.ToString());
Del_Step(id); //执行删除
}
}
}
/// <summary>
/// Group初始化新增
/// </summary>
private void InitGroup()
{
lab3.Text = "新增";
lab3.ForeColor = Color.Green;
btn_submit.Text = "添加";
btn_submit.BackColor = Color.Green;
tbname.Text = "";
tbaddr.Text = "";
tbtel.Text = "";
tbper.Text = "";
labid.Text = "0";//tbsearch
tbsearch.Text = "";
tbemail.Text = "";
tbspec.Text = "";
tbnotes.Text = "";
tbtax.Text = "";
laburl.Text = "";
_com = new companys();
InitImg();
}
private void InitImg()
{
btnimg.Visible = true;
laburl.Text = "";
btnimg.Text = "选择";
//默认图片
picbox1.Image = Img.noimg();
btn_submit.BackColor = Color.Green;
btn_submit.ForeColor = Color.White;
}
/// <summary>
/// Group转变为修改
/// </summary>
private void UpGroup(int id, string name)
{
lab3.Text = string.Format("修改:{0}", name);
lab3.ForeColor = Color.DodgerBlue;
btn_submit.Text = "修改";
btn_submit.BackColor = Color.DodgerBlue;
labid.Text = id.ToString();//tbsearch
//获取该条数据
_com = DataApi.GetModel<companys>(id);
outModel(_com);
}
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_cancel_Click(object sender, EventArgs e)
{
InitGroup();
}
/// <summary>
/// 提交编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_submit_Click(object sender, EventArgs e)
{
if (ValueFormat())
{
string name = tbname.Text.ToString().Trim().ToUpper();
if (_com.id == 0) Add_Step();//添加
else
{
companys m = CtrstM.DeepCopy(_com);//深度复制
ForModel(ref m);//获取页面值
if (CtrstM.ForCheck(m, _com)) Msg.ParamAsis();//无更改验证
else
Up_Step(m);//修改
}
}
}
/// <summary>
/// 提交验证参数正确
/// </summary>
/// <returns></returns>
private bool ValueFormat()
{
bool rst = true;
if (string.IsNullOrEmpty(tbname.Text))
{
Msg.ParamNull("公司名"); return false;
}
return rst;
}
/// <summary>
/// 实体赋值
/// </summary>
/// <returns></returns>
private void ForModel(ref companys com)
{
com.name = tbname.Text.Trim().ToUpper();
com.addr = tbaddr.Text.Trim().ToUpper();
com.tel = tbtel.Text.Trim().ToUpper();
com.mobile = tbmobile.Text.Trim().ToUpper();
com.contacts = tbper.Text.Trim().ToUpper();
com.code = tbcode.Text.Trim().ToUpper();
com.email = tbemail.Text.Trim().ToUpper();
com.Profile = tbspec.Text.Trim();
com.notes = tbnotes.Text.Trim();
com.tax = tbtax.Text.Trim().ToString();
com.logo = laburl.Text.Trim();
}
/// <summary>
/// 实体读取
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
private companys outModel(companys com)
{
tbname.Text = com.name.Trim().ToUpper();
tbaddr.Text = com.addr.Trim().ToUpper();
tbtel.Text = com.tel.Trim().ToUpper();
tbmobile.Text = com.mobile.Trim().ToUpper();
tbper.Text = com.contacts.Trim().ToUpper();
tbcode.Text = com.code.Trim().ToUpper();
tbemail.Text = com.email.Trim().ToUpper();
tbspec.Text = com.Profile.Trim();
tbnotes.Text = com.notes.Trim();
tbtax.Text = com.tax.Trim().ToString();
if (string.IsNullOrEmpty(com.logo))
{
picbox1.Image = Img.noimg();
}
else
{
string imgFile = path + com.logo;
if(File.Exists(imgFile))
{
picbox1.Image = Image.FromFile(imgFile);
}
else
{
picbox1.Image = Img.noimg();
}
laburl.Text = com.logo.Trim();
}
return com;
}
/// <summary>
/// 电话只能输入数字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbtel_KeyPress(object sender, KeyPressEventArgs e)
{
KeyEvent.ForTel(sender, e);
}
/// <summary>
/// 简称 数字/字母
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tbcode_KeyPress(object sender, KeyPressEventArgs e)
{
KeyEvent.ForNumLett(sender, e);
}
private void tbmobile_KeyPress(object sender, KeyPressEventArgs e)
{
KeyEvent.ForNum(sender, e);
}
#endregion
#region 三、数据交互
/// <summary>
/// 数据-初始化
/// </summary>
private void GetData()
{
//GridView外观设置
GridViewInit.main(DataGridView1);
//绑定数据
DataGridView1.DataSource = DataApi.GetDatas<companys>();//调用全部查询
DataGridView1.RowTemplate.Height = 50;
}
/// <summary>
/// 查询-模糊
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string Str = tbsearch.Text;
if (Str.Length == 0) DataGridView1.DataSource = DataApi.GetDatas<companys>();//调用全部查询
else
DataGridView1.DataSource = DataApi.LikeDatas<companys>(Str);//调用模糊查询
}
/// <summary>
/// 删除客户
/// </summary>
/// <param name="id"></param>
private void Del_Step(int id)
{
int type = (int)Msg.mode.del;
DialogResult drResult = Msg.Confirm(type);
//执行删除
if (drResult == DialogResult.Yes)
{
if (DataApi.Del<companys>(id)) { GetData(); Msg.RstYes(type); }
else
Msg.RstNo(type);
}
InitGroup();
}
/// <summary>
/// 修改
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
private void Up_Step(companys m)
{
int type = (int)Msg.mode.update;
DialogResult drResult = Msg.Confirm(type);
//执行修改
if (drResult == DialogResult.Yes)
{
if (DataApi.UpdateByName<companys>(m)) { GetData(); Msg.RstYes(type); }
else
Msg.RstFalse("公司名或简称");
}
InitGroup();
}
/// <summary>
/// 新增
/// </summary>
/// <param name="name"></param>
private void Add_Step()
{
int type = (int)Msg.mode.add;
companys m = new companys();
ForModel(ref m);
if (DataApi.AddByName<companys>(m)) { GetData(); Msg.RstYes(type); }
else
Msg.RstFalse("公司名或简称");
InitGroup();
}
#endregion
#region 四、缓存-内存回收GC
/// <summary>
/// 缓存-内存回收GC
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EditCompany_FormClosing(object sender, FormClosingEventArgs e)
{
path = null;
_com = null;
}
#endregion
private void tbmobile_TextChanged(object sender, EventArgs e)
{
}
}
查看更多关于C# winform开发一般开发格式的详细内容...