C#显示/隐藏动态创建按钮的动态创建按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#显示/隐藏动态创建按钮的动态创建按钮相关的知识,希望对你有一定的参考价值。

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

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
        }
    }
}
另一答案

在做这样的事情时,我更喜欢创建一个派生自System.Windows.Forms.Button类的自定义类,并为其添加属性,以帮助我更轻松地进行后续调用。

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类的工作方式与普通的Button完全相同,但还有一个好处是可以使用一个好的自定义属性来访问InfoButton或以后要添加的任何其他属性。

另一答案

通常,您需要隐藏按钮的参考。如果您不想执行按钮变量,可以为contractButton添加tagLayoutPanel1.control中的位置标记。在事件上点击contractButton获取标记并从flowLayoutPanel1.controls面板添加1并获取按钮并更改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必须是唯一的

以上是关于C#显示/隐藏动态创建按钮的动态创建按钮的主要内容,如果未能解决你的问题,请参考以下文章

为每个具有类名的 div 动态创建按钮,并创建显示/隐藏单击功能

需要在按钮单击时隐藏/取消隐藏动态创建的下拉列表

使用片段的android studio中不显示动态按钮

为按钮 OnClick 动态创建文件名

片段中的按钮自定义视图

根据状态动态更改登录/注销按钮