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

📄 studentform.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 System.Collections;

namespace test
{
    public partial class StudentForm : Form
    {
        public Boolean isSinge = true;
        public Table table=new Table ();
        public ArrayList arrStudent;
        public studentLocator studentLocator = new studentLocator();
        public Boolean isAddNew = false;
        int newRecordIndex = 0;//记录新行行号
        public StudentForm()
        {
            InitializeComponent();
        }


        public void initStudentForm()
        {
            //初始化界面
            //设置按钮图标

            Bitmap undo_bitmap, new_bitmap, delete_bitmap, multi_bitmap, end_bitmap;
            Bitmap save_bitmap, first_bitmap, previous_bitmap, next_bitmap, last_bitmap;

            Bitmap bitmap;

            bitmap = new Bitmap("images\\Calendar24.gif");
            pictureBox.Image = bitmap;

            undo_bitmap = new Bitmap("images\\Undo24.gif");
            undoToolStripButton.Image = undo_bitmap;
            undoToolStripButton.ToolTipText = "撤销";

            new_bitmap = new Bitmap("images\\New24.gif");
            newToolStripButton.Image = new_bitmap;
            newToolStripButton.ToolTipText = "新建";

            delete_bitmap = new Bitmap("images\\Delete24.gif");
            deleteToolStripButton.Image = delete_bitmap;
            deleteToolStripButton.ToolTipText = "删除";

            save_bitmap = new Bitmap("images\\Save24.gif");
            saveToolStripButton.Image = save_bitmap;
            saveToolStripButton.ToolTipText = "保存";

            first_bitmap = new Bitmap("images\\First24.gif");
            firstToolStripButton.Image = first_bitmap;
            firstToolStripButton.ToolTipText = "第一条";


            previous_bitmap = new Bitmap("images\\Previous24.gif");
            previousToolStripButton.Image = previous_bitmap;
            previousToolStripButton.ToolTipText = "前一条";

            next_bitmap = new Bitmap("images\\Next24.gif");
            nextToolStripButton.Image = next_bitmap;
            nextToolStripButton.ToolTipText = "后一条";

            last_bitmap = new Bitmap("images\\Last24.gif");
            lastToolStripButton.Image = last_bitmap;
            lastToolStripButton.ToolTipText = "后一条";

            multi_bitmap = new Bitmap("images\\Multi24.gif");
            multiToolStripButton.Image = multi_bitmap;
            multiToolStripButton.ToolTipText = "表格";

            end_bitmap = new Bitmap("images\\End24.gif");
            endToolStripButton.Image = end_bitmap;
            endToolStripButton.ToolTipText = "退出";

            this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView.AutoGenerateColumns = true;
            this.dataGridView.AllowUserToAddRows = true;


            isAddNew = false;

            arrStudent = studentLocator.FindAllstudent();
           
            if (arrStudent.Count > 0)//如果记录条数大于0
            {
                singleStudentBinding();//将数据源绑定到单条记录上

                //设置dataGridView的数据源
             


                dataGridView.DataSource = arrStudent;
                
                deleteToolStripButton.Enabled = true;
                undoToolStripButton.Enabled = false;
                newToolStripButton.Enabled = true;
                saveToolStripButton.Enabled = false;
                firstToolStripButton.Enabled = true;
                previousToolStripButton.Enabled = true;
                nextToolStripButton.Enabled = true;
                lastToolStripButton.Enabled = true;
                multiToolStripButton.Enabled = true;
            }

            else//数据表没有记录
            {


                setSingleTextEnableStatus(false);
                firstToolStripButton.Enabled = false ;
                previousToolStripButton.Enabled = false;
                nextToolStripButton.Enabled = false;
                lastToolStripButton.Enabled = false;

                saveToolStripButton.Enabled = false;
                deleteToolStripButton.Enabled = false;
                undoToolStripButton.Enabled = false;
            }

            if (isSinge)//如果是单条记录面板
            {
                collectionPanel.Hide();
                singlePanel.Dock = DockStyle.Fill;

            }
        }
        private void StudentForm_Load(object sender, EventArgs e)
        {

            initStudentForm();







        }

