![](/img/trans.png)
[英]How to set height to the dynamically added user control in stackpanel?
[英]How to set the size of an inner control of a user control dynamically
我制作了一个用户控件,其中包含2个简单控件:一个复选框和一个组合框。 (以及其中的某些副本,其中包含一个复选框和一个文本框,或者一个复选框和一个IBAN控件vs ...)
当我在设计器模式下使用此用户控件时,更改用户控件的大小不会自然地更改内部控件的大小。 我必须在页面中设置它们的大小,而在实际类中却在设计器类中使用用户控件。 我的目的是仅通过更改用户控件的宽度来更改那些控件的宽度。 我的意思是:
我们将其称为ucControl
控件,及其内部控件cbCheckBox
和cmbComboBox。 创建此用户控件时,我为所有这些控件设置了静态大小,除了ucControl
大小外,其余大小均不可用于设计者更改大小。
我想要的大小cmbComboBox
时的尺寸变化ucControl
根据像式变化,:
cmbComboBox.Size = new Size(ucControl.Size.Width - cbCheckBox.Size.Width - 15, 20)
我应该如何在哪里做?
到目前为止我尝试过的是:
我尝试使用SizeChanged
事件,但是没有用。 (它不允许我在用户控件中创建一个void returning事件方法,不知道为什么。)
我试图在load
方法中设置它,但是没有用。
我试图在设计类的InitializeComponent
方法中设置它,但没有用。
解决此问题的最佳方法是使用容器,并使用“填充”选项制作控件Dock。 这样,它将为您动态调整大小。 您也可以将其左右固定,但我发现容器是更优雅的选择。 下面的示例使用一个简单的TableLayoutPanel,其中固定了一些行和列。
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;
}
只需将ComboBox锚定到左侧和右侧即可实现您想要的功能。
这是将ComboBox添加到其中之后的UserControl:
选择ComboBox并拖动其Right Edge,直到它距UserControl的Right Edge所需的距离为止:
更改ComboBox的Anchor属性,然后打开Right Anchor,以便同时打开Left和Right:
现在尝试调整UserControl的大小,看看ComboBox会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.