panellayoutbuttons.cs

来自「这是用VC编写的一个关于计算器的代码」· CS 代码 · 共 48 行

CS
48
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace ProgramCalculator
{
    /// <summary>
    /// 继承于Panel,用于放置函数按钮的面板
    /// </summary>
    class PanelLayoutButtons:Panel
    {
        private readonly int dxButtonLoc = 6;
        private readonly int dyButtonLoc = 6;
        private readonly int xButtonCount = 4;


        public PanelLayoutButtons()
        {
            this.AutoScroll = true;
        }

        /// <summary>
        /// 添加函数按钮到面板
        /// </summary>
        /// <param name="btn">按钮对象</param>
        public void AddButton(ref Button btn)
        {
            //计算按钮高度与宽度
            btn.Width = (this.Width - (xButtonCount - 1) * dxButtonLoc) / xButtonCount;
            btn.Height = (int)(btn.Width / 2.5);


            //计算按钮位置
            int index = this.Controls.Count;
            int rowLoc = (index) / xButtonCount;
            int columnLoc = index % xButtonCount;
            btn.Location = new System.Drawing.Point(
                columnLoc * (btn.Width + dxButtonLoc), rowLoc * (btn.Height + dyButtonLoc));
         
            if ( !this.Contains(btn) )
            {
                this.Controls.Add(btn);
            }
        }
    }
}

⌨️ 快捷键说明

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