【问题标题】:Align button in Panel control面板控件中的对齐按钮
【发布时间】:2011-08-16 10:28:08
【问题描述】:

我写了UserControl,它在Panel 控件中包含两个按钮。而且,我有几种形式。我想在表单中找到这个 UserControl 并停靠到底部。
问题是按钮向右对齐。我想离开。如何做到这一点。
PS。我正在使用 DevExpress,但这不是必需的。谢谢。

【问题讨论】:

    标签: c# winforms devexpress


    【解决方案1】:

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

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

    【讨论】:

    • 很好的解决方案。谢谢。我会试试的。
    【解决方案2】:

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

    【讨论】:

    • 如果我对每个控件都使用 Dock,那么它们会粘在一起。
    • 我相信您也可以使用扩展坞进行特定控制。那么为什么每1呢?您只是希望底部的那些按钮在右侧对齐吗??
    【解决方案3】:

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

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

    Form.cs:

    using System;
    

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

    命名空间 WindowsFormsApplication8 { 公共部分类Form1:表格 { 私有 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();
    
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2019-07-26
      • 1970-01-01
      • 2016-03-26
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 2010-12-13
      • 2017-04-07
      相关资源
      最近更新 更多