从子窗口刷新父窗口形式 [英] Refresh parent windows form from child windows

查看:79
本文介绍了从子窗口刷新父窗口形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在关闭子窗口窗体后尝试刷新父窗口窗体时遇到麻烦.这是我的代码:

I am having trouble trying to have parent windows form refreshed after closing the child windows form. Here's the code I have:

private void btnSave_Click(object sender, EventArgs e)
    {
        BusinessClient bc = new BusinessClient();
        bc.CompanyName = txtCompanyName.Text;
        bc.PointOfContact = txtPointOfContact.Text;
        bc.Address1 = txtAddressOne.Text;
        bc.Address2 = txtAddressTwo.Text;
        bc.City = txtCity.Text;
        bc.State = cbxState.Text;
        bc.Zip = txtZip.Text;
        bc.Phone = txtPhone.Text;
        bc.Email = txtEmail.Text;

        BusinessClientMgr bcMgr = new BusinessClientMgr();
        bcMgr.StoreNewBusinessClient(bc);

        AfterTheSave();

        AssignmentForm assignForm = new AssignmentForm();
        assignForm.Refresh();

        this.Close();
    }

我在这里要做的是保存数据并关闭子窗口窗体,并通过检索要显示的新数据来刷新父窗口窗体.我在这里想念什么吗?虽然我知道子窗口窗体不应该控制父窗口.想一想,一个孩子正在要求父母更新信息.

What I'm trying to do here is to save the data and close the child windows form and have parent windows form refreshed by retrieving the new data to display. Am I missing something here? While I understand that the child windows form shouldn't control the parent. Come to think of it, a child is asking the parent to update the information.

推荐答案

Aniruddha Varma的答案是正确的.

Answer by Aniruddha Varma is correct.

您有2种形式:父母和子女.

You have 2 forms: Parent and Child.

在父项中,我们将在需要的地方显示子表单:

Into the parent, we are going to show the Child Form with this where you need:

Form2 child = new Form2();
child.Show(this); //We pass through the Parent instance to Child

而且,我们将声明一个Public Method来编辑您的表单控件,如下所示:

And, also, we are going to declare a Public Method to edit whatever are your form controls like this:

public void SetText(string text)
{
    parentTextbox.Text = text;
}

此后,我们进入子窗体.在此,我们将在Form事件中声明"FormClosing"或在一个按钮中声明以下代码以关闭表单:

After that, we pass into the Child Form. In this, we going to declare over the Form event "FormClosing" or into a button to close the form the following code:

 Form1 parent = (Form1) Owner;
 parent.SetText(childTextbox.Text);

有了这个,我们将把Parent实例带回到Child中,然后回调方法SetTex,并在其中传递文本参数.回顾第一个表单,我们将使用孩子的表单"中的文本更新父母的"文本框.

With this, we are going to take back the Parent instance into the Child, and then callback the Method SetTex where we pass through the parameter our text. Looking back at the first form, we will have the Parent's textbox updated with text from Child's Form.

这篇关于从子窗口刷新父窗口形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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