Zeos6
Zeos6

Reputation: 273

C# Windows Form: Mdi Parent and Child form issues

I have a parent form that is set as an Mdi container. I load a child form called Plot from a menu bar click in the parent form. The code is:

protected void menuPlot_Click(object sender, EventArgs e)
{
    // ... load form with Plot settings in center of parent form

    // ... create a new instance of the Plot settings child form
    PlotSettings plotSettings = new PlotSettings();

    // ... set Welcome as the parent form for the Plot settings child window
    plotSettings.MdiParent = this;

    // ... display and position Plot settings child form
    plotSettings.StartPosition = FormStartPosition.CenterScreen;  // center child form 
    plotSettings.Show();  //  display child form
}

This works well except I have the following questions:

  1. Is there any way I can force the child form to remain at the center. At the present time I am able to drag it around inside the container. I would like to prevent the user from moving it around. The only way I can think of doin git at this time is too make the child form borderless but I am not sure if this will work.

  2. Is there any way I can make the child form modal? Yes, I know that I could make the child form modal but then it would no longer be contained inside the parent form which is what I want. Is there perhaps a way to disable the parent controls while the child form is active? Currently I can open multiple instances of the child form but I want to have only one instance at any time.

  3. I have some labels on the parent form and the labels always sit on top of the child form. Is there any way to force the child form to be topmost? I have use TopMost and this does not seem to work.

Thanks for any help you may be able to provide.

Upvotes: 0

Views: 5205

Answers (2)

James
James

Reputation: 2911

  1. Use the child forms "LocationChanged" event and put in code to centre the form.

    this.Left = ((this.ParentForm.ClientRectangle.Width - this.Width) / 2);
    this.Top = ((this.ParentForm.ClientRectangle.Height - this.Height) / 2);
    
  2. To have only one instance of the form check for it's existance using:

    if (!this.MdiChildren.Any<Form>(item => item is Form1))
    {
    
    }
    
  3. You could redraw the graphics manually on the MDI form window but otherwise I would not put any controls there. (You would need to override OnPaint and OnPaintBackgound.)

Upvotes: 1

competent_tech
competent_tech

Reputation: 44961

What about setting ControlBox, MinimizeBox, and MaximizeBox to False and setting WindowState to Maximized?

Then, you could have a Panel or GroupBox or other visual element that is centered within the maximized child form such that it would always stay in the center of the screen and the user could not resize that element or otherwise move it.

As for only opening a single instance, this is just busy work: when your form is opened, register the open instance in a static class; when it is closed unregister it. Prior to opening the form, check to see if an instance is registered in the static class; if so, set the focus on it, if not open a new instance.

Upvotes: 0

Related Questions