C#實現(xiàn)銷售管理系統(tǒng)
C#制作簡易的的銷售管理系統(tǒng),供大家參考,具體內(nèi)容如下
1.整體需求
1).具有簡易的登錄界面
2).能對商品信息進行快速查看、查詢、添加、編輯、保存等功能。
2.設(shè)計的窗體界面
1).登錄界面
2).商品信息的操作界面
3.所需的知識
1).C#基礎(chǔ)語法
2).ADO.NET數(shù)據(jù)庫
不太清楚的可以去看我主頁的文章,都是關(guān)于C#基礎(chǔ)的知識。
4.具體步驟及代碼
1).創(chuàng)建項目
首先打開vs2017,選擇“創(chuàng)建項目” ,選擇“Windows窗體應(yīng)用”。詳細的操作 可以看我之前寫的一些簡單項目。
2).添加控件
登錄界面和商品信息界面如下:
可以試著根據(jù)圖片顯示的去添加控件,詳情見主頁的C#Windows窗體應(yīng)用設(shè)計系列。商品信息界面最上面是一個tool strip 控件。后面會把源碼發(fā)出來,邊參考源碼編寫可以對C#的設(shè)計更加清楚。
3).添加代碼
需要添加的代碼如下,添加代碼的方法見主頁的文章介紹。
登錄界面:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace EMS { public partial class frmLogin : Form { BaseClass.BaseInfo baseinfo = new EMS.BaseClass.BaseInfo(); BaseClass.cPopedom popedom = new EMS.BaseClass.cPopedom(); public frmLogin() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { if (txtUserName.Text == string.Empty) { MessageBox.Show("用戶名稱不能為空!", "錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } DataSet ds = null; popedom.SysUser = txtUserName.Text; popedom.Password = txtUserPwd.Text; ds=baseinfo.Login(popedom); if (ds.Tables[0].Rows.Count > 0) { EMS.BaseInfo.frmStock frm_Stock = new EMS.BaseInfo.frmStock(); frm_Stock.Show(); } else { MessageBox.Show("用戶名稱或密碼不正確!","錯誤提示",MessageBoxButtons.OK,MessageBoxIcon.Error); } } private void txtUserName_KeyUp(object sender, KeyEventArgs e) { if (e.KeyValue == 13) //判斷是否按下Enter鍵 txtUserPwd.Focus();//將鼠標焦點移動到“密碼”文本框 } private void txtUserPwd_KeyUp(object sender, KeyEventArgs e) { if (e.KeyValue == 13)//判斷是否按下Enter鍵 btnLogin.Focus();//將鼠標焦點移動到“登錄”按鈕 } private void btnExit_Click(object sender, EventArgs e) { this.Close(); } } }
商品主界面的代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace EMS.BaseInfo { public partial class frmStock : Form { BaseClass.BaseInfo baseinfo = new EMS.BaseClass.BaseInfo();//創(chuàng)建BaseInfo類的對象 BaseClass.cStockInfo stockinfo = new EMS.BaseClass.cStockInfo();//創(chuàng)建cStockInfo類的對象 int G_Int_addOrUpdate = 0;//定義添加/修改操作標識 public frmStock() { InitializeComponent(); } private void tlBtnAdd_Click(object sender, EventArgs e) { this.editEnabled();//設(shè)置各個控件的可用狀態(tài) this.clearText();//清空文本框 G_Int_addOrUpdate = 0;//等于0為添加數(shù)據(jù) DataSet ds = null;//創(chuàng)建數(shù)據(jù)集對象 string P_Str_newTradeCode = "";//設(shè)置庫存商品編號為空 int P_Int_newTradeCode = 0;//初始化商品編號中的數(shù)字碼 ds = baseinfo.GetAllStock("tb_stock");//獲取庫存商品信息 if (ds.Tables[0].Rows.Count == 0)//判斷數(shù)據(jù)集中是否有值 { txtTradeCode.Text = "T1001";//設(shè)置默認商品編號 } else { P_Str_newTradeCode = Convert.ToString(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["tradecode"]);//獲取已經(jīng)存在的最大編號 P_Int_newTradeCode = Convert.ToInt32(P_Str_newTradeCode.Substring(1, 4)) + 1;//獲取一個最新的數(shù)字碼 P_Str_newTradeCode = "T" + P_Int_newTradeCode.ToString();//獲取最新商品編號 txtTradeCode.Text = P_Str_newTradeCode;//將商品編號顯示在文本框中 } } //設(shè)置各按鈕的可用狀態(tài) private void editEnabled() { groupBox1.Enabled = true; tlBtnAdd.Enabled = false; tlBtnEdit.Enabled = false; tlBtnDelete.Enabled = false; tlBtnSave.Enabled = true; tlBtnCancel.Enabled = true; } //設(shè)置各按鈕的可用狀態(tài) private void cancelEnabled() { groupBox1.Enabled = false; tlBtnAdd.Enabled = true; tlBtnEdit.Enabled = true; tlBtnDelete.Enabled = true; tlBtnSave.Enabled = false; tlBtnCancel.Enabled = false; } //清空文本框 private void clearText() { txtTradeCode.Text= string.Empty; txtFullName.Text = string.Empty; txtType.Text = string.Empty; txtStandard.Text = string.Empty; txtUnit.Text = string.Empty; txtProduce.Text = string.Empty; } //設(shè)置DataGridView列標題 private void SetdgvStockListHeadText() { dgvStockList.Columns[0].HeaderText = "商品編號"; dgvStockList.Columns[1].HeaderText = "商品名稱"; dgvStockList.Columns[2].HeaderText = "商品型號"; dgvStockList.Columns[3].HeaderText = "商品規(guī)格"; dgvStockList.Columns[4].HeaderText = "商品單位"; dgvStockList.Columns[5].HeaderText = "商品產(chǎn)地"; dgvStockList.Columns[6].HeaderText = "庫存數(shù)量"; dgvStockList.Columns[7].Visible = false; dgvStockList.Columns[8].HeaderText = "商品價格(加權(quán)平均價格)"; dgvStockList.Columns[9].Visible = false; dgvStockList.Columns[10].HeaderText = "盤點數(shù)量"; dgvStockList.Columns[11].Visible = false; dgvStockList.Columns[12].Visible = false; } private void frmStock_Load(object sender, EventArgs e) { txtTradeCode.ReadOnly = true;//設(shè)置商品編號文本框只讀 this.cancelEnabled();//設(shè)置各按鈕的可用狀態(tài) //顯示所有庫存商品信息 dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView; this.SetdgvStockListHeadText();//設(shè)置DataGridView控件的列標題 } private void tlBtnSave_Click(object sender, EventArgs e) { //判斷是添加還是修改數(shù)據(jù) if (G_Int_addOrUpdate == 0) { try { //添加數(shù)據(jù) stockinfo.TradeCode = txtTradeCode.Text; stockinfo.FullName = txtFullName.Text; stockinfo.TradeType = txtType.Text; stockinfo.Standard = txtStandard.Text; stockinfo.Unit = txtUnit.Text; stockinfo.Produce = txtProduce.Text; //執(zhí)行添加操作 int id = baseinfo.AddStock(stockinfo); MessageBox.Show("新增--庫存商品數(shù)據(jù)--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.Message,"錯誤提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { //修改數(shù)據(jù) stockinfo.TradeCode = txtTradeCode.Text; stockinfo.FullName = txtFullName.Text; stockinfo.TradeType = txtType.Text; stockinfo.Standard = txtStandard.Text; stockinfo.Unit = txtUnit.Text; stockinfo.Produce = txtProduce.Text; //執(zhí)行修改操作 int id = baseinfo.UpdateStock(stockinfo); MessageBox.Show("修改--庫存商品數(shù)據(jù)--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); } dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;//顯示最新的庫存商品信息 this.SetdgvStockListHeadText();//設(shè)置DataGridView控件列標題 this.cancelEnabled();//設(shè)置各個按鈕的可用狀態(tài) } private void tlBtnEdit_Click(object sender, EventArgs e) { this.editEnabled();//設(shè)置各個按鈕的可用狀態(tài) G_Int_addOrUpdate = 1;//等于1為修改數(shù)據(jù) } private void tlBtnFind_Click(object sender, EventArgs e) { if (tlCmbStockType.Text == string.Empty)//判斷查詢類別是否為空 { MessageBox.Show("查詢類別不能為空!", "錯誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Error); tlCmbStockType.Focus();//使查詢類別下拉列表獲得鼠標焦點 return; } else { if (tlTxtFindStock.Text.Trim() == string.Empty)//判斷查詢關(guān)鍵字是否為空 { //顯示所有庫存商品信息 dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView; this.SetdgvStockListHeadText();//設(shè)置DataGridView控件的列標題 return; } } DataSet ds = null;//創(chuàng)建DataSet對象 if (tlCmbStockType.Text == "商品產(chǎn)地") //按商品產(chǎn)地查詢 { stockinfo.Produce = tlTxtFindStock.Text;//記錄商品產(chǎn)地 ds = baseinfo.FindStockByProduce(stockinfo, "tb_Stock");//根據(jù)商品產(chǎn)地查詢商品信息 dgvStockList.DataSource = ds.Tables[0].DefaultView;//顯示查詢到的信息 } else//按商品名稱查詢 { stockinfo.FullName = tlTxtFindStock.Text;//記錄商品名稱 ds = baseinfo.FindStockByFullName(stockinfo, "tb_stock");//根據(jù)商品名稱查詢商品信息 dgvStockList.DataSource = ds.Tables[0].DefaultView;//顯示查詢到的信息 } this.SetdgvStockListHeadText();//設(shè)置DataGridView控件列標題 } private void tlBtnDelete_Click(object sender, EventArgs e) { if (txtTradeCode.Text.Trim() == string.Empty)//判斷是否選擇了商品編號 { MessageBox.Show("刪除--庫存商品數(shù)據(jù)--失??!", "錯誤提示!", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } stockinfo.TradeCode = txtTradeCode.Text;//記錄商品編號 int id = baseinfo.DeleteStock(stockinfo);//執(zhí)行刪除操作 MessageBox.Show("刪除--庫存商品數(shù)據(jù)--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information); dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;//顯示最新的庫存商品信息 this.SetdgvStockListHeadText();//設(shè)置DataGridView控件列標題 this.clearText();//清空文本框 } private void tlBtnCancel_Click(object sender, EventArgs e) { this.cancelEnabled();//設(shè)置各個按鈕的可用狀態(tài) } private void dgvStockList_CellClick(object sender, DataGridViewCellEventArgs e) { txtTradeCode.Text = this.dgvStockList[0, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品編號 txtFullName.Text = this.dgvStockList[1, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品全稱 txtType.Text = this.dgvStockList[2, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品型號 txtStandard.Text = this.dgvStockList[3, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品規(guī)格 txtUnit.Text = this.dgvStockList[4, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品單位 txtProduce.Text = this.dgvStockList[5, dgvStockList.CurrentCell.RowIndex].Value.ToString();//顯示商品產(chǎn)地 } private void tlBtnExit_Click(object sender, EventArgs e) { this.Close();//關(guān)閉當前窗體 } private void dgvStockList_CellContentClick(object sender, DataGridViewCellEventArgs e) { } } }
Main.cs
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace EMS { static class Program { /// <summary> /// 應(yīng)用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmLogin()); } } }
需要添加的圖片素材在源碼文件夾的icon和image文件夾里面,覺得不好看的可以自行查找。
注意,添加代碼時要與自己的添加的控件一一對應(yīng)起來。
4).建立數(shù)據(jù)庫
數(shù)據(jù)庫具體的添加方法在我主頁的文章《一起來學C#之數(shù)據(jù)庫》中講過,在菜單欄中的“項目”-》》“添加新項目”-》》“基于服務(wù)的數(shù)據(jù)庫”,具體操作可以看我前面的文章。本次給出的源碼有數(shù)據(jù)庫,可以自行修改添加。
5).調(diào)試運行
根據(jù)自己所寫的去運行,再對照提供的源碼修改錯誤,運行界面如下。
登錄界面:
登錄賬戶名:mr 密碼:mrsoft,可以根據(jù)源碼自行修改。
信息界面:
寫好運行后就可以看到該界面。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)日期格式轉(zhuǎn)換的公共方法類實例
這篇文章主要介紹了C#實現(xiàn)日期格式轉(zhuǎn)換的公共方法類,結(jié)合完整實例形式分析了C#針對各種常見日期格式的轉(zhuǎn)換方法,涉及C#字符串、日期、時間相關(guān)操作技巧,需要的朋友可以參考下2017-01-01C#操作Clipboard讀取剪切板中數(shù)據(jù)實例詳解
這篇文章主要介紹了C#操作Clipboard讀取剪切板中數(shù)據(jù)的方法,實例分析了C#讀取剪貼板數(shù)據(jù)的具體步驟與實現(xiàn)技巧,需要的朋友可以參考下2015-05-05C#實現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法
應(yīng)人才測評產(chǎn)品的需求,導(dǎo)出測評報告是其中一個重要的環(huán)節(jié),報告的文件類型也多種多樣,其中WORD輸出也扮演了一個重要的角色,本文給大家介紹了C#實現(xiàn)數(shù)據(jù)導(dǎo)出任一Word圖表的通用呈現(xiàn)方法及一些體會,需要的朋友可以參考下2023-10-10