不懒虫

WinForm开发之取送货管理2

写的有点慢,但都是一步步操作的,希望这些能成为以后宝贵的财富,话不多说,续上次取送货基本信息管理之产品分类管理,下面进行增删改的编写。

增加产品分类管理信息记录,双击[新增]按钮(其新增可让用户在TextBox上可输入,并不是将信息添加到数据库的过程,在[保存]按钮上才在数据库中进行信息的添加与修改),在编写buttonAdd_Click事件之前,根据需求没有点击新增或选择修改项,TextBox上不可编辑的,所以编写了使panel中所有控件生效,无效,可视方法。代码如下:

        public static void ControlsInPanelEnable(Panel panel)
        {
            for (int i = 0; i < panel.Controls.Count; i++)
            {
                if (panel.Controls[i] is TextBox)
                {
                    (panel.Controls[i] as TextBox).Enabled = true;
                    (panel.Controls[i] as TextBox).Text = "";
                }
                if (panel.Controls[i] is RichTextBox)
                {
                    (panel.Controls[i] as RichTextBox).Enabled = true;
                    (panel.Controls[i] as RichTextBox).Text = "";
                }
                if (panel.Controls[i] is ComboBox)
                {
                    (panel.Controls[i] as ComboBox).Enabled = true;
                    (panel.Controls[i] as ComboBox).Text = "";
                }
                if (panel.Controls[i] is DateTimePicker)
                {
                    (panel.Controls[i] as DateTimePicker).Enabled = true;
                    (panel.Controls[i] as DateTimePicker).Text = "";
                }
                if (panel.Controls[i] is PictureBox)
                {
                    (panel.Controls[i] as PictureBox).Enabled = true;
                    (panel.Controls[i] as PictureBox).Text = "";
                }
            }
        }

使panel中所有控件无效和有效不清空所选记录信息的方法的实现与上述代码相似,参考代码可下载代码。

在Load事件中先写下控件文本不可编辑,即panel中的所有控件无效,代码如下:

        private void Frm_ProductType_Load(object sender, EventArgs e)
        {
            ControlsInPanelDisable(this.panel1);
        }

然后实现触发buttonAdd_Click事件,代码编写如下:

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            m_strOperationType = "add";
            ControlsInPanelEnable(this.panel1);
            m_CK_ProductTypeID = -1;//使添加记录后,不指向任何记录
        }

在进行[修改]按钮双击事件的编写时,有得到鼠标在dataGridView1单元格选中的信息,得到信息就得知道表的主键,方法在dataGridView1_CellMouseClick事件得到,代码如下:

        //将信息显示到控件上
        private void cellclick()
        {
            TSM.Model.CK_ProductType modelCK_ProductType = m_bllCK_ProductType.GetModel(m_CK_ProductTypeID);

            if (modelCK_ProductType != null)
            {
                this.textBoxCateName.Text = modelCK_ProductType.CK_ProductTypeName;
            }
            else
            {
                this.textBoxCateName.Text = "";
            }
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strID = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_CK_ProductTypeID = int.Parse(strID);
            ControlsInPanelDisable(this.panel1);
            cellclick();
            m_strOperationType = "";
        }

[修改]按钮的触发事件实现代码:

        private void buttonModify_Click(object sender, EventArgs e)
        {
            if (m_CK_ProductTypeID == -1)
            {
                MessageBox.Show("请选中一条信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                ControlsInPanelDisable(this.panel1);
                m_strOperationType = "";
                return;
            }
            ControlsInPanelEnableNotClear(this.panel1);
            m_strOperationType = "edit";
        }

下面是[保存]按钮的触发事件,代码如下:

        //将控件上的信息赋值给对象的属性里
        private void setInfo(ref TSM.Model.CK_ProductType modelCK_ProductType)
        {
            modelCK_ProductType.CK_ProductTypeName = textBoxCateName.Text;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (m_strOperationType == "add")//增加
            {
                //new 一个 TSM.Model.CK_ProductType 对象
                TSM.Model.CK_ProductType modelCK_ProductType = new TSM.Model.CK_ProductType();
                setInfo(ref modelCK_ProductType);

                int nId = m_bllCK_ProductType.Add(modelCK_ProductType);
                ControlsInPanelDisable(this.panel1);
                m_CK_ProductTypeID = nId;

                ShowAllRecords(m_strWhere);
                MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if(m_strOperationType == "edit")
            {
                //从bll层根据Id得到model层的实例对象
                TSM.Model.CK_ProductType modelCK_ProductType = m_bllCK_ProductType.GetModel(m_CK_ProductTypeID);
                setInfo(ref modelCK_ProductType);

                m_bllCK_ProductType.Update(modelCK_ProductType);
                ShowAllRecords(m_strWhere);
                ControlsInPanelDisable(this.panel1);
                MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            m_strOperationType = "";
            m_CK_ProductTypeID = -1;
        }

下面是[保存]按钮的触发事件,代码如下:

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (m_bllCK_ProductType.GetModel(m_CK_ProductTypeID) == null)
            {
                MessageBox.Show("请选中一条信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么,删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllCK_ProductType.Delete(m_CK_ProductTypeID);
            ShowAllRecords(m_strWhere);

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
        }

总结:在增加和更新数据时,都要得到model层的实例,如:m_bllCK_ProductType.Add(modelCK_ProductType);

在得到model层实例和删除数据时,要得到数据表的主键,如:modelCK_ProductType = m_bllCK_ProductType.GetModel(CK_ProductTypeID);

前两节代码

编程是一个熟练的过程,不能光看,也不能理解了就认为会了,就算在简单的代码也要敲几遍,不用多,理解熟练了就好,可能现在的你看都不懂,动起手来自己敲敲,运行看看,出一些错误,总是有收获的,这是我的理解,我还在体会这一过程,希望与君同行,不说成为社会做多大贡献,只说为了更好的生活。

posted on 2014-03-15 20:50  不懒虫  阅读(1065)  评论(0编辑  收藏  举报

导航