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

📄 shitimoban.cs

📁 程序设计语言上机考试系统
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1.KaowuManage
{
    public partial class ShitiMoBan : Form
    {
        SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
        baseclass.BaseOperate boperate = new baseclass.BaseOperate();
        baseclass.OperateAndValidate opAndvalidate = new baseclass.OperateAndValidate();
        protected string M_str_sql = "select Types as 试题类型,T_shu as 试题数目,Score as 分值 ,idmoban as 模板编号 from moban ";
        protected string M_str_table = "moban";
        protected int M_int_judge;
        bool ifnew = false; //是否添加新模板
        //鼠标点击的行号
        int currentrow;
        int tshu;
        //试题分数
        decimal mark = 0;
        decimal score;
        public ShitiMoBan()
        {
            InitializeComponent();
        }

        private void ShitiMoBan_Load(object sender, EventArgs e)
        {
            DataSet myds = boperate.getds(M_str_sql, M_str_table);
            dataGridView1.DataSource = myds.Tables[0];
            if (myds.Tables[0].Rows.Count <= 0)
            {
                dataGridView1.AllowUserToAddRows = true;
            }
            if (myds.Tables[0].Rows.Count > 0)
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
            tooltxtnum.ComboBox.DisplayMember = "idmoban";
            tooltxtnum.ComboBox.ValueMember = "idmoban";

            SqlDataAdapter adp = new SqlDataAdapter("select distinct idmoban  from moban", cn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "moban");
            tooltxtnum.ComboBox.DisplayMember = "idmoban";
            tooltxtnum.ComboBox.ValueMember = "idmoban";
            tooltxtnum.ComboBox.DataSource = ds.Tables["moban"];
        }

        #region 判断输入的成绩和题数是否合法
        public bool Islegal()
        {
            bool islegal = true;
            if (!opAndvalidate.validateNumOnly(combmark.Text.Trim()))
            {
                errorPnum.SetError(combmark, "分数不能为0、负数或其他字符!");
                islegal = false;
            }
            if (!opAndvalidate.validateNum(combnumber.Text.Trim()))
            {
                errorProvider1.SetError(combnumber, "请输入正整数!");
                islegal = false;
            }
            return islegal;
        }
        #endregion

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if(IfEnough())
            {
                errorProvider1.Clear();
                errorPnum.Clear();
                if(Islegal())
                {
                    errorPnum.Clear();
                    errorProvider1.Clear();
                    if (cn.State == ConnectionState.Closed)
                        cn.Open();
                    string id;
                    if (tooltxtnum.Text == "")
                        id = labelbianhao.Text.Trim();
                    else
                    {
                        if (ifnew)
                        {
                            id = labelbianhao.Text.Trim();
                            labelbianhao.Text = "";
                            ifnew = false;
                        }
                        else
                            id = tooltxtnum.Text.Trim();
                    }
                    tshu = Convert.ToInt32(combnumber.Text.ToString().Trim());
                    score = Convert.ToDecimal(combmark.Text.ToString().Trim());

                    SqlCommand cmd = new SqlCommand("select * from moban where idmoban ='" + id + "'and types = '" + comboshiti.Text.ToString().Trim() + "'", cn);
                    if (null != cmd.ExecuteScalar())
                        MessageBox.Show("已存在该类型试题", "提示");
                    else
                    {
                        //试题分值清零
                        mark = 0;
                        //统计数据库中该模板已经存在的试题的总分值
                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            if (string.Compare(dataGridView1.Rows[i].Cells[3].Value.ToString().Trim(), id, true) == 0)
                                mark += Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value) *
                                        Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
                        }
                        //累加将要添加的试题的信息
                        mark += Convert.ToInt32(combnumber.Text.Trim()) * Convert.ToDecimal(combmark.Text.Trim());
                        if (mark > 100)
                            MessageBox.Show("试题总分值不得超过100!");
                        else
                        {
                            boperate.getcom("insert into moban(types,T_shu,score,idmoban) values('" + comboshiti.Text.ToString().Trim() + "','" + tshu + "','" + score + "','" + id + "')");
                            MessageBox.Show("信息添加成功", "提示");
                            ShitiMoBan_Load(sender, e);
                        }
                    }
                    if (cn.State == ConnectionState.Open)
                        cn.Close();
                }
               
            }
        }
        
        #region 判断信息是否完整
        public bool IfEnough()
        {
            if (tooltxtnum.Text.Trim() == "" && labelbianhao.Text.Trim() == "")
            {
                MessageBox.Show("请先添加新模板!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (comboshiti.Text.Trim() == "" || combnumber.Text.Trim() == "" || combmark.Text.Trim() == "")
            {
                MessageBox.Show("请填写完整的信息", "提示");
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion
        /// <summary>
        /// 删除题型信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要删除吗?", "删除确定", MessageBoxButtons.YesNo,MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    boperate.getcom("delete from moban where idmoban='" + Convert.ToString(dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'and types = '" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'");
                    ShitiMoBan_Load(sender, e);
                    MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 修改题型信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdata_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            errorPnum.Clear();
            if (comboshiti.Text.Trim() == "" || combnumber.Text.Trim() == "" || combmark.Text.Trim() == "" )
                MessageBox.Show("请填写完整的信息", "提示");
            else
            {
                if(Islegal())
                {
                    errorPnum.Clear();
                    string id = Convert.ToString(dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value).Trim();
                    //试题分值清零
                    mark = 0;
                    //统计数据库中该模板已经存在的试题的总分值
                    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    {
                        if (i == currentrow)
                            continue;
                        else
                        {
                            if (string.Compare(dataGridView1.Rows[i].Cells[3].Value.ToString().Trim(), id, true) == 0)
                                mark += Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value) *
                                        Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
                        }
                    }
                    //累加将要修改的试题信息
                    decimal cur1, cur2;
                    cur1 = Convert.ToDecimal(combnumber.Text.Trim());
                    cur2 = Convert.ToDecimal(combmark.Text.Trim());
                    mark += Convert.ToDecimal(cur1 * cur2);
                    if (mark > 100)
                        MessageBox.Show("试题总分值不得超过100!");
                    else
                    {
                        boperate.getcom("update moban set types ='" + comboshiti.Text.Trim() + "',T_shu ='" + combnumber.Text.Trim() + "',score ='" + combmark.Text.Trim() + "' where idmoban ='" + id + "'and types = '" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'");
                        ShitiMoBan_Load(sender, e);
                        MessageBox.Show("信息修改成功", "提示");
                    }
                }
            }
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            currentrow = dataGridView1.CurrentCell.RowIndex;
            mark = 0;
            string id = Convert.ToString(dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
               
                if (string.Compare(dataGridView1.Rows[i].Cells[3].Value.ToString().Trim() ,id,true) == 0)
                    mark += Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value)*
                            Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
                
            }
            labelmark.Text = mark.ToString();
            dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.LightSlateGray; 
            
            comboshiti.Text = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            combnumber.Text = Convert.ToString(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            combmark.Text = Convert.ToString(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            tooltxtnum.Text = id;


        }
        
        #region 添加试题模板
        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            opAndvalidate.autoNum("select idmoban from moban", "moban", "idmoban", "M", "1000001", txtnum);
            labelbianhao.Text = txtnum.Text.Trim();
            ifnew = true;
            ShitiMoBan_Load(sender, e);

        }
        #endregion

        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 删除编号为tooltxtnum的所有试题信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要删除编号为" + tooltxtnum.Text.ToString().Trim()+ "的模板吗?", "删除确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    boperate.getcom("delete from moban where idmoban ='" + tooltxtnum.Text.ToString().Trim() + "'");
                    MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    tooltxtnum.Text = "";
                    ShitiMoBan_Load(sender, e);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
        }
        
        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnLook_Click(object sender, EventArgs e)
        {
            DataSet myds = boperate.getds("select Types as 试题类型,T_shu as 试题数目,Score as 分值 ,idmoban as 模板编号 from moban where idmoban = '"+tooltxtnum.Text.Trim()+"'", M_str_table);
            dataGridView1.DataSource = myds.Tables[0];
            if (myds.Tables[0].Rows.Count > 0)
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
        }

        #region 显示所有信息
        private void tSbtnAll_Click(object sender, EventArgs e)
        {
            DataSet myds = boperate.getds(M_str_sql, M_str_table);
            dataGridView1.DataSource = myds.Tables[0];
            if (myds.Tables[0].Rows.Count <= 0)
            {
                dataGridView1.AllowUserToAddRows = true;
            }
            if (myds.Tables[0].Rows.Count > 0)
                btnDelete.Enabled = true;
            else
                btnDelete.Enabled = false;
        }
        #endregion


    }
}

⌨️ 快捷键说明

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