在子表单工作时启用父表单 [英] Parent form enable while child form working

查看:83
本文介绍了在子表单工作时启用父表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI全部



i在C#中创建一个应用程序,其中来自父表单我按下按钮并打开子表单我希望父表单在子表单工作时保持活动状态。

注意:

父母表格从儿童表格符号表格填写一个文本框。



请帮助



我尝试了什么:



i在处理按钮中尝试孩子

制作父母表格的对象



Parentform obj = new parentform();

obj.Activate ();



但它不起作用

HI All

i make an application in C# in which from parent form i press a button and child form open i want parent form remain active while child form is working .
Note:
parent form one text box fill from the child form ruing form.

Please Help

What I have tried:

i tried in the processing button of child
make object of parent form
like
Parentform obj= new parentform();
obj.Activate();

but its not working

推荐答案

这并不像你想象的那么简单:它是还不错,但是你必须改变它一切正常。

问题几乎肯定是你这样做:

That's not as simple as you may think: It's not too bad, but you have to change teh way it all works.
The problem almost certainly is that you do this:
ChildForm cf = new ChildForm();
cf.ShowDialog();

这会创建一个所谓的模态表单 - 在子表单关闭之前,对ShowDialog的调用不会返回 - 这意味着您的父表单可以'在用户完成孩子之前做任何事情。



您可以使用Show方法,但这可能会产生问题 - 例如,如果用户再次按下相同的按钮?

要解决这些问题,请创建一个类级私有变量来保存chicld表单实例:

This creates what is called a Modal Form - the call to ShowDialog does not return until the child form has been closed - and that means that your parent form can't do anything until the user has finished with the child.

You can use the Show method instead, but that can create it's own problems - for example, what happens if the user presses the same button again?
To get round these problems, create a class level private variable to hold the chicld form instance:

private ChildForm child = null;

然后在你的按钮处理程序中检查它:

Then in your button handler check it:

if (child == null)
   {
   child = new ChildForm();
   ...
   }

现在您的用户可以按他喜欢的次数按下它,没有任何问题。但我们需要管理用户再次按下它的方式!为此,我们需要知道用户何时关闭表单:

Now your user can press it as many times as he likes, without a problem. But we need to manage how the user can press it again! To do that, we need to know when the user closes the form:

if (child == null)
   {
   child = new ChildForm();
   child.FormClosed += child_FormClosed;
   child.Show();
   }



并且处理程序终止实例:


And the handler kills the instance:

void child_FormClosed(object sender, FormClosedEventArgs e)
    {
    child.FormClosed -= child_FormClosed;
    child = null;
    }

现在,当孩子可见时,父表单仍然可以工作。

Now, the parent form can still work while the child is visible.


一种略有不同的方法。



1.将你称之为'childForm的主表格的所有形式。假设您已在项目中添加了名为'OwnedFormExample的第二个表单:
A slightly different approach.

1. make what you call the 'childForm an owned Form of the Main Form. Assume you've added a second Form to your Project named 'OwnedFormExample:
OwnedFormExample OwnedForm = new OwnedFormExample();

private void MainForm_Load(object sender, EventArgs e)
{
    OwnedForm.ShowInTaskbar = false;
    OwnedForm.Owner = this;
    OwnedForm.FormClosing += OwnedFormOnFormClosing;
}

拥有的表单将始终与其所有者表单一起显示:它将始终显示在所有者表单上方;如果你最小化所有者Form,那么拥有的Form将被最小化。



假设MainForm上的一个名为'btnShowOwnedFor

An owned Form will always appear along with its owner Form: it will always appear above the owner Form; if you minimize the owner Form, the owned Form will be minimized.

Assume a Button on the MainForm named 'btnShowOwnedFor

private void btnShowOwnedForm_Click(object sender, EventArgs e)
{
    OwnedForm.Show();
    btnShowOwnedForm.Enabled = false;
}

点击显示拥有的表格,并禁用按钮:您确保用户不会犯多次点击的错误(防御性编程)。



假设我想重新使用第二个Form,而不是删除它并在每次我想使用它时重新创建它?

The click shows the owned Form, and disables the Button: you insure the user can't make the mistake of multiple clicks (defensive programming).

Suppose I want to re-use that second Form, rather than delete it and re-create it every time I want to use it ?

        private void OwnedFormOnFormClosing(object sender, FormClosingEventArgs formClosingEventArgs)
{
    OwnedForm.Hide();

    if (formClosingEventArgs.CloseReason == CloseReason.UserClosing)
    {
        btnShowOwnedForm.Enabled = true;

        formClosingEventArgs.Cancel = true;
    }
}

此处所拥有的Form的FormClosing EventHandler隐藏了拥有的Form,并且用户关闭了拥有的Form,启用按钮后取消Close。



以下是拥有的Form示例:

Here the FormClosing EventHandler for the owned Form hides the owned Form, and, the owned Form is being closed by the user, cancels the Close after enabling the button.

Here's what the owned Form example could look like:

public partial class OwnedFormExample : Form
{
    public OwnedFormExample()
    {
        InitializeComponent();
    }

    public string OwnedFormTextBoxData 
    {
        set { textBox1.Text = value; }
        get { return textBox1.Text; }
    }
}

公共属性为拥有的Form 实例的外部创建者提供了一种访问TextBox内容的方法...而不直接暴露TextBox控件(封装)。



在所有者Form的代码中,您可以在拥有的Form的TextBox中访问或设置Text内容。



请注意,虽然您可以将表格作为另一个表格的父母,但这是一个永远的,非常重要的错误。

A public Property gives an external creator of the owned Form instance a way to access the TextBox contents ... without directly exposing the TextBox Control (encapsulation).

At any time in the owner Form's code, you can access or set the Text content in the owned Form's TextBox.

Note that while you can make a Form be the Parent of another Form, this is something that is always, imho, a serious mistake.


这篇关于在子表单工作时启用父表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