Access the containing forms control

ankit goel 746 Reputation points
2022-10-23T11:09:43.293+00:00

I am working on a project where I have a main form and inside the main form, I have a panel where I am opening a form, then closing it and opening a new form. For closing and opening, I have created a generic method which is as below

// for first time use when only form is to open and no form is to close   
     public static void showwindow(Form openwin, Panel p)  
    {  
        openwin.TopLevel = false;  
        p.Controls.Add(openwin);  
        openwin.WindowState = FormWindowState.Maximized;  
        openwin.ControlBox = false;  
        openwin.BringToFront();  
        openwin.Show();  
    }  

  // for subsequent uses when i have to close the current form, opens a new form inside the panel3   
   public static void showwindow(Form openwin , Form closewin , Panel p)  
    {  
        closewin.Close();  
        openwin.TopLevel = false;  
        p.Controls.Add(openwin);  
        openwin.WindowState = FormWindowState.Maximized;  
        openwin.ControlBox = false;  
        openwin.BringToFront();  
        openwin.Show();             
    }  

now the problem is, I am unable to access the owner form's panel when i am inside the second form . The panels name is always "panel3" .

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,371 questions
C#
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.
10,240 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ankit goel 746 Reputation points
    2022-10-29T12:43:16.473+00:00

    Thanks everyone, I found the answer on stackoverflow . I am posting here to help the new community members. It's my request to the senior members here to please spare some time to improve the below answer.

    Panel P = this.Parent as Panel;  
            if(P!=null)  
            {  
                sale_purchase sp = new sale_purchase();  
                code.logics.showwindow(sp, this, P);  
            }  
    
    
    public static void showwindow(Form openwin , Form closewin , Panel p)  
     {  
         closewin.Close();  
         openwin.TopLevel = false;  
         p.Controls.Add(openwin);  
         openwin.WindowState = FormWindowState.Maximized;  
         openwin.ControlBox = false;  
         openwin.BringToFront();  
         openwin.Show();             
     }
    
    0 comments No comments