c# - 在 C# 中为面板创建布局时遇到问题

标签 c# layout layout-manager

我想创建一个布局管理器,它将面板划分为多个单元格,并且可以将一个组件添加到指定单元格(行、列)的面板中。调整面板大小时应更新单元格的宽度和高度(这是问题所在,它不会调整单元格的大小)。这些类本身有效,但在调整大小时它不会更新单元格大小,它甚至似乎没有调用 LayoutManager::SetBounds(int, int)。我创建一个单独的类的原因是因为我将对其他容器使用相同的布局管理器,例如 GroupBox。这是我的代码

public class Insets
{
    public int bottom = 0;
    public int top = 0;
    public int left = 0;
    public int right = 0;

    public Insets(int top, int left, int bottom, int right) {
        this.top = top;
        this.left = left;
        this.bottom = bottom;
        this.right = right;
    }
}

/*
*  LayoutManager class
*/

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;



public class LayoutManager
{
    public enum Constraints{
        HORIZONTAL,
        VERTICAL,
        NONE
    };

    public Insets insets = new Insets(0, 0, 0, 0);
    public Constraints fill = Constraints.NONE;
    public Panel container = null;
    public int cellheight = 1;
    public int cellwidth = 1;

    private List <int> indices = new List <int>();
    private int columns = 1;
    private int rows = 1;
    private int dh = 0;
    private int dw = 0;

    public LayoutManager(){
    }
    private int Index2DTo1D(int row, int column){
        return (column + (row * columns));
    }
    private int Index1DToRow2D(int index){
        return index / columns;
    }
    private int Index1DToColumn2D(int index){
        return index / rows;
    }
    private void SetGridMatrix(int rows, int columns) {
        if (rows >= this.rows){
            this.rows = rows + 1;
        }
        if (columns >= this.columns){
            this.columns = columns + 1;
        }
    }
    public void RepaintChildrenComponents() {
        if(container != null){
            foreach(Control control in container.Controls){
                ((CheckBox)control).Text = container.Width.ToString();
            }       
        }
    }
    public void SetBounds(int width, int height) {
        SetCellsDimensions(width, height);
        RepaintChildrenComponents();
    }
    public void SetCellsDimensions(int width, int height){
        this.dw = width / columns;
        this.dh = height / rows;
    }
    public void SetContainer(ref Panel container) {
        this.container = container;
    }
    public Point GetChildComponentXY(int x, int y){
        if(x >= 0 && y >= 0){
            return new Point((x * dw) + insets.right, (y * dh) + insets.top);
        }
        throw new System.ArgumentOutOfRangeException("");
    }
    public Size GetComputedComponentSize(){
        return new Size((cellwidth * dw) - (insets.left + insets.right),
                        (cellheight * dh) - (insets.top + insets.bottom));            
    }
    public void AddComponent(Control control, Point location, LayoutManager.Constraints fill){
        if(container != null && control != null){
            indices.Insert(container.Controls.Count, Index2DTo1D(location.X, location.Y));
            container.Controls.Add(SetChildComponentBounds(control, location));
            SetGridMatrix(location.X, location.Y);
            SetCellsDimensions(container.Width, container.Height);
        }else{
            throw new NullReferenceException("");
        }
    }
    protected Control SetChildComponentBounds(Control control, Point location) {
        control.Location = GetChildComponentXY(location.X, location.Y);
        control.Size = GetComputedComponentSize();
        return control;
    }
    public void AddComponent(Control control, Point location){
        AddComponent(control, location, LayoutManager.Constraints.NONE);
    }
}

/*
*
* JPanel class
*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;


class JPanel : Panel
{
    private LayoutManager layman;

    public JPanel(){
        layman = new JComponent.LayoutManager();

        Panel container = this;
        layman.SetContainer(ref container);


        string[] elems = { "Multiplication", "Division", "Addition", "Subtraction"};
        int e = 0;

        this.Size = new Size(300, 125);

        foreach(string str in elems){

            CheckBox box = new CheckBox();
            box.Text = str;
            layman.AddComponent(box, new Point(0, e++));
            //this.Controls.Add(box);
        }

    }
    public JPanel(LayoutManager layman) {
        LayoutManager = layman;
    }

    protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified){
        if(layman != null){
            layman.SetBounds(width, height);
        }
        base.SetBoundsCore(x, y, width, height, specified);
    }
    public LayoutManager LayoutManager{
        set{
            layman = value;
        }
        get{
            return layman;
        }
    }
}

最佳答案

请查看 TableLayoutPanel 类。我认为这个控件正是在做你想要实现的。 TableLayoutPanel 允许您在表单编辑器中以编程方式定义行/列、将控件置于单元格内、合并行/列、根据内容自动调整面板大小或根据面板大小自动调整内容大小。

这是一份非常有趣的文档,介绍了如何在 Windows 窗体中进行布局:

https://docs.google.com/viewer?a=v&q=cache:nePz-uaPReYJ:www.windowsclient.net/samples/go%2520to%2520market/layout/layoutgtm.doc+Whidbey+Layout+tablelayoutpanel&hl=de&gl=de&pid=bl&srcid=ADGEEShlobh-RfSTegC1NuOc72m0vRPu6RsF3USTSPnSasU-9qSklTbJzclRol6Nxg5sxEUXZOWYZwgrSdZdAjJ5EXhCft7qj5jBr4-kkkX6380-0itVaHkG-TAvK4KvNtDymEFlUXM6&sig=AHIEtbTnyasKOG5tuZwub59LBw8UeZL8Lg

不幸的是,我只能通过谷歌搜索缓存找到文档。原始链接似乎已被删除。

关于c# - 在 C# 中为面板创建布局时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13084168/

相关文章:

html - 如何使用 CSS3 创建以下样式?

python - 如何调整 QVBoxLayout 中布局之间的空间

c# - 如何在 RichTextBox 中有选择地给字符串加下划线?

c# - 使用 Lucene.net 进行分页

c# - ViewModel 或 Model 中的 ASP.NET MVC 验证?

C# 在 List<T> 中查找对象

html - CSS使一列中的两个div占据与相邻div相同的高度

java - 如何对齐 JPanels/JFrames 中的元素?

java - GridBagLayout 中的不均匀列

java - GridLayout 空边距