⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 materialform.cs

📁 一个超市管理系统,没有错误,非常好,里面什么都有!很使用,很有用
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DaFanRongMIS.Model.Material ;
using DaFanRongMIS.Model.CateGory;
using DaFanRongMIS.Model.Unit;

namespace DaFanRongMIS.ViewController.Material
{
    public partial class MaterialForm : Form
    {
        public TextBox tbMaterial=null;

        public MaterialForm(TextBox tb)
        {
            InitializeComponent();
            tbMaterial=tb;
            tbAdd.Visible = false;
            tbDelete.Visible = false;
            tbUpdate.Visible = false;
        }
        public MaterialForm()
        {
            InitializeComponent();
            
        }
        MaterialEntity me = new MaterialEntity();
        MaterialDAO md = new MaterialDAOImpl();
        private void MaterialForm_Load(object sender, EventArgs e)
        {
            
            this.Fill();
            this.Clear();
            //绑定下拉列表框中的内容
            CateGoryDAO categoryDAO = new CateGoryDAOImpl();
            this.cmbCategory .DataSource = categoryDAO.Select();
            cmbCategory.DisplayMember = "name";
            cmbCategory.ValueMember = "ID";
            //实例化操作类
            UnitDAO unitDAO = new UnitDAOImpl();
            this.cmbUnit.DataSource = unitDAO.Select();
            cmbUnit.DisplayMember = "name";
            cmbUnit.ValueMember = "ID";
        }
        #region 填充方法
        public void Fill()
        {
            this.LV.Items.Clear();
            DataTable dt = md.Select();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem(dt.Rows[i][0].ToString());
                item.SubItems.Add(dt.Rows[i][1].ToString());
                item.SubItems.Add(dt.Rows[i][2].ToString());
                item.SubItems.Add(dt.Rows[i][3].ToString());
                item.SubItems.Add(dt.Rows[i][4].ToString());
                
                LV.Items.Add(item);
            }
        }
        #endregion

        #region 增加
        private void tbAdd_Click(object sender, EventArgs e)
        {
            if (txtId.Text != "" && txtName.Text != "")
            {
                //为文本框赋值
                me.Id = this.txtId.Text;
                me.Name = this.txtName.Text;
                me.Unitid = this.cmbUnit.SelectedValue.ToString();
                me.Categoryid = this.cmbCategory.SelectedValue.ToString();
                me.Memo = this.txtMemo.Text;
                //调用操作类中的增加方法
                string str = md.AddMateria(me);
                if (str == "ok")
                {
                    MessageBox.Show("添加信息已成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }//end block if
                else
                {
                    MessageBox.Show("添加信息失败!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }//end block else
                //this.Fill();//可修改为“在控件上只填加一行,而不必从数据库中提取全部数据”??
                ListViewItem item = new ListViewItem(this.txtId.Text);
                item.SubItems.Add(this.txtName.Text);
                item.SubItems.Add(this.cmbUnit.SelectedValue.ToString());
                item.SubItems.Add(this.cmbCategory.SelectedValue.ToString());
                item.SubItems.Add(this.txtMemo.Text);

                LV.Items.Add(item);
                this.Clear();
            }
            else
            {
                MessageBox.Show("编号或名称不能为空!","提示信息",MessageBoxButtons.OK ,MessageBoxIcon.Information );
            }
        }
        #endregion

        #region 修改
      
        private void tbUpdate_Click_1(object sender, EventArgs e)
        {
            //为文本框赋值
            me.Id = this.txtId.Text;
            me.Name = this.txtName.Text;
            me.Unitid = this.cmbUnit.SelectedValue.ToString();
            me.Categoryid = this.cmbCategory.SelectedValue.ToString();
            me.Memo = this.txtMemo.Text;
            //调用操作类中的修改方法
            string str = md.UpdateMateria(me);
            if (str == "ok")
            {
                MessageBox.Show("修改信息已成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }//end block if
            else
            {
                MessageBox.Show("修改信息失败!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }//end block else
            //this.Fill();//也可修改当前的选中项,而不从数据库中提取全部数据
            
            LV.SelectedItems[0].SubItems[0].Text =this.txtId.Text ;
            LV.SelectedItems[0].SubItems[1].Text = this.txtName.Text;
            LV.SelectedItems[0].SubItems[2].Text = this.cmbUnit.SelectedValue.ToString();
            LV.SelectedItems[0].SubItems[3].Text = this.cmbCategory.SelectedValue.ToString();
            LV.SelectedItems[0].SubItems[4].Text = this.txtMemo.Text;
            this.Clear();
        }
              #endregion

        #region 删除
  
        private void tbDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除此记录吗?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //为文本框赋值
                me.Id = this.txtId.Text;
                //调用操作类中删除方法
                string str = md.DeleteMateria(this.txtId.Text);
                if (str == "ok")
                {
                    MessageBox.Show("删除信息已成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }//end block if
                else
                {
                    MessageBox.Show("删除信息失败!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                }//end block else
                //this.Fill();//可修改为"只在控件中删除一行,而不必提取数据库中的全部数据"???
                this.LV.SelectedItems[0].Remove();
                this.Clear();
            }
        }
        #endregion

        #region 反填
        
        private void LV_Click_1(object sender, EventArgs e)
        {
            tbUpdate.Enabled = true ;
            tbDelete.Enabled = true ;
            this.txtId .Text = LV.SelectedItems[0].SubItems[0].Text;
            this.txtName .Text = LV.SelectedItems[0].SubItems[1].Text;
            this.cmbUnit .Text = LV.SelectedItems[0].SubItems[2].Text;
            this.cmbCategory .Text = LV.SelectedItems[0].SubItems[3].Text;
            this.txtMemo .Text = LV.SelectedItems[0].SubItems[4].Text;
           
        }
        #endregion

        #region 清空文本框
        public void Clear()
        {
            this.txtId.Text = "";
            this.txtName.Text = "";
            this.txtMemo.Text = "";

            tbUpdate.Enabled = false;
            tbDelete.Enabled = false;

        }
        #endregion

        #region 按名称查询
        private void tbSelect_Click(object sender, EventArgs e)
        {
            //为文本框赋值
            me.Name = tbSelectName.Text;
            //调用操作类中的查询方法
            DataTable dt = md.SelectLike(tbSelectName.Text);
            LV.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem(dt.Rows[i][0].ToString());
                item.SubItems.Add(dt.Rows[i][1].ToString());
                item.SubItems.Add(dt.Rows[i][2].ToString());
                item.SubItems.Add(dt.Rows[i][3].ToString());
                item.SubItems.Add(dt.Rows[i][4].ToString());
                
                LV.Items.Add(item);
            }
        }
        #endregion

        private void LV_DoubleClick(object sender, EventArgs e)
        {
            if (tbMaterial != null)
            {
                tbMaterial.Tag = LV.SelectedItems[0].SubItems[0].Text;
                tbMaterial.Text = LV.SelectedItems[0].SubItems[1].Text;
                
                this.Close();
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -