【WinForm】WinForm实现课程表

文章目录

  • 前言
  • 一、课程表
    • 1、效果
    • 2、界面设计
    • 3、代码
  • 总结


前言


一、课程表

1、效果

编辑模式:
【WinForm】WinForm实现课程表_第1张图片
锁定模式:

【WinForm】WinForm实现课程表_第2张图片

2、界面设计

【WinForm】WinForm实现课程表_第3张图片

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 课程表
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            foreach (Control ctr in this.tableLayoutPanel1.Controls)
            {
                if (ctr is TextBox)
                {
                    TextBox tb = (TextBox)ctr;
                    tb.Click += (tb_Click);
                    tb.Click += (tb_ChangeFormName);   
                }
            }
        }

        void tb_ChangeFormName(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text != "")
                this.Text = tb.Text;
        }

        void tb_Click(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (tb.Text != "")
                MessageBox.Show(tb.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.button1.Text == "编辑")
            {
                button1.Text = "锁定";
                SetTableLockStatus(false);
            }
            else
            {
                button1.Text = "编辑";
                SetTableLockStatus(true);
            }
        }

        public void SetTableLockStatus(bool isLocked)
        {
            foreach (Control ctr in this.tableLayoutPanel1.Controls)
            {
                if (ctr is TextBox)
                {
                    ctr.Enabled = !isLocked;
                }
            }
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            Rectangle updatteRec = new Rectangle(0, 0, tableLayoutPanel1.Width, tableLayoutPanel1.Height / 10);
            tableLayoutPanel1.Invalidate(updatteRec);
            tableLayoutPanel1.Update();
        }

        private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
        {
            if (e.Column + 1 == (int)monthCalendar1.SelectionStart.DayOfWeek && e.Row == 0)
            {
                e.Graphics.FillRectangle(Brushes.Red, e.CellBounds);
            }
        }

  
    }
}


总结

你可能感兴趣的:(#,WinForm案例,c#,winform)