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

时间:2018-02-28 22:22:42

标签: c# button programmatically-created

我有一个简单的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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在做这样的事情时,我更喜欢创建一个派生自 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或稍后要添加的任何其他属性。

答案 2 :(得分:0)

通常,您需要隐藏按钮的参考。如果您不想执行按钮变量,可以为contractButton添加tagLayoutPanel1.control中的位置标记。 On事件OnClick the 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必须是唯一的