我制作了一个用户控件,其中包含2个简单控件:一个复选框和一个组合框。 (以及包含复选框和文本框的一些副本,或者复选框和IBAN控件vs ...)
当我在设计器模式下使用此用户控件时,更改用户控件的大小不会自然地更改内部控件的大小。我必须在我在实际类中使用用户控件但设计器类的页面中设置它们的大小。我的目标是仅通过更改用户控件的宽度来更改这些控件的宽度。我的意思是:
让我们调用我们的控件ucControl
及其内部控件cbCheckBox
和cmbComboBox。当我创建这个用户控件时,我为所有这些控件设置了一个静态大小,除ucControl
的大小外,其余的大小不能用于设计师的大小更改。
我希望cmbComboBox
的大小在ucControl
的大小发生变化时根据以下公式进行更改:
cmbComboBox.Size = new Size(ucControl.Size.Width - cbCheckBox.Size.Width - 15, 20)
我应该怎样以及在哪里这样做?
到目前为止我尝试了什么:
我尝试使用SizeChanged
事件,但它无效。 (它不允许我在用户控件中创建一个void返回事件方法,不知道为什么。)
我尝试在load
方法中设置它,但它不起作用。
我尝试在设计类的InitializeComponent
方法中设置它,但它不起作用。
答案 0 :(得分:1)
解决此问题的最佳方法是使用容器并使用“填充”选项创建控件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;
}
答案 1 :(得分:1)
简单地将ComboBox锚定到左侧和右侧应该可以达到您想要的效果。
这是ComboBox添加到其后的UserControl:
选择ComboBox并拖动其右边缘,直到距离UserControl的右边缘所需的距离为止:
更改ComboBox的Anchor属性并打开右锚,以便同时打开左侧和右侧:
现在尝试调整UserControl的大小,看看ComboBox会发生什么。