C#案例应用(一)-----------销售管理系统

C#制作简易的的销售管理系统

1.整体需求

1.具有简易的登录界面
2.能对商品信息进行快速查看、查询、添加、编辑、保存等功能。

2.设计的窗体界面

1.登录界面
2.商品信息的操作界面

3.所需的知识

 1.C#基础语法
 2.ADO.NET数据库

不太清楚的可以去看我主页的文章,都是关于C#基础的知识。

4.具体步骤及代码

1.创建项目

首先打开vs2017,选择“创建项目” ,选择“Windows窗体应用”。详细的操作 可以看我之前写的一些简单项目。

2.添加控件

登录界面和商品信息界面如下:
在这里插入图片描述
在这里插入图片描述
可以试着根据图片显示的去添加控件,详情见主页的C#Windows窗体应用设计系列。商品信息界面最上面是一个tool strip 控件。后面会把源码发出来,边参考源码编写可以对C#的设计更加清楚。

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();//创建BaseInfo类的对象
        BaseClass.cStockInfo stockinfo = new EMS.BaseClass.cStockInfo();//创建cStockInfo类的对象
        int G_Int_addOrUpdate = 0;//定义添加/修改操作标识
        public frmStock()
        {
            InitializeComponent();
        }

        private void tlBtnAdd_Click(object sender, EventArgs e)
        {
            this.editEnabled();//设置各个控件的可用状态
            this.clearText();//清空文本框
            G_Int_addOrUpdate = 0;//等于0为添加数据
            DataSet ds = null;//创建数据集对象
            string P_Str_newTradeCode = "";//设置库存商品编号为空
            int P_Int_newTradeCode = 0;//初始化商品编号中的数字码
            ds = baseinfo.GetAllStock("tb_stock");//获取库存商品信息
            if (ds.Tables[0].Rows.Count == 0)//判断数据集中是否有值
            {
                txtTradeCode.Text = "T1001";//设置默认商品编号
            }
            else
            {
                P_Str_newTradeCode = Convert.ToString(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1]["tradecode"]);//获取已经存在的最大编号
                P_Int_newTradeCode = Convert.ToInt32(P_Str_newTradeCode.Substring(1, 4)) + 1;//获取一个最新的数字码
                P_Str_newTradeCode = "T" + P_Int_newTradeCode.ToString();//获取最新商品编号
                txtTradeCode.Text = P_Str_newTradeCode;//将商品编号显示在文本框中
            }
        }
        //设置各按钮的可用状态
        private void editEnabled()
        {
            groupBox1.Enabled = true;
            tlBtnAdd.Enabled = false;
            tlBtnEdit.Enabled = false;
            tlBtnDelete.Enabled = false;
            tlBtnSave.Enabled = true;
            tlBtnCancel.Enabled = true;
        }
        //设置各按钮的可用状态
        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;
        }
        //设置DataGridView列标题
        private void SetdgvStockListHeadText() 
        {
            dgvStockList.Columns[0].HeaderText = "商品编号";
            dgvStockList.Columns[1].HeaderText = "商品名称";
            dgvStockList.Columns[2].HeaderText = "商品型号";
            dgvStockList.Columns[3].HeaderText = "商品规格";
            dgvStockList.Columns[4].HeaderText = "商品单位";
            dgvStockList.Columns[5].HeaderText = "商品产地";
            dgvStockList.Columns[6].HeaderText = "库存数量";
            dgvStockList.Columns[7].Visible = false;
            dgvStockList.Columns[8].HeaderText = "商品价格(加权平均价格)";
            dgvStockList.Columns[9].Visible = false;
            dgvStockList.Columns[10].HeaderText = "盘点数量";
            dgvStockList.Columns[11].Visible = false;
            dgvStockList.Columns[12].Visible = false;
        }

        private void frmStock_Load(object sender, EventArgs e)
        {
            txtTradeCode.ReadOnly = true;//设置商品编号文本框只读
            this.cancelEnabled();//设置各按钮的可用状态
            //显示所有库存商品信息
            dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;
            this.SetdgvStockListHeadText();//设置DataGridView控件的列标题
        }

        private void tlBtnSave_Click(object sender, EventArgs e)
        {
            //判断是添加还是修改数据
            if (G_Int_addOrUpdate == 0)
            {
                try
                {
                    //添加数据
                    stockinfo.TradeCode = txtTradeCode.Text;
                    stockinfo.FullName = txtFullName.Text;
                    stockinfo.TradeType = txtType.Text;
                    stockinfo.Standard = txtStandard.Text;
                    stockinfo.Unit = txtUnit.Text;
                    stockinfo.Produce = txtProduce.Text;
                    //执行添加操作
                    int id = baseinfo.AddStock(stockinfo);
                    MessageBox.Show("新增--库存商品数据--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,"错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                //修改数据
                stockinfo.TradeCode = txtTradeCode.Text;
                stockinfo.FullName = txtFullName.Text;
                stockinfo.TradeType = txtType.Text;
                stockinfo.Standard = txtStandard.Text;
                stockinfo.Unit = txtUnit.Text;
                stockinfo.Produce = txtProduce.Text;
                //执行修改操作
                int id = baseinfo.UpdateStock(stockinfo);
                MessageBox.Show("修改--库存商品数据--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;//显示最新的库存商品信息
            this.SetdgvStockListHeadText();//设置DataGridView控件列标题
            this.cancelEnabled();//设置各个按钮的可用状态
        }

        private void tlBtnEdit_Click(object sender, EventArgs e)
        {
            this.editEnabled();//设置各个按钮的可用状态
            G_Int_addOrUpdate = 1;//等于1为修改数据
        }

        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)//判断查询关键字是否为空
                {
                    //显示所有库存商品信息
                    dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;
                    this.SetdgvStockListHeadText();//设置DataGridView控件的列标题
                    return;
                }
            }
            DataSet ds = null;//创建DataSet对象
            if (tlCmbStockType.Text == "商品产地") //按商品产地查询
            {
                stockinfo.Produce = tlTxtFindStock.Text;//记录商品产地
                ds = baseinfo.FindStockByProduce(stockinfo, "tb_Stock");//根据商品产地查询商品信息
                dgvStockList.DataSource = ds.Tables[0].DefaultView;//显示查询到的信息
            }
            else//按商品名称查询
            {
                stockinfo.FullName = tlTxtFindStock.Text;//记录商品名称
                ds = baseinfo.FindStockByFullName(stockinfo, "tb_stock");//根据商品名称查询商品信息
                dgvStockList.DataSource = ds.Tables[0].DefaultView;//显示查询到的信息
            }
            this.SetdgvStockListHeadText();//设置DataGridView控件列标题
        }

        private void tlBtnDelete_Click(object sender, EventArgs e)
        {
            if (txtTradeCode.Text.Trim() == string.Empty)//判断是否选择了商品编号
            {
                MessageBox.Show("删除--库存商品数据--失败!", "错误提示!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            stockinfo.TradeCode = txtTradeCode.Text;//记录商品编号
            int id = baseinfo.DeleteStock(stockinfo);//执行删除操作
            MessageBox.Show("删除--库存商品数据--成功!", "成功提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dgvStockList.DataSource = baseinfo.GetAllStock("tb_stock").Tables[0].DefaultView;//显示最新的库存商品信息
            this.SetdgvStockListHeadText();//设置DataGridView控件列标题
            this.clearText();//清空文本框
        }

        private void tlBtnCancel_Click(object sender, EventArgs e)
        {
            this.cancelEnabled();//设置各个按钮的可用状态
        }

        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();//显示商品规格
            txtUnit.Text = this.dgvStockList[4, dgvStockList.CurrentCell.RowIndex].Value.ToString();//显示商品单位
            txtProduce.Text = this.dgvStockList[5, dgvStockList.CurrentCell.RowIndex].Value.ToString();//显示商品产地
        }

        private void tlBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();//关闭当前窗体
        }

        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>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin());
        }
    }
}

