简体   繁体   English

C#Windows窗体:Mdi父级和子级窗体问题

[英]C# Windows Form: Mdi Parent and Child form issues

I have a parent form that is set as an Mdi container. 我有一个设为Mdi容器的父表单。 I load a child form called Plot from a menu bar click in the parent form. 我从父窗体的菜单栏中单击加载称为Plot的子窗体。 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. 我现在只能想到doin git的唯一方法也是使子窗体变为无边界,但是我不确定这是否可行。

  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. 我使用了TopMost,这似乎不起作用。

Thanks for any help you may be able to provide. 感谢您提供的任何帮助。

  1. Use the child forms "LocationChanged" event and put in code to centre the form. 使用子窗体“ LocationChanged”事件并放入代码以使窗体居中。

     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. 您可以在MDI表单窗口上手动重画图形,但否则我不会在其中放置任何控件。 (You would need to override OnPaint and OnPaintBackgound.) (您将需要重写OnPaint和OnPaintBackgound。)

What about setting ControlBox , MinimizeBox , and MaximizeBox to False and setting WindowState to Maximized ? ControlBoxMinimizeBoxMaximizeBox设置为False并将WindowState设置为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. 然后,您可能具有一个Panel或GroupBox或其他可视元素,该元素位于最大化子窗体的中央,这样它将始终停留在屏幕的中心,并且用户无法调整该元素的大小或以其他方式移动它。

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. 如果是这样,请把重点放在它上,如果不打开一个新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM