0

i tried to create a form instance with in another form and then add that form into main form. but the form which i added that is not showing. i want to show that form at center at top of all controls.

here is my code

BBA.Controls.ExecludeSpecialist ucExecludeSpecialist = null;
Form frmContainer = null;

private void btnExclude_Click(object sender, EventArgs e)
{
    if (ucExecludeSpecialist != null)
    {
        if (frmContainer != null)
        {
            frmContainer.Controls.Remove(ucExecludeSpecialist);
            ucExecludeSpecialist = null;
        }
    }

    if (frmContainer != null)
    {
        this.Controls.Remove(frmContainer);
        frmContainer = null;
    }

    frmContainer = new Form();
    frmContainer.ControlBox = false;
    frmContainer.StartPosition = FormStartPosition.Manual;
    frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

    ucExecludeSpecialist = new BBA.Controls.ExecludeSpecialist();
    ucExecludeSpecialist.SaveClicked += 
        new BBA.Controls.ExecludeSpecialist.SaveComplete(OnSaveClicked);
    ucExecludeSpecialist.CloseClicked += 
        new BBA.Controls.ExecludeSpecialist.CloseComplete(OnCloseClicked);
    ucExecludeSpecialist.BringToFront();
    frmContainer.Height = ucExecludeSpecialist.Height;
    frmContainer.Width = ucExecludeSpecialist.Width;
    //frmContainer.Top = this.Height - frmContainer.Height / 2;
    //frmContainer.Left = this.Height - frmContainer.Height / 2;
    frmContainer.BringToFront();
    frmContainer.TopLevel = false;
    frmContainer.Controls.Add(ucExecludeSpecialist);
    this.Controls.Add(frmContainer);
}

please guide me how to show that form on top of all control of another form at center. thanks

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Thomas
  • 33,544
  • 126
  • 357
  • 626
  • check [this](http://stackoverflow.com/questions/1351322/c-sharp-winform-forms-in-form) out – Abdusalam Ben Haj Jan 22 '13 at 11:35
  • Try to add frmContainer.Show(); to your code – Kooki Jan 22 '13 at 11:37
  • yes i did form is showing but there is a data grid in my form so form is not being able to show on top of data grid. is there any fix there. i want to position my form at center of main form and my form will come on top of other control. i use bring to front but did not work. – Thomas Jan 22 '13 at 11:54

1 Answers1

1

If I understand your comment correct, your problem is that a DataGrid overlays your recently added form? Try :

After you have add

frmContainer.Show();

your Form shoul be visible. After that you should solve your problem, if you call ucExecludeSpecialist.BringToFront(); after calling frmContainer.Show();

Example :

    private void button1_Click(object sender, EventArgs e)
    {

        frmContainer = new Form();
        frmContainer.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

        frmContainer.Height = this.Height / 2;
        frmContainer.Width = this.Width / 2;
        frmContainer.BackColor = Color.Red;
        frmContainer.TopLevel = false;
        this.Controls.Add(frmContainer);
        frmContainer.Show();
        frmContainer.BringToFront();
    }
Kooki
  • 1,157
  • 9
  • 30