WinForms FlowLayoutPanel对齐问题

WinForms FlowLayoutPanel alignment problem

当我单击 Floqlayout 面板中的按钮时,它们应该隐藏在我单击它们的位置。但是它们消失了,所有其他按钮都移动了。

他们应该躲在他们的地方

但这就是发生的事情

我如何创建按钮:

 private void CreateButton()
    {
        int buttonIndex = 0;
        for (int i = 0; i < 16; i++)
        {
            Button button = new Button();
            button.Name = $"Button_{buttonIndex}";
            button.Width = 100;
            button.Height = 100;
            button.Click += OnButtonClick;
            button.BackgroundImage = BackSideImage();

            flowLayoutPanel1.Controls.Add(button);

            buttonIndex++;
        }
    }

我如何隐藏我的按钮:

private void CompareCards()
    {
        if (clickedCards.Count >= 3)
        {

                if (clickedCards[0].PairIndex == clickedCards[1].PairIndex)
                {
                    clickedCards[0].Button.Hide();
                    clickedCards[1].Button.Hide();
                }
                else
                {
                   clickedCards[0].Button.BackgroundImage = BackSideImage();
                   clickedCards[1].Button.BackgroundImage = BackSideImage();
                }

                clickedCards.Clear();
        }
    }

您可以像这样让它不可见,而不是隐藏您的按钮:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        for (int x = 0; x < 9; x++)
        {
            var button = new Button
            {
                Name = "Test-" + x,
                Text = "Test-" + x,
                Width = 100,
                Height = 100
            };

            button.Click += OnButtonClick;
      flowLayoutPanel1.Controls.Add(button);
        }
    }

    private void OnButtonClick(object sender, EventArgs e)
    {
        //Instead of this...
        //((Button)sender).Hide();

        //Do this...
        var button = ((Button) sender);
        button.FlatStyle = FlatStyle.Flat;
        button.FlatAppearance.BorderColor = BackColor;
        button.FlatAppearance.MouseOverBackColor = BackColor;
        button.FlatAppearance.MouseDownBackColor = BackColor;
        button.Text = string.Empty;
        button.Enabled = false;
    }
}