C# show/hide 从动态创建的按钮动态创建的按钮

C# show/hide dynamic created button from dynamic created button

我有一个简单的 C# Windows 表单应用程序,我在其中创建了两组基于特定循环的按钮。第一组(称为 contractButton)是可见的,但在它们旁边创建的下一组(称为 infoButton)是隐藏的。我想要实现的是点击 contractButton 将在他旁边生成的相应 infoButton 的 visible 设置为 true。 这是我的代码,

namespace DynamicButtons
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 3; i++) //simple loop just to simulate the request
            {
                createContractButton();  //will generate 3 contractButtons with Visible = true
                createInfoButton();  //will cgenerate 3 infoButtons with Visible = false
            }
        }
        private void createInfoButton()
        {
            Button infoButton = infoButtonCreration("infoButton");
            flowLayoutPanel1.Controls.Add(infoButton);
            infoButton.Click += new EventHandler(btnInfo_Click);
        }
        private void createContractButton()
        {
            Button contractButton = contractButtonCreation("contractButton");
            flowLayoutPanel1.Controls.Add(contractButton);
            contractButton.Click += new EventHandler(btn_Click);
        }
        Button contractButtonCreation(string contract)
        {
            Button b = new Button();

            b.FlatAppearance.BorderSize = 1;
            b.FlatStyle = FlatStyle.Flat;
            b.Name = contract;
            b.Size = new Size(150, 80);
            b.Text = contract;
            b.UseVisualStyleBackColor = false;

            return b;
        }
        Button infoButtonCreration(string info)
        {
            Button b = new Button();

            b.FlatAppearance.BorderSize = 1;            
            b.FlatStyle = FlatStyle.Flat;
            b.Name = info;
            b.Size = new Size(70, 80);
            b.Text = info;
            b.UseVisualStyleBackColor = false;

            b.Visible = false;         //this with make the button hidden

            return b;
        }
        private void btn_Click(object sender, EventArgs e)
        {
            //here i need the magic, on click i need the infoButton nect to me to be visible

        }
        private void btnInfo_Click(object sender, EventArgs e)
        {
            Button b = (sender as Button);
            b.Text = "name"; //simple even when button gets visible so i can test it woeks
        }
    }
}

我希望我解释了我的需求。
非常感谢你们的支持。
亲切的问候

how it should look when working

在 contractButton 上添加标签,然后当您单击按钮时将标签转换为按钮。

namespace DynamicButtons
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 3; i++) //simple loop just to simulate the request
            {
                createContractButton();                    
            }
        }
        private Button createInfoButton()
        {
            Button infoButton = infoButtonCreration("infoButton");
            flowLayoutPanel1.Controls.Add(infoButton);
            infoButton.Click += new EventHandler(btnInfo_Click);
            return infoButton;
        }
        private void createContractButton()
        {
            Button contractButton = contractButtonCreation("contractButton");
            flowLayoutPanel1.Controls.Add(contractButton);
            contractButton.Click += new EventHandler(btn_Click);

            contractButton.Tag=createInfoButton();

        }
        Button contractButtonCreation(string contract)
        {
            Button b = new Button();

            b.FlatAppearance.BorderSize = 1;
            b.FlatStyle = FlatStyle.Flat;
            b.Name = contract;
            b.Size = new Size(150, 80);
            b.Text = contract;
            b.UseVisualStyleBackColor = false;

            return b;
        }
        Button infoButtonCreration(string info)
        {
            Button b = new Button();

            b.FlatAppearance.BorderSize = 1;
            b.FlatStyle = FlatStyle.Flat;
            b.Name = info;
            b.Size = new Size(70, 80);
            b.Text = info;
            b.UseVisualStyleBackColor = false;

            b.Visible = false;         //this with make the button hidden

            return b;
        }
        private void btn_Click(object sender, EventArgs e)
        {
            ((Button)((sender as Button)?.Tag)).Visible = true;            
        }
        private void btnInfo_Click(object sender, EventArgs e)
        {
            Button b = (sender as Button);
            b.Text = "name"; //simple even when button gets visible so i can test it woeks
        }
    }
}

做这样的事情时,我更喜欢创建一个自定义 class 从 System.Windows.Forms.Button class 派生并向其添加属性这有助于我更轻松地进行后续通话。

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

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 3; i++) //simple loop just to simulate the request
        {
            createContractButton();  //will generate 3 contractButtons with Visible = true
            // createInfoButton(); - Don't do this here
        }
    }
    private Button createInfoButton()
    {
        Button infoButton = infoButtonCreration("infoButton");
        flowLayoutPanel1.Controls.Add(infoButton);
        infoButton.Click += new EventHandler(btnInfo_Click);

        return infoButton;
    }
    private void createContractButton()
    {
        MyCustomButton contractButton = contractButtonCreation("contractButton");
        flowLayoutPanel1.Controls.Add(contractButton);
        contractButton.Click += new EventHandler(btn_Click);

        // Create new infoButton here, tied directly to the ContractButton you have just created
        contractButton.InfoButton = this.createInfoButton();
    }
    MyCustomButton contractButtonCreation(string contract)
    {
        MyCustomButton b = new MyCustomButton();

        b.FlatAppearance.BorderSize = 1;
        b.FlatStyle = FlatStyle.Flat;
        b.Name = contract;
        b.Size = new Size(150, 80);
        b.Text = contract;
        b.UseVisualStyleBackColor = false;

        return b;
    }
    Button infoButtonCreration(string info)
    {
        Button b = new Button();

        b.FlatAppearance.BorderSize = 1;
        b.FlatStyle = FlatStyle.Flat;
        b.Name = info;
        b.Size = new Size(70, 80);
        b.Text = info;
        b.UseVisualStyleBackColor = false;

        b.Visible = false;         //this with make the button hidden

        return b;
    }
    private void btn_Click(object sender, EventArgs e)
    {
        //here i need the magic, on click i need the infoButton nect to me to be visible
        MyCustomButton button = sender as MyCustomButton;
        button.InfoButton.Visible = true;
    }
    private void btnInfo_Click(object sender, EventArgs e)
    {
        Button b = (sender as Button);
        b.Text = "name"; //simple even when button gets visible so i can test it woeks
    }
}

public class MyCustomButton : Button
{
    public Button InfoButton { get; set; }
}

MyCustomButton class 的工作方式与普通按钮完全一样,但有一个很好的自定义 属性 来访问 InfoButton 或您要添加的任何其他 属性 的额外好处稍后再说。

一般情况下,隐藏按钮需要参考。如果你不想做按钮变量,你可以为 contractButton 添加标签,位置在 flowLayoutPanel1.control。在 OnClick 事件中,contractButton 获取标签并添加 1 并从 flowLayoutPanel1.controls 面板获取按钮并更改 Visible = true

private void btn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    var hiddenBtn = flowLayoutPanel1.controls[btn.Tag + 1];
    hiddenBtn.Visible = true;
}

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 3; i++) 
    {
       createContractButton(i);  
       createInfoButton();  
    }
}

private void createContractButton(int i)
{
    Button contractButton = contractButtonCreation("contractButton");
    contractButton.Tag = i
    flowLayoutPanel1.Controls.Add(contractButton);
    contractButton.Click += new EventHandler(btn_Click);
 }

注意:b.Name必须是唯一的