        private void setSingleTextEnableStatus(Boolean state)
        { //功能:设置文本框的Enable为true还是false

            snoTextBox.Enabled = state ;
            snameTextBox.Enabled = state ;
            ageTextBox.Enabled = state;
        }
        private void setSingleTextIsNull()
        { //功能:设置文本框的Enable为true还是false

            snoTextBox.Text = "";
            snameTextBox.Text = "";
            ageTextBox.Text = "";
        }
        private void singleStudentBinding()
        {

            snoTextBox.DataBindings.Add("Text", arrStudent, "学号");
            snameTextBox.DataBindings.Add("Text", arrStudent, "姓名");
            ageTextBox.DataBindings.Add("Text", arrStudent, "年龄");

        }



        private void singleStudentUnBinding()
        {

            snoTextBox.DataBindings.Clear();
            snameTextBox.DataBindings.Clear();
            ageTextBox.DataBindings.Clear();



        }
        private void studentTabPage_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (student s in arrStudent)
            { MessageBox.Show(s.姓名); }
        }

        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            if (!isAddNew)//如果是对原有记录修改
            {
                student s = dataGridView.CurrentRow.DataBoundItem as student;
                studentLocator.Updatestudent(s);
            }
            else
            {
                if (dataGridView.CurrentCell.RowIndex == dataGridView.Rows.Count - 1)//如果是新加的行
                {
                    if (dataGridView.CurrentCell.Value == null)
                        MessageBox.Show("该值不能为空");
                }


            }

        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {

            setSingleTextEnableStatus(true);
            if (arrStudent.Count > 0)//如果当前有记录
                newRecordIndex = dataGridView.CurrentCell.RowIndex;//获得当前行号
            else
                newRecordIndex = 0;
            student s = new student();


           // studentLocator.Addstudent(s);

            snoTextBox.Text = "";

            arrStudent.Insert(newRecordIndex, s);


          

            isAddNew = true;
            undoToolStripButton.Enabled = true;
            deleteToolStripButton.Enabled = false;
            newToolStripButton.Enabled = false;
            saveToolStripButton.Enabled = true;
            firstToolStripButton.Enabled = false;
            previousToolStripButton.Enabled = false;
            nextToolStripButton.Enabled = false;
            lastToolStripButton.Enabled = false;
            multiToolStripButton.Enabled = false;


        }

        private void deleteToolStripButton_Click(object sender, EventArgs e)
        {
            if (dataGridView.CurrentRow != null)
            {

                DialogResult result;
                result =MessageBox.Show ("是否删除该条记录(Y/N)" ,"系统提示",MessageBoxButtons.YesNo ,MessageBoxIcon.Exclamation );
                if (result == DialogResult.Yes)
                {
                    int index = dataGridView.CurrentRow.Index;
                    student s = dataGridView.CurrentRow.DataBoundItem as student;
                    studentLocator.DelstudentById(s.id);
                    arrStudent.RemoveAt(index);
                    dataGridView.DataSource = null;
                    dataGridView.DataSource = arrStudent;

                    if (arrStudent.Count > 0)
                    {
                        singleStudentUnBinding();
                        singleStudentBinding();
                    }
                    else
                    {
                        setSingleTextEnableStatus(false);
                        setSingleTextIsNull();
                    
                    }
                }

            }


        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            student student;
            int count = arrStudent.Count;//获得数据记录的个数
            if (isSinge)//如果是单条记录面板
            {
                
                if (snoTextBox.Text.Trim().Length == 0)
                {
                    
                    MessageBox.Show("请输入学号");
                    return;
                }

                student = new student();
                student.sno = snoTextBox.Text;
                student.sname = snameTextBox.Text;
               // student.age = int.Parse(ageTextBox.Text);
                //student.sum = int.Parse(sumTextBox.Text);
            }
            else
            {
                student = (student)arrStudent[newRecordIndex];//获得新加记录
            }
            if (studentLocator.Addstudent(student))
            {
                isAddNew = false;
                deleteToolStripButton.Enabled = true;
                newToolStripButton.Enabled = true;
                saveToolStripButton.Enabled = false;
                undoToolStripButton.Enabled = false;
                firstToolStripButton.Enabled = true;
                previousToolStripButton.Enabled = true;
                nextToolStripButton.Enabled = true;
                lastToolStripButton.Enabled = true;
                multiToolStripButton.Enabled = true;

                dataGridView.DataSource = null;
                dataGridView.DataSource = arrStudent;
                dataGridView.Rows[newRecordIndex].Selected = true;//光标移动到新加行
                dataGridView.Refresh(); 
            }
            else
            {
                MessageBox.Show("记录添加失败");


            }


        }

       

