Search code examples
c#buttondynamiccontrolspanel

c# control dynamically created panel by dynamically created button


i've added a button that creates dynamically another buttons(lets call it accountButton) that accountButtons can adds dynamically panel and another button(exitButton) into panel

i cannot make dynamically created panel turn visible or invisible via accountButton


        private void account_save_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 1; i++)
            {
                BunifuFlatButton accountButton = addButton(i); // Hesap Butonu
                Panel accountPanel = addPanel(i); // Hesap Paneli
                BunifuImageButton exitButton = addexitButton(i); // Hesap Exit Butonu

                panel_accounts.Controls.Add(accountButton); // MainPanel'e eklenen Hesap butonu
                accountButton.Click += new EventHandler(this.accountButtonClick); // MainPanel'e eklenen Hesap butonu islevi
                this.Controls.Add(accountPanel); // Main'e eklenen Hesap paneli
                accountPanel.Controls.Add(exitButton); // Hesap paneli'ne eklenen Exit butonu
                exitButton.Click += new EventHandler(this.exitButtonClick); // Hesap paneli'ne eklenen Exit butonu islevi
            }
        }
        private void accountButtonClick(object sender, EventArgs e)
        {
            BunifuFlatButton currentaccountButton = (BunifuFlatButton)sender;

            Panel currentPanel = (Panel)sender;
            currentPanel.Visible = true;
        }

Solution

  • This line will not work:

    Panel currentPanel = (Panel)sender;
    

    because the sender is the button that is clicked, not the panel. Just a line above, you cast the exact same sender as a button.

    If the button is inside the panel, you can do this:

     Panel currentPanel = (Panel)currentaccountButton.NamingContainer;