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

Posted

技术标签:

【中文标题】C# 从动态创建的按钮中显示/隐藏动态创建的按钮【英文标题】:C# show/hide dynamic created button from dynamic created button 【发布时间】:2018-02-28 22:22:42 【问题描述】:

我有一个简单的 C# Windows 窗体应用程序,我在其中基于特定循环创建了两组按钮。第一个集合(称为contractButton)是可见的,但在它们旁边创建的下一个集合(称为infoButton)是隐藏的。我想要实现的是点击 contractButton 以将其旁边生成的相应 infoButton 的可见设置为 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

【问题讨论】:

遍历控件并找到您的按钮或按住实例。 您可以只使用 1 个按钮并在单击时更改其内容,在 btn_Click() 中您检查内容文本是什么(或您设置的任何其他标志),如果它的“contractButton”然后更改它到“infoButton”,如果它的“infoButton”执行你需要的任务...... 【参考方案1】:

在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
        
    

【讨论】:

你好 Henoc,这个例子非常适合我,很容易实现我的需要。谢谢你的帮助。【参考方案2】:

在执行此类操作时,我更喜欢创建一个派生自 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 或您以后想要添加到它的任何其他属性的额外好处。

【讨论】:

JayV,感谢您的支持,这个例子也可以。【参考方案3】:

一般来说,隐藏按钮需要参考。如果您不想做按钮变量,您可以在 flowLayoutPanel1.control 中添加带有位置的 contractButton 标记。在事件 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 必须是唯一的

【讨论】:

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

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

每次新创建动态图都需要点击新建按钮吗

WPF C#为动态创建的按钮创建Click事件

MFC SDI中 如何为动态创建的按钮添加消息处理函数

如何从动态创建的按钮中获取属性?

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