        private void firstToolStripButton_Click(object sender, EventArgs e)
        {
            int currentRowIndex = dataGridView.CurrentCell.RowIndex;
            if (currentRowIndex > 0)
            {

                dataGridView.ClearSelection();//清除当前选择行
                dataGridView.CurrentCell = dataGridView.Rows[0].Cells[0]; //选中第一行
                snoTextBox.BindingContext[arrStudent].Position = 0;
            }
        }

        //private void prevToolStripButton_Click(object sender, EventArgs e)
        //{
        //    int currentRowIndex = dataGridView.CurrentCell.RowIndex;
        //    if (currentRowIndex > 0)
        //    {
        //        dataGridView.ClearSelection();//清除当前选择行
        //        dataGridView.CurrentCell = dataGridView.Rows[currentRowIndex - 1].Cells[0]; //选中第一行
        //        snoTextBox.BindingContext[arrStudent].Position = currentRowIndex - 1;
        //    }

        //}

        private void nextToolStripButton_Click(object sender, EventArgs e)
        {
            int currentRowIndex = dataGridView.CurrentCell.RowIndex;
            if (currentRowIndex < dataGridView.Rows.Count - 1)
            {
                dataGridView.ClearSelection();//清除当前选择行
                dataGridView.CurrentCell = dataGridView.Rows[currentRowIndex + 1].Cells[0]; //选中第一行
                snoTextBox.BindingContext[arrStudent].Position = currentRowIndex + 1;
                // singleStudentBinding(currentRowIndex + 1);
            }


        }

        private void lastToolStripButton_Click(object sender, EventArgs e)
        {
            int currentRowIndex = dataGridView.CurrentCell.RowIndex;
            if (currentRowIndex < dataGridView.Rows.Count - 1)
            {
                dataGridView.ClearSelection();//清除当前选择行
                dataGridView.CurrentCell = dataGridView.Rows[dataGridView.Rows.Count - 1].Cells[0];
                snoTextBox.BindingContext[arrStudent].Position = arrStudent.Count - 1; //选中第一行
            }

        }

        private void multiToolStripButton_Click(object sender, EventArgs e)
        {
            if (isSinge)//如果是单条记录面板显示
            {
                singlePanel.Hide();
                collectionPanel.Dock = DockStyle.Fill;
                collectionPanel.Show(); ;
                isSinge = false;

            }
            else
            {
                collectionPanel.Hide();
                singlePanel.Dock = DockStyle.Fill;
                singlePanel.Show();
                isSinge = true;

            }
        }

        private void undoToolStripButton_Click(object sender, EventArgs e)
        {
            student student = (student)arrStudent[newRecordIndex];//获得新记录对象
            // studentLocator.DelstudentById(student.id);//删除数据库中的记录
            arrStudent.RemoveAt(newRecordIndex);//删除对象数组中的对象
            dataGridView.DataSource = null;
            dataGridView.DataSource = arrStudent;

            isAddNew = false;
            deleteToolStripButton.Enabled = true;
            undoToolStripButton.Enabled = false;
            newToolStripButton.Enabled = true;
            saveToolStripButton.Enabled = false;
            firstToolStripButton.Enabled = true;
            previousToolStripButton.Enabled = true;
            nextToolStripButton.Enabled = true;
            lastToolStripButton.Enabled = true;
            multiToolStripButton.Enabled = true;
        }

        private void previousToolStripButton_Click(object sender, EventArgs e)
        {
            int currentRowIndex = dataGridView.CurrentCell.RowIndex;
            if (currentRowIndex > 0)
            {
                dataGridView.ClearSelection();//清除当前选择行
                dataGridView.CurrentCell = dataGridView.Rows[currentRowIndex - 1].Cells[0]; //选中第一行
                snoTextBox.BindingContext[arrStudent].Position = currentRowIndex - 1;
            }
        }

        private void endToolStripButton_Click(object sender, EventArgs e)
        {

        }

       

      

        
       

       

      

       

       

       

     
       



    }
}

⌨️ 快捷键说明

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