English 中文(简体)
设置表单的父级
原标题:Set the Parent of a Form
  • 时间:2010-02-07 18:17:33
  •  标签:
  • c#
  • winforms

I have a Windows form from which I would like to open a status form that says "Saving..." and then disapears when the saving is complete. I would like to center this small status form in the middle of the calling form. I ve tried setting the "StartPosition" propery to "CenterParent", but it doest work. I create the status form from the other form like so:

SavingForm saving = new SavingForm();
savingForm.Show();
Thread.Sleep(500); //Someone said this is bad practice ... why?
savingForm.Close();

Wouldn t the calling form be the "Parent"? When I set a watch for saving it says it has no parent.

我试过。

SavingForm saving = new SavingForm();
saving.Parent = this;
savingForm.Show();
Thread.Sleep(500);
savingForm.Close();

并且放弃了一种例外情况,即不能将“Top级控制”列入“控制”。

我如何将此状态窗口居中在调用窗口中?

提前感謝。

最佳回答

我会做类似这样的事情:

SavingForm saving = new SavingForm();
savingForm.ShowDialog(this);

在SavingForm中,我将在加载处理程序中启动一个计时器,运行500毫秒,然后在完成后关闭表格。这样更清洁。ShowDialog还将锁定您的用户界面,仅显示保存形式,不允许用户搞什么。

问题回答

请使用此项:

saving.Show(this);

当您显示表单时设置所有者。

编辑:ShowDialog()方法还有一个重载,如果您决定走这条路线,它会让您指定所有者:

saving.ShowDialog(this);

如果你把父级 (this) 传递给所有者,就像

SavingForm saving = new SavingForm() { Owner = this };

然后,您可以访问所有者在子表单中的属性和方法(在此示例中为SavingForm),前提是您需要访问每个属性时Owner的属性修改器设置为内部公共(您可以直接在源代码中编辑修改器,或通过表单的设计器属性进行编辑 - 每个控件都有一个Modifier属性)。

你可以在这里找到Owner、Parent和ParentForm之间的差异的详细解释。

注意:在我的情况下,像saving.Show(this);saving.ShowDialog(this);这样传递它并没有帮助。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签