本文介绍了tableLayoutPanel中的UserControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我只想在运行时将"UserControl"放入"TableLayoutPanel"中.
问题是,UserControl始终保持其原始大小.因此,您当然永远看不到它.这似乎并不困难,但例如所有Dock-和Autosize-属性绝对无效!

如何调整容器(TableLayoutPanel)的大小并自动调整大小以适合容器(没有加载resize-eventhandler)?


Hallo everybody,

I just want to put a "UserControl" into a "TableLayoutPanel" at runtime.
The problem is, the UserControl always keeps its original size. So of course you can never see it properly. It doesn''t seem to be difficult, but e.g. all the Dock- and Autosize-properties have absolutely no effect!

How can one adjust the size to the container (the TableLayoutPanel) and automatically resize it fitting the container (without loads of resize-eventhandlers)?


Thanks for any answer!

推荐答案

using System;
using System.Windows.Forms;
namespace UserControlDocking {
  public class MainForm : Form {
    [STAThread]
    static void Main() {
      Application.Run(new MainForm());
    }

    TableLayoutPanel tlp;

    protected override void OnShown(EventArgs e) {
      base.OnShown(e);
      // Configure the panel 2 x 2
      tlp = new TableLayoutPanel();
      tlp.Dock = DockStyle.Fill;
      tlp.RowCount = 2;
      tlp.ColumnCount = 2;
      tlp.GrowStyle = TableLayoutPanelGrowStyle.FixedSize;
      tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
      tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
      tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
      tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
      Controls.Add(tlp);
      // Add a control to each cell
      AddDockedControl(0, 0, System.Drawing.Color.Red);
      AddDockedControl(1, 0, System.Drawing.Color.Green);
      AddDockedControl(1, 0, System.Drawing.Color.Blue);
      AddDockedControl(1, 1, System.Drawing.Color.Yellow);
    }

    private void AddDockedControl(int col, int row, System.Drawing.Color colour) {
      UserControl newOne = new UserControl();
      newOne.BorderStyle = BorderStyle.FixedSingle;
      newOne.BackColor = colour;
      newOne.Dock = DockStyle.Fill;
      tlp.Controls.Add(newOne, col, row);
    }
  }
}



这篇关于tableLayoutPanel中的UserControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:57