各位大虾好。我最近开发的项目中,需要用到一个User Control。这个User Control很简单,由一个在上方的ListBox和一个在下面的Label组成。ListBox用于显示一组信息,而Label用于显示其他相关信息。ListBox位于User Control的左上角(0,0),宽度与User Control相同。Label的高度固定,为20,宽度也与User Control相同。要求当User Control大小改变时,两个子控件的宽度也同时调整,始终与User Control宽度一致;而ListBox的高度应该始终等于User Control的高度减去Label的高度;而Label则始终位于紧邻ListBox的下方。也就是说,这两个子控件的高度之和始终要等于User Control的高度,这样这两个子控件将始终完全覆盖User Control。但是,我使用下面的代码(下面的代码与实际VS中的代码不完全相同,但主要逻辑代码一致),却发现当我改变User Control的尺寸时,两个子控件的宽度调整正常,而ListBox的高度却不能即时变化,而是大约要累积到一定程度的时候,才会一次性的变化。我审阅代码,进行调试,也没有找到问题所在。恳请各位帮忙。代码如下:    //在MyUserControl.cs中,包含自定义User Control的定义
    public partial class MyUserControl : UserControl
    {
        public MyUserControl()
        {
            InitializeComponent();            //有无下面这条语句对结果没有影响
            this.AutoSize = false;            this.listBox1.Location = new Point(0, 0);
            //有无下面这条语句对结果没有影响
            this.listBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left;
            this.label1.Height = 20;
            this.listBox1.Size = new Size(this.Width, this.Height - this.label1.Height);
            this.label1.Location = new Point(0, this.listBox1.Bottom);
            //有无下面这条语句对结果没有影响
            this.label1.Anchor = AnchorStyles.Top | AnchorStyles.Left;
            this.label1.Width = this.Width;        }        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.BackColor = System.Drawing.Color.Green;
            this.listBox1.FormattingEnabled = true;
            this.listBox1.ItemHeight = 12;
            this.listBox1.Location = new System.Drawing.Point(0, 0);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(263, 292);
            this.listBox1.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.Color.Gold;
            this.label1.Location = new System.Drawing.Point(0, 292);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(263, 20);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            // 
            // MyUserControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.Red;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Controls.Add(this.label1);
            this.Controls.Add(this.listBox1);
            this.Name = "MyUserControl";
            this.Size = new System.Drawing.Size(261, 308);
            this.ResumeLayout(false);        }        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            
            //在我调试的时候,发现当运行到下面第二条语句的时候,this.listBox1.Size.Height的值并不等于
            //this.Height - this.label1.Height,正因此,导致我前面提到的问题。不知道为什么。
            this.listBox1.Size = new Size(this.Width, this.Height - this.label1.Height);
            this.label1.Top = this.listBox1.Bottom;
            this.label1.Width = this.Width;
        }
    }
    
    //在Form1.cs中,包含主窗体,用于测试此User Control。
    public partial class Form1 : Form
    {
        ...
        
        //增加User Control的尺寸
        private void btnLarger_Click(object sender, EventArgs e)
        {
            this.myUserControl1.Size = new Size(this.myUserControl1.Width + 2, this.myUserControl1.Height + 2);
        }        //减小User Control的尺寸
        private void btnSmaller_Click(object sender, EventArgs e)
        {
            this.myUserControl1.Size = new Size(this.myUserControl1.Width - 2, this.myUserControl1.Height - 2);
        }
    }

解决方案 »

  1.   

    .NET中界面控件布局基本上是不需要写代码的,只需要设置Anchor属性即可,很方便的
      

  2.   

    ...而ListBox的高度却不能即时变化
    这个问题你可以先试一下this.listBox1.IntegralHeight = false;
      

  3.   

    例如,对于搂主的这个UserControl,可以这样设置:
    1、设置ListBox控件的Anchor为:
       this.listBox1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;2、设置Label控件的Anchor为:
       this.label1.Anchor = AnchorStyles.Left | AnchorStyles.Right;把这两句代码添加到InitializeComponent函数的末尾。注意:也要注释掉你在OnResize里面的代码。
      

  4.   

    label1.Dock=DockStyle.Bottom;
    listBox1.Dock=DockStyle.Fill;
      

  5.   

    非常感谢gomoku。你的方案的确管用。我在调试的时候就想到可能有什么机制导致ListBox自动调整了尺寸,但我完全没有考虑到这一点。也非常感谢net5i和ICanUseThisID,尤其是net5i细致的指导,感谢你们的帮助。你们提出的使用Anchor属性代替代码的方法的确很好。不过,首先也必须设置ListBox的IntegralHeight属性为false,否则效果和我提供的代码是一样的。再次感谢各位!