需要添加的图片素材在源码文件夹的icon和image文件夹里面,觉得不好看的可以自行查找。
注意,添加代码时要与自己的添加的控件一一对应起来。

4.建立数据库

数据库具体的添加方法在我主页的文章《一起来学C#之数据库》中讲过,在菜单栏中的“项目”-》》“添加新项目”-》》“基于服务的数据库”,具体操作可以看我前面的文章。本次给出的源码有数据库,可以自行修改添加。
在这里插入图片描述

5.调试运行

根据自己所写的去运行,再对照提供的源码修改错误,运行界面如下。
登录界面:在这里插入图片描述
登录账户名:mr 密码:mrsoft,可以根据源码自行修改。

信息界面:
在这里插入图片描述
写好运行后就可以看到该界面。

最后给大家分享一个有趣的C++案例

今日分享 C++Beep函数写歌

(转自B站UP主Charleyxiao 视频BV1R54y1X7F4 )

//L'Internationale
#include <bits/stdc++.h>
#include <windows.h>
#define qdo 262 
#define qre 294
#define qmi 330     //q前缀为低音,1后缀为高音,s前缀为半音阶 
#define qfa 349
#define qso 392
#define qla 440
#define qsi 494
#define Do 523
#define re 578
#define mi 659
#define fa 698
#define so 784
#define la 880
#define si 988
#define do1 1046
#define re1 1175
#define mi1 1318
#define fa1 1480
#define so1 1568
#define la1 1760
#define si1 1976
#define sqdo 277
#define sqre 311
#define sqfa 370
#define sqso 415
#define sqla 466
#define sdo 554
#define sre 622
#define sfa 740
#define sso 831
#define sla 932
#define sdo1 1046
#define sre1 1245
#define sfa1 1480
#define sso1 1661
#define sla1 1865
int main(){
  int pai=600,ban=300;
  int ting=128;
  Sleep(100);
  Beep(re1,pai);
  Beep(1370,2*pai);
  Beep(sre1,pai);
  Beep(re1,pai);
  Beep(do1,3*ban);
  Beep(re1,ban);
  Beep(sre1,3*ban);
  
  Beep(sre1,ban);
  Beep(re1,3*ban);
  Beep(re1,ban);
  Beep(do1,3*ban);
  Beep(do1,ban);
  Beep(sla,pai);
  
  Beep(sla,0.75*pai);
  Beep(sla,0.25*pai);
  Beep(sla,pai);
  
  Beep(fa,pai);
  Beep(sla,3*ban);
  Beep(la,ban);
  Beep(do1,ban);
  Beep(sla,ban);
  Beep(fa,ban);
  Beep(re,ban);
  Beep(so,2*pai);
  Beep(sre,3*ban);
  Sleep(128);
  
  Beep(so,ban);
  Beep(do1,3*ban);
  Beep(sla,ban);
  Beep(la,ban);
  Beep(so,ban);
  Beep(fa,ban);
  Beep(sre,ban);
  Beep(re,3*pai);
  Sleep(128);
  
  Beep(fa,pai);
  Beep(sla,3*ban);
  Beep(la,ban);
  Beep(do1,ban);
  Beep(sla,ban);
  Beep(fa,ban);
  Beep(re,ban);
  Beep(so,2*pai);
  Beep(sre,ban);
  Beep(so,ban);
  Beep(do1,ban);
  Beep(sla,ban);
  Beep(la,pai);
  Beep(do1,pai);
  Beep(sre1,pai);
  Beep(la,pai);
  Beep(sla,3*pai);
  Sleep(128);
  
  Beep(re1,ban);
  Beep(do1,ban);
  Beep(la,3*ban);
  Beep(la,ban);
  Beep(so,ban);
  Beep(la,ban);
  Beep(sla,ban);
  Beep(so,ban);
  Beep(la,2*pai);
  Beep(fa,ban);
  Beep(fa,ban);
  Beep(670,ban);
  Beep(fa,ban);
  Beep(so,3*ban);
  Beep(so,ban);
  Beep(do1,3*ban);
  Beep(sla,ban);
  Beep(la,3*pai);
  Sleep(128);
  
  Beep(do1,pai);
  Beep(do1,3*ban);
  Beep(la,ban);
  Beep(fa,ban);
  Beep(fa,ban);
  Beep(670,ban);
  Beep(fa,ban);
  Beep(re1,2*pai);
  Beep(sla,ban);
  Beep(so,ban);
  Beep(la,ban);
  Beep(sla,ban);
  Beep(do1,pai);
  Beep(do1,pai);
  Beep(sla,pai);
  Beep(so,pai);
  Beep(fa,pai);
  Beep(1370,200);
  Beep(1370,200);
  Beep(1370,200);
  Beep(1370,200);
  Beep(1370,200);
  Beep(1370,200);
  
  Beep(re1,0.75*pai);
  Beep(do1,0.25*pai);
  Beep(sla,2*pai);
  Beep(fa,3*ban);
  Beep(re,ban);
  Beep(so,2*pai);
  Beep(sre,pai);
  Beep(do1,0.75*pai);
  Beep(sla,0.25*pai);
  Beep(la,2*pai);
  Beep(so,pai);
  Beep(fa,pai);
  Beep(fa,2.8*pai);
  Sleep(128+0.2*pai);
  
  Beep(fa,pai);
  Beep(re1,2*pai);
  Beep(do1,pai);
  Beep(fa,pai);
  Beep(sla,2*pai);
  Beep(la,3*ban);
  Beep(la,ban);
  Beep(so,3*ban);
  Beep(sfa,ban);
  Beep(so,pai);
  Beep(do1,pai);
  Beep(do1,2.7*pai);
  Sleep(128+0.3*pai);
  
  Beep(re1,0.75*pai);
  Beep(do1,0.25*pai);
  Beep(sla,2*pai);
  Beep(fa,3*ban);
  Beep(re,ban);
  Beep(so,2*pai);
  Beep(sre,pai);
  Beep(do1,0.75*pai);
  Beep(sla,0.25*pai);
  Beep(la,2*pai);
  Beep(so,pai);
  Beep(fa,pai);
  Beep(re1,3*pai);
  Sleep(128);
  
  Beep(re1,pai);
  Beep(1370,2*pai);
  Beep(sre1,pai);
  Beep(re1,pai);
  Beep(do1,3*ban);
  Beep(re1,ban);
  Beep(sre1,3*ban);
  Beep(sre1,ban);
  Beep(re1,3*ban);
  Beep(re1,ban);
  Beep(do1,3*ban);
  Beep(do1,ban);
  Beep(sla,3*pai);
}

有兴趣的小伙伴可以去看看效果,由于我没上传视频不能给大家展示。也可以看B站原视频或者我上传到网盘上的视频 :链接:https://pan.baidu.com/s/1hX3_4ELhGsE9edOCxApPFQ
提取码:gubq

由于时间处在期末,博主还有许多课程设计和考核要做,所以写的比较粗糙。大家有疑问的话欢迎在评论区留言,根据大家反映的情况更新和讲解。切记先自己试着去写,然后根据源码不断修改,自己才会有提高。

销售管理系统源代码:链接:链接:https://pan.baidu.com/s/1GibGpV4JQvC_0it4Qo4Z5w
提取码:4uki
据读者反映的C#小游戏设计的问题,把详细的文档给大家查错:
链接:https://pan.baidu.com/s/1ejQu2y3CWPkj2ubIXsshUg
提取码:zutz
ADO.NET数据库参考资料:链接:https://pan.baidu.com/s/1CUjuA4UrMY2LXyVKjp-cyA
提取码:in68

最后谢谢大家的理解,我把大家可能用到的资料放在上面,需要的自取。

  • 16
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

思维矩阵K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值