繁体   English   中英

面板控件中的对齐按钮

[英]Align button in Panel control

我已经编写了UserControl ,它在Panel控件中包含两个按钮。 而且,我有几个 forms。 我想在 forms 中找到这个UserControl并停靠到底部。
问题是按钮向右对齐。 我想离开。 这个怎么做。
在此处输入图像描述 PS。 我正在使用 DevExpress,但这不是必需的。 谢谢。

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

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

  1. 1-您可以在一个单独的小面板中制作每个按钮并更改它的填充并将这些拖曳面板停靠到父面板
  2. 2-您可以使用更容易的分隔符并将其颜色设置为父面板颜色(并将它们也停靠在右侧)。> 从右到左:按钮拆分器按钮。

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

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

表格.cs:

using System;

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

命名空间 WindowsFormsApplication8 { public partial class Form1: Form { private System.Windows.Forms.DataGridView dataGridView1; 私有 System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; 私有 System.Windows.Forms.Button button1; 私有 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();

    }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM