English 中文(简体)
c# 双形 - 如何从儿童形式获得父母表格
原标题:c# winforms - how to access ParentForm from ChildForm

我读过关于这个的马努文章,但... 仍然困惑。

从表单1 I 打开表单2:

new Form2().Show();

在表格2. 关闭时,我需要:

Form1.TextBox1.Visible = false;

为了达到这个目的,我应该把什么代码和确切位置放在哪里?

最佳回答

问题是, 您的代码结构方式, 您的 < code> Form2 类实例对您的 < code > Form1 类实例一无所知。 因此, 它无法访问其它对象的属性或调用方法 。 请记住, < code > Form1 和 < code> Form2 是 < targe_ em > class 类的名称, 而不是 < em> objects

黑客解决方案是将一个公共字段添加到您的 Form2 类中, 该类别包含您的 Form1 对象的引用。 在创建您 Form2 类实例后, 您将填入该字段中, 但在您调用 show 方法之前 。

您会遇到的下一个问题是,默认情况下,对窗体的控制是 私人 ,这意味着只有定义窗体的类别内的代码才能访问它们。它们无法从 design 类内的代码中访问或操作。

您的设计从根本上从目标导向的角度被打破了。 一种等级不应该是操纵或接触另一等级的私人成员。

如果有的话, 您应该在 Form1 中处理所有这一切。 修改 Form2 类, 以便在事件关闭时提醒事件, 然后从 Form1 订阅该事件。 在 Form1 事件处理器方法中, 隐藏文本框 。

快速和肮脏的解决办法是切换到 ShowDialog 方法,该方法显示另一种形式和区块执行,直到该形式关闭。然后您可以写入 :

// Create an instance of your Form2 class and show it as a modal dialog
using (var f = new Form2())
{
    f.ShowDialog(this);
}

// When the ShowDialog method returns, the Form2 form has closed, so
// you can go ahead and change the visible state of your control on Form1.
this.TextBox1.Visible = false;

然而,模式的缺点是,用户无法与 Form1 互动,而 Form2 却被打开。从你的问题中不清楚这是否可行。如果没有,我建议前一个解决方案,即提出一个事件。无论哪种方式,我强烈建议您在 C# 中拿一本关于目标导向编程的书。如果错误的话,这样的设计很难在以后修正 。

问题回答

您可以使用 ParentForum 访问您的父表格。

在这种情况下, ((( Form1) ParentForm) 。 TextBox1. 可视=假; ,假设 TextBox1 从外部可见。

表格2. 封闭日

Form1 parentForm = Application.OpenForms["FormName"] as Form1;
if (parentForm != null)
 {
    parentForm.TextBox1.Visible = false;
 }

确保在表单1 文本框1 上是公开的





相关问题
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.