在面板控件中对齐按钮

时间:2011-08-16 10:28:09

标签: c# winforms devexpress

我写了UserControl,其中包含Panel控件中的两个按钮。而且,我有几种形式。我想在表格中找到这个UserControl并停靠在底部 问题是按钮向右对齐。我想离开。怎么做。
enter image description here PS。我正在使用DevExpress,但没有必要。感谢。

3 个答案:

答案 0 :(得分:2)

winforms AFAIK中的每个控件都有一个名为dock的属性。用它。 或者,您可以使用此usercontrol的X和Y位置值进行设置。但是在调整表单大小时,您还必须重新定位控件。

答案 1 :(得分:2)

这里有些解决方案可能是最佳的,但我的工作

  1. 1-您可以将每个按钮放在一个单独的小面板中并进行更改 它将这些两个面板填充并对接到父面板
  2. 2-你可以使用更简单的分隔符并将其颜色设置为 父面板颜色(并将它们停靠在右侧)。>从右到左:按钮分割按钮。

答案 2 :(得分:1)

我会将按钮放在1行2列tablelayoutpanel中,其中行高=自动调整大小,第一列的宽度为100%,第二列的宽度为自动调整大小。然后,我会将按钮添加到每个列,并将锚点设置为每个列的右上角。

我还提供了执行此操作的表单的代码。

Form.cs:

using System;

使用System.Collections.Generic; 使用System.ComponentModel; 使用System.Data; 使用System.Drawing; 使用System.Text; 使用System.Windows.Forms;

命名空间WindowsFormsApplication8 {     公共部分类Form1:表格     {         private System.Windows.Forms.DataGridView dataGridView1;         private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;         private System.Windows.Forms.Button button1;         private System.Windows.Forms.Button button2;

    public Form1()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        this.tableLayoutPanel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(620, 269);
        this.dataGridView1.TabIndex = 0;
        // 
        // tableLayoutPanel1
        // 
        this.tableLayoutPanel1.AutoSize = true;
        this.tableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.tableLayoutPanel1.ColumnCount = 2;
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
        this.tableLayoutPanel1.Controls.Add(this.button1, 1, 0);
        this.tableLayoutPanel1.Controls.Add(this.button2, 0, 0);
        this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Bottom;
        this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 240);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        this.tableLayoutPanel1.RowCount = 1;
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
        this.tableLayoutPanel1.Size = new System.Drawing.Size(620, 29);
        this.tableLayoutPanel1.TabIndex = 1;
        // 
        // button1
        // 
        this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.button1.Location = new System.Drawing.Point(542, 3);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        // 
        // button2
        // 
        this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
        this.button2.Location = new System.Drawing.Point(461, 3);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(75, 23);
        this.button2.TabIndex = 1;
        this.button2.Text = "button2";
        this.button2.UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(620, 269);
        this.Controls.Add(this.tableLayoutPanel1);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        this.tableLayoutPanel1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }
}

}