依照教程实现了动态的侧边栏(C#)

发布时间 2023-10-17 18:55:31作者: yesyes1

心得说明

原来C#的动态效果是通过timer组件的运用实现的(start、stop);

真的直接发现了各个组件的属性的重要性,不然真的很难解决问题;

这里我觉得吧,先把组件的各个属性搞清楚,那么搭建系统就不会云里雾里了,首先逻辑就会很清晰了~

后端具体代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace testcsharp001
{
    public partial class Form1 : Form
    {
        bool sidebarExpand;
        bool oneExpand;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void sidebar_timer(object sender, EventArgs e)
        {
            if (sidebarExpand)
            {
                sidebar.Width -= 10;
                if (sidebar.Width == sidebar.MinimumSize.Width)
                {
                    sidebarExpand = false;
                    sidebarTimer.Stop();
                }
            }
            else
            {
                sidebar.Width += 10;
                if (sidebar.Width == sidebar.MaximumSize.Width)
                {
                    sidebarExpand = true;
                    sidebarTimer.Stop();
                }
            }
        }

        private void menu_Click(object sender, EventArgs e)
        {
            sidebarTimer.Start();
        }

        private void onetimer_Tick(object sender, EventArgs e)
        {
            if (oneExpand)
            {
                textonecontainer.Height += 10;
                if (textonecontainer.Height == textonecontainer.MaximumSize.Height)
                {
                    oneExpand = false;
                    onetimer.Stop();
                }
            }
            else
            {
                textonecontainer.Height -= 10;
                if (textonecontainer.Height == textonecontainer.MinimumSize.Height)
                {
                    oneExpand = true;
                    onetimer.Stop();
                }
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            onetimer.Start();
        }
    }
}