实例介绍
【实例简介】简单汽车修理厂收费系统
【实例截图】
【核心代码】
using CYQ.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WXPayer
{
public partial class frmMain : CCWin.CCSkinMain
{
public frmMain()
{
InitializeComponent();
this.Icon = Resource1.bitbug_favicon;
}
private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
{
//Application.Exit();
}
List<Model.ChargePartsItem> _partslist = null;
List<Model.ChargeServerItem> _serverlist = null;
private void frmMain_Load(object sender, EventArgs e)
{
dgvServer.AutoGenerateColumns = true;
string payerprinter = Common.Setting.Get("payerprinter", "Printer", "");
if (string.IsNullOrEmpty(payerprinter))
{
MessageBox.Show("未设置打印机");
}
txtYouhui.KeyPress = new KeyPressEventHandler(control_KeyPress);
txtYouhui.Leave = new EventHandler(control_Leave);
txtYouhui.TextChanged = new EventHandler(control_TextChanged);
Reset();
}
private void Reset()
{
_partslist = new List<Model.ChargePartsItem>();
_serverlist = new List<Model.ChargeServerItem>();
dgvServer.Rows.Clear();
dgvPart.Rows.Clear();
skinTabControl1.SelectedIndex = 0;
txtCarNo.Text = txtCustomName.Text = txtJiedai.Text = txtPhone.Text = "";
txtRealPrice.Text = txtTotal.Text = txtYouhui.Text = "0.00";
}
TextBox control;
private void control_KeyPress(object sender, KeyPressEventArgs e)
{
//限制只能输入-9的数字,退格键,小数点和回车
if (((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57) || e.KeyChar == 13 || e.KeyChar == 8 || e.KeyChar == 46)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
private void control_Leave(object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
if (txt.Text == "")
{
txt.Text = "0.00";
}
CountCost();
}
public DataGridViewTextBoxEditingControl CellEdit = null;
private void dgvServer_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
if (dgv.CurrentCellAddress.X == 1 || dgv.CurrentCellAddress.X == 2)//获取当前处于活动状态的单元格索引
{
CellEdit = (DataGridViewTextBoxEditingControl)e.Control;
CellEdit.SelectAll();
CellEdit.KeyPress = control_KeyPress;
}
//if (dgv.CurrentCell.ColumnIndex == 1 || dgv.CurrentCell.ColumnIndex == 2)
//{
// control = new TextBox();
// control = (TextBox)e.Control;
// //if (control.Text == "0") //需要限制输入数字的单元格
// //{
// control.KeyPress = new KeyPressEventHandler(control_KeyPress);
// control.TextChanged = new EventHandler(control_TextChanged);
// //}
// //else
// //{
// // //非数字类型单元格
// // control.Leave = new EventHandler(control_Leave);
// //}
//}
}
private void dgvServer_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
{
return;
}
if (e.ColumnIndex == 0)
{
var ss = new frmServerList();
if (ss.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ServerName = ss.model.ServerName;
bool IshasEmptyRow = false;
int level = -1;
foreach (DataGridViewRow row in dgvServer.Rows)
{
level ;
if (row.Cells[0].Value != null && row.Cells[0].Value.ToString() == ServerName)
{
MessageBox.Show("服务项目重复");
return;
}
if (e.RowIndex != level && (row.Cells[0].Value == null || row.Cells[0].Value.ToString() == ""))
{
IshasEmptyRow = true;
}
}
if (!IshasEmptyRow)
{
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dgvServer);
dr.Cells[0].Value = ServerName;
dr.Cells[1].Value = ss.model.Cost;
int quantity = 1;
if (dgvServer.Rows[e.RowIndex].Cells[2].Value != null)
{
int.TryParse(dgvServer.Rows[e.RowIndex].Cells[2].Value.ToString(), out quantity);
}
dr.Cells[3].Value = ss.model.Cost * quantity;
dgvServer.Rows.Insert(dgvServer.Rows.Count - 1, dr);
}
else
{
dgvServer.CurrentCell.Value = ServerName;
dgvServer.Rows[e.RowIndex].Cells[1].Value = ss.model.Cost;
int quantity = 1;
if (dgvServer.Rows[e.RowIndex].Cells[2].Value != null)
{
int.TryParse(dgvServer.Rows[e.RowIndex].Cells[2].Value.ToString(), out quantity);
}
dgvServer.Rows[e.RowIndex].Cells[3].Value = ss.model.Cost * quantity;
}
CountCost();
}
}
}
private void btnClear_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (MessageBox.Show("确定要清空吗?", "温馨提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
{
if (skinTabControl1.SelectedIndex == 0)
{
dgvServer.Rows.Clear();
}
else
{
dgvPart.Rows.Clear();
}
}
}
/// <summary>
/// 计算金额
/// </summary>
private void CountCost()
{
decimal total = 0;
foreach (DataGridViewRow dr in dgvServer.Rows)
{
if (dr.Cells[1].Value == null)
{
break;
}
decimal price = decimal.Parse(dr.Cells[1].Value.ToString());
string a = "1";
if (dr.Cells[2].Value != null)
{
a = dr.Cells[2].Value.ToString();
}
decimal quantity = decimal.Parse(a);
total = (price * quantity);
}
foreach (DataGridViewRow dr in dgvPart.Rows)
{
if (dr.Cells[1].Value == null)
{
break;
}
decimal price = decimal.Parse(dr.Cells[1].Value.ToString());
string a = "1";
if (dr.Cells[2].Value != null)
{
a = dr.Cells[2].Value.ToString();
}
decimal quantity = decimal.Parse(a);
total = (price * quantity);
}
txtTotal.Text = total.ToString("#0.00");
txtRealPrice.Text = "0.00";
if (!string.IsNullOrEmpty(txtYouhui.Text.Trim()))
{
decimal youhui = decimal.Parse(txtYouhui.Text.Trim());
decimal realprice = total - youhui;
txtRealPrice.Text = realprice.ToString("#0.00");
}
}
private void control_TextChanged(object sender, EventArgs e)
{
CountCost();
}
private void dgvServer_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
{
return;
}
if (e.ColumnIndex == 0)
{
}
DataGridView dgv = (DataGridView)sender;
int quantity = 1;
if (dgv.Rows[e.RowIndex].Cells[2].Value != null)
{
int.TryParse(dgv.Rows[e.RowIndex].Cells[2].Value.ToString(), out quantity);
}
decimal cost = 0;
if (dgv.Rows[e.RowIndex].Cells[1].Value != null)
{
decimal.TryParse(dgv.Rows[e.RowIndex].Cells[1].Value.ToString(), out cost);
}
dgv.Rows[e.RowIndex].Cells[3].Value = cost * quantity;
CountCost();
}
private void dgvPart_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
{
return;
}
if (e.ColumnIndex == 0)
{
var ss = new frmPartList();
if (ss.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string PartName = ss.model.PartName;
bool IshasEmptyRow = false;
int level = -1;
foreach (DataGridViewRow row in dgvServer.Rows)
{
level ;
if (row.Cells[0].Value != null && row.Cells[0].Value.ToString() == PartName)
{
MessageBox.Show("配件重复");
return;
}
if (e.RowIndex != level && (row.Cells[0].Value == null || row.Cells[0].Value.ToString() == ""))
{
IshasEmptyRow = true;
}
}
if (!IshasEmptyRow)
{
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dgvServer);
dr.Cells[0].Value = ServerName;
dr.Cells[1].Value = ss.model.Cost;
int quantity = 1;
if (dgvPart.Rows[e.RowIndex].Cells[2].Value != null)
{
int.TryParse(dgvPart.Rows[e.RowIndex].Cells[2].Value.ToString(), out quantity);
}
dr.Cells[3].Value = ss.model.Cost * quantity;
dgvPart.Rows.Insert(dgvPart.Rows.Count - 1, dr);
}
else
{
dgvPart.CurrentCell.Value = ServerName;
dgvPart.Rows[e.RowIndex].Cells[1].Value = ss.model.Cost;
int quantity = 1;
if (dgvPart.Rows[e.RowIndex].Cells[2].Value != null)
{
int.TryParse(dgvPart.Rows[e.RowIndex].Cells[2].Value.ToString(), out quantity);
}
dgvPart.Rows[e.RowIndex].Cells[3].Value = ss.model.Cost * quantity;
}
CountCost();
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
btnSave.Enabled = false;
btnSave.Text = "提交中...";
Model.Charges charge = new Model.Charges();
charge.CustomName = txtCustomName.Text.Trim();
charge.Phone = txtPhone.Text.Trim();
charge.CarNo = txtCarNo.Text.Trim();
charge.JieDaiName = txtJiedai.Text.Trim();
charge.JieDaiTime = txtJiedaiDate.Text txtJiedaiTime.Text;
charge.RealPrice = decimal.Parse(txtRealPrice.Text);
charge.Total = decimal.Parse(txtTotal.Text);
charge.Youhui = decimal.Parse(txtYouhui.Text);
charge.Remark = txtRemark.Text;
if (string.IsNullOrEmpty(charge.CustomName))
{
MessageBox.Show("客户名称不能为空");
btnSave.Text = "提 交";
btnSave.Enabled = true;
return;
}
if (dgvServer.Rows.Count == 0 && dgvPart.Rows.Count == 0)
{
MessageBox.Show("请添加服务或配件");
btnSave.Text = "提 交";
btnSave.Enabled = true;
return;
}
using (MAction action = new MAction("Charges", Model.Conn.Access))
{
bool result = false;
action.SetTransLevel(IsolationLevel.ReadCommitted);//可设置的事务级别,一般可以不用设置
action.BeginTransation();//设置开启事务标识
action.Set("CustomName", charge.CustomName);
action.Set("Phone", charge.Phone);
action.Set("CarNo", charge.CarNo);
action.Set("JieDaiName", charge.JieDaiName);
action.Set("JieDaiTime", charge.JieDaiTime);
action.Set("RealPrice", charge.RealPrice);
action.Set("Total", charge.Total);
action.Set("Youhui", charge.Youhui);
action.Set("Remark", charge.Remark);
action.Set("CreateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
int id = 0;
if (action.Insert())//第一个执行时,事务才被加载
{
id = action.Get<int>("ID");
action.ResetTable("ChargeServerItem");
foreach (DataGridViewRow dr in dgvServer.Rows)
{
if (dr.Cells[0].Value == null)
{
break;
}
action.Set("ChargeID", id);
action.Set("ServerName", dr.Cells[0].Value.ToString());
action.Set("Cost", dr.Cells[1].Value);
string quantity = "0";
if (dr.Cells[2].Value != null)
{
quantity = dr.Cells[2].Value.ToString();
}
action.Set("Quantity", (quantity == "0" ? "1" : quantity));
result = action.Insert(InsertOp.None);
}
action.ResetTable("ChargePartsItem");
foreach (DataGridViewRow dr in dgvPart.Rows)
{
if (dr.Cells[0].Value == null)
{
break;
}
action.Set("ChargeID", id);
action.Set("PartName", dr.Cells[0].Value.ToString());
action.Set("Cost", dr.Cells[1].Value);
string quantity = "0";
if (dr.Cells[2].Value != null)
{
quantity = dr.Cells[2].Value.ToString();
}
action.Set("Quantity", (quantity == "0" ? "1" : quantity));
result = action.Insert(InsertOp.None);
}
}
else
{
action.RollBack();//手工回滚
MessageBox.Show("操作失败", "温馨提示");
btnSave.Text = "提 交";
btnSave.Enabled = true;
return;
}
action.EndTransation();
var dialogresult = MessageBox.Show("操作成功!\n是:打印收费单\n否:不打印", "温馨提示", MessageBoxButtons.YesNo);
if (dialogresult == System.Windows.Forms.DialogResult.Yes)
{
//打印
Common.PrintOrder order = new Common.PrintOrder(id);
if (order.Printer())
{
MessageBox.Show("打印任务已发出");
}
else
{
MessageBox.Show(order.outmessage);
}
}
btnSave.Text = "提 交";
btnSave.Enabled = true;
Reset();
}
}
private void txtCarNo_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtCarNo.Text.Trim()))
{
txtCarNo.Text = "粤A";
txtCarNo.ScrollToCaret();
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)
支持(0) 盖楼(回复)