简体   繁体   English

WinForms FlowLayoutPanel对齐问题

[英]WinForms FlowLayoutPanel alignment problem

When I click on a button in my Floqlayoutpanel they should hide at the place where I clicked on them. 当我单击Floqlayoutpanel中的按钮时,它们应该隐藏在我单击它们的地方。 But instead they disappear and all the other buttons move. 但是相反,它们消失了,所有其他按钮都移动了。

They should hide at their place 他们应该躲在自己的地方

他们应该躲在自己的地方

But this is what happens 但这是发生了什么

但这是发生了什么

How I create my Buttons: 我如何创建按钮:

 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++;
        }
    }

How I hide my Buttons: 我如何隐藏按钮:

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();
        }
    }

Instead of hiding your button, you can make it invisible like this: 除了隐藏您的按钮,您还可以这样使其不可见:

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;
    }
}

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

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