-3

I am writing an industrial program in c#. Since there are too many operations in the program, I placed multiple forms inside the form on the main page. But I cannot make any changes to the Main form using any control in the sub-form. What is the reason? Should I use Invoke?

This is my main form :

 private void frmMain_Shown(object sender, EventArgs e)
    {   

        frmMeasure frmMeasure = new frmMeasure();
        frmMeasure.TopLevel = false;
        frmMeasure.Dock = DockStyle.Fill;
        frmMeasure.Visible = true;
        pnlMain.Controls.Clear();
        pnlMain.Controls.Add(frmMeasure);
   }

This is sub-form and not working .

private void btnSettings_Click(object sender, EventArgs e)
    {

        frmMain frmMain = new frmMain();

        frmProductSettings frmProductSettings = new frmProductSettings();
        frmProductSettings.TopLevel = false;
        frmProductSettings.Dock = DockStyle.Fill;
        frmProductSettings.Visible = true;
        frmMain.pnlMain.Controls.Clear();
        frmMain.pnlMain.Controls.Add(frmProductSettings);
    }

My aim is to change the sub-forms by using the button in the sub-form that I opened in the main form. Thank you.

ugurrrn
  • 3
  • 4
  • 1
    `frmMain frmMain = new frmMain();` is a complete new object of frmMain. It is not the same as the one you already opened – Chetan Jun 18 '22 at 08:50
  • I created it in the button event inside the sub-form. It's not the same, sir. Information is being exchanged between 2 forms. There is a situation here, more than one sub-form is working in the Main form and I want to change the other sub-form by using the button in these sub-forms. – ugurrrn Jun 18 '22 at 08:55
  • As already noted… “why” is the code in the “sub” form creating a NEW main form with … `frmMain frmMain = new frmMain();` … ? … and then it is never shown? This same idea would apply to all the forms in your code. `frmMeasure frmMeasure = new frmMeasure();` … and … `frmProductSettings frmProductSettings = new frmProductSettings();` … may well be created, however they are NEVER displayed to the user. – JohnG Jun 18 '22 at 09:47
  • You did not fully understand the problem,These are Sub-forms. – ugurrrn Jun 18 '22 at 09:57
  • See [Application.OpenForms](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.openforms?view=windowsdesktop-6.0). You should use [UserControl](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.usercontrol?view=windowsdesktop-6.0) for what you call Sub-Forms. – dr.null Jun 18 '22 at 10:03

2 Answers2

0

You can use SetParent function to display form inside another form.

using System.Runtime.InteropServices;


[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

 private void frmMain_Shown(object sender, EventArgs e)
 {
      frmMeasure frmMeasure = new frmMeasure();
      frmMeasure.TopLevel = false;
      frmMeasure.WindowState = System.Windows.Forms.FormWindowState.Maximized;
      frmMeasure.Visible = true;
      frmMeasure.Show();
      SetParent(frmMeasure.Handle, pnlMain.Handle);
 }
0

You can use the ParentForm property to get a reference to the main form from within the sub form:

// ... from within a sub-form ...
private void btnSettings_Click(object sender, EventArgs e)
{
    frmMain main = this.ParentForm as frmMain;
    frmProductSettings frmProductSettings = new frmProductSettings();
    frmProductSettings.TopLevel = false;
    frmProductSettings.Dock = DockStyle.Fill;
    frmProductSettings.Visible = true;
    main.pnlMain.Controls.Clear();
    main.pnlMain.Controls.Add(frmProductSettings);
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40