2015-01-05 50 views
-1

我已经做了一个用户控件,其中包含2个简单的控件:复选框和组合框。 (和它的一些副本,其中包含一个复选框和一个文本框,或一个复选框和IBAN控制vs ...)如何动态设置用户控件的内部控件的大小

当我在设计者模式下使用此用户控件时,更改用户控件的大小不会更改内部控制的大小自然而然。我必须在页面中设置它们的大小,我在实际的类中使用用户控件,但在设计器类中。我的目标是通过改变用户控件的宽度来改变这些控件的宽度。我的意思是:

让我们打电话给我们的控制ucControl,其内部控件cbCheckBox和cmbComboBox。创建此用户控件时,我为所有这些控件设置了一个静态大小,除了大小ucControl之外,其余大小不适用于从设计器更改大小。

我想cmbComboBox的大小改变时的ucControl的大小而变化,根据就像一个公式:

cmbComboBox.Size = new Size(ucControl.Size.Width - cbCheckBox.Size.Width - 15, 20)

如何,我应该在哪里呢?

我试过到目前为止:

我试图用SizeChanged事件,但没有奏效。 (它不让我在用户控件中创建一个void returns事件方法,不知道为什么。)

我试图在load方法中设置它,但它不起作用。

我试图在设计类的InitializeComponent方法中设置它,但它没有工作。

+2

锚定这些子控件。 – LarsTech

+0

它在用户控件的设计器中工作,但不是以我使用该用户控件的形式工作。 –

+0

忘记锚..使用容器:) – Darek

回答

1

解决此问题的最佳方法是使用容器并使用控件Dock填充选项。这样它会为你动态调整大小。你也可以将它锚定到左侧和右侧,但是我发现容器是更优雅的选择。下面的示例使用了一个简单的TableLayoutPanel,并修复了一些行和列。

enter image description here

partial class Form1 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); 
     this.label1 = new System.Windows.Forms.Label(); 
     this.checkBox1 = new System.Windows.Forms.CheckBox(); 
     this.label2 = new System.Windows.Forms.Label(); 
     this.textBox1 = new System.Windows.Forms.TextBox(); 
     this.tableLayoutPanel1.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // tableLayoutPanel1 
     // 
     this.tableLayoutPanel1.ColumnCount = 2; 
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 120F)); 
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); 
     this.tableLayoutPanel1.Controls.Add(this.label2, 0, 1); 
     this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0); 
     this.tableLayoutPanel1.Controls.Add(this.checkBox1, 1, 0); 
     this.tableLayoutPanel1.Controls.Add(this.textBox1, 1, 1); 
     this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); 
     this.tableLayoutPanel1.Name = "tableLayoutPanel1"; 
     this.tableLayoutPanel1.RowCount = 3; 
     this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 26F)); 
     this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 26F)); 
     this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); 
     this.tableLayoutPanel1.Size = new System.Drawing.Size(412, 198); 
     this.tableLayoutPanel1.TabIndex = 0; 
     // 
     // label1 
     // 
     this.label1.AutoSize = true; 
     this.label1.Dock = System.Windows.Forms.DockStyle.Right; 
     this.label1.Location = new System.Drawing.Point(62, 3); 
     this.label1.Margin = new System.Windows.Forms.Padding(3); 
     this.label1.Name = "label1"; 
     this.label1.Size = new System.Drawing.Size(55, 20); 
     this.label1.TabIndex = 0; 
     this.label1.Text = "Checkbox"; 
     // 
     // checkBox1 
     // 
     this.checkBox1.AutoSize = true; 
     this.checkBox1.Dock = System.Windows.Forms.DockStyle.Left; 
     this.checkBox1.Location = new System.Drawing.Point(123, 3); 
     this.checkBox1.Name = "checkBox1"; 
     this.checkBox1.Size = new System.Drawing.Size(80, 20); 
     this.checkBox1.TabIndex = 1; 
     this.checkBox1.Text = "checkBox1"; 
     this.checkBox1.UseVisualStyleBackColor = true; 
     // 
     // label2 
     // 
     this.label2.AutoSize = true; 
     this.label2.Dock = System.Windows.Forms.DockStyle.Right; 
     this.label2.Location = new System.Drawing.Point(71, 29); 
     this.label2.Margin = new System.Windows.Forms.Padding(3); 
     this.label2.Name = "label2"; 
     this.label2.Size = new System.Drawing.Size(46, 20); 
     this.label2.TabIndex = 2; 
     this.label2.Text = "TextBox"; 
     // 
     // textBox1 
     // 
     this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.textBox1.Location = new System.Drawing.Point(123, 29); 
     this.textBox1.Name = "textBox1"; 
     this.textBox1.Size = new System.Drawing.Size(286, 20); 
     this.textBox1.TabIndex = 3; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(412, 198); 
     this.Controls.Add(this.tableLayoutPanel1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.tableLayoutPanel1.ResumeLayout(false); 
     this.tableLayoutPanel1.PerformLayout(); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 
    private System.Windows.Forms.Label label2; 
    private System.Windows.Forms.Label label1; 
    private System.Windows.Forms.CheckBox checkBox1; 
    private System.Windows.Forms.TextBox textBox1; 
} 
1

简单地固定组合框的左侧和右侧应达到你想要什么。

这里是用户控件后,右边的组合框添加到它:

ComboBox in the UserControl

选择组合框并拖动其右边缘,直到它从用户控件的右边缘所需的距离:

ComboBox resized so that it fills the width of the UserControl

更改组合框的锚定属性并打开右侧锚点,以便打开左侧和右侧:

ComboBox with Left and Right Anchor turned on

现在尝试调整大小UserControl和看看会发生什么到ComboBox。

+0

它在用户控件本身的设计器中调整用户控件大小时起作用,但不是以我使用它的形式调整大小。 –

+0

最有可能的是,你没有UserControl本身的设置,它会被Form调整大小。它也需要停靠或锚定,以便调整大小(或者需要配置表单中的其他容器)。 –

相关问题