Finding parent form and tablelayoutPanel

Shaifali jain 420 Reputation points
2023-09-26T10:44:39.0333333+00:00

hello ,

 private void TDlloading_error_Load(object sender, EventArgs e)
        {
            HideButtonsInParentFormTableLayoutPanel();
        }
        private void HideButtonsInParentFormTableLayoutPanel()
        {
            // Access the parent form
            Form parentForm = this.ParentForm;

            // Check if the parent form exists and contains a TableLayoutPanel
            if (parentForm != null && parentForm.Controls.ContainsKey("tableLayoutPanel1"))
            {
                TableLayoutPanel tableLayoutPanel = parentForm.Controls["tableLayoutPanel1"] as TableLayoutPanel;

                // Hide the buttons in the TableLayoutPanel
                if (tableLayoutPanel != null)
                {
                    foreach (Control control in tableLayoutPanel.Controls)
                    {
                        if (control is Button button)
                        {
                            button.Visible = false;
                        }
                    }
                }
            }
        }


i am trying to hide all the buttons in the tablelayout panel of parent form but i am getting this.parentform as null as my current form is hosted itself inside a panel . so how do i access the parentform in which the panel is hosted .

Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. KOZ6.0 6,735 Reputation points
    2023-09-27T15:04:40.62+00:00

    mainmaster.RecreateCenterForm<T>

         newForm.Show();
         Mainmasterpanel.Controls.Add(newForm);
    

    Please correct this part. (Controls.Add → Show)

    Make this method public or internal and call it from TDlloading_error.
    These are the same, right?

    private void RecreateCenterForm<T>() where T : Form, new()
    {
         mainmaster master = (mainmaster)this.Parent.FindForm();
         master.RecreateCenterForm<T>
    }
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.