首页 > 解决方案 > 如何在另一个按钮旁边生成一个按钮?

问题描述

我使用自定义按钮创建了单独的类,我希望在单击此“newButton”时弹出该类。我该如何手动完成?所以如果有多个“newButtons”并且我想隐藏以前的自定义按钮我的问题是我不知道如何寻找新按钮的位置,因为它位于自动放置它们的 flowLayout 中。

Button newButton = new Button();
newButton.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(50)))), ((int)(((byte)(175)))), ((int)(((byte)(241)))));
newButton.FlatAppearance.BorderSize = 2;
newButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
newButton.Font = new System.Drawing.Font("Arial", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
newButton.ForeColor = System.Drawing.Color.White;
newButton.Location = new System.Drawing.Point(158, 288);
newButton.Size = new System.Drawing.Size(150, 38);
newButton.UseVisualStyleBackColor = true;
newButton.Text = k.grp;

newButton.Click += (sender, e) => newButton_Click(sender, e);

if (checkinNames.Contains(newButton.Text))
{ }
else
    flowLayoutPanel5.Controls.Add(newButton);

checkinNames.Add(k.grp);

标签: c#winformsbuttonlocationlogic

解决方案


您无法直接在 FlowLayoutPanel 中通过特定属性来实现它。放置在其中的所有可视控件都会根据 FlowLayoutPanel 的大小自动调整其位置。

这是通过设置按钮的解决方法border, backcolor, text and enable

private void HideButton(Button button)
{
    button.BackColor = this.BackColor;
    button.Text = string.Empty;
    button.TabStop = false;
    button.FlatStyle = FlatStyle.Flat;
    button.FlatAppearance.BorderSize = 0;
    button.Enabled = false;
}

希望这可以帮到你。


推荐阅读