简体   繁体   English

C#父窗体未从子目录更新

[英]C# Parent form not updating from child

I have two Forms Parent Form : properties : Total Value. 我有两个表格父表格:属性:总值。 Button to load child form. 加载子表单的按钮。 Child Form: property : insert Value. 子窗体: property:插入值。

When I run application, Parent form loaded first. 当我运行应用程序时,首先加载父窗体。 I can load second (Child) form by click button. 我可以通过单击按钮加载第二个(子)表格。 Problem is that when I insert value in child form parent form is not showing any changes even I pass value by delegate and other methods. 问题是,当我在子窗体中插入值时,即使我通过委托和其他方法传递值,父窗体也未显示任何更改。

I don't want to reload Parent Window. 我不想重新加载父窗口。 Please help 请帮忙

What you should be doing is creating events on the child for (or using the existing events if they will do the job) and having the parent form subscribe to those events. 您应该做的是在子级上创建事件(或使用现有事件,如果它们可以完成任务),然后让父级表单订阅这些事件。

One common example is to have the parent hide itself, show the child, and then show itself again when the child is closed. 一个常见的示例是让父项隐藏自己,显示子项,然后在关闭子项时再次显示自身。 Here is some code that does that: 这是执行此操作的一些代码:

//in parent form
private void someButtonClickHander(object sender, EventArgs args)
{
  ChildForm child = new ChildForm();
  this.Hide();

  child.Closing += (sender2, args2) => 
  {
    var someResultFromChildForm = child.SomePropertyOnChildForm;
    this.Show();
  }

  child.Show();
}

If the closing event doesn't work for you (maybe you want to do something when the child form presses a button) you may need to have the child create it's own event. 如果关闭事件对您不起作用(也许您希望在子窗体按下按钮时执行某些操作),则可能需要让孩子创建它自己的事件。 There are lots of tutorials on MSDN or other sites on how to do this. MSDN或其他站点上有很多有关如何执行此操作的教程。 If you have trouble with that (or any other aspect of this design) please ask for clarifications in comments. 如果对此(或该设计的任何其他方面)有疑问,请在评论中进行说明。

If you want to keep data consistent between multiple forms, I recommend putting the data into an object that you can reach from both forms. 如果要使多个表单之间的数据保持一致,建议将数据放入可以从两种表单访问的对象中。

The most direct way is to implement INotifyPropertyChanged for the object, so you can bind to this object in both forms and any changes made will trigger the property change event. 最直接的方法是为该对象实现INotifyPropertyChanged ,因此您可以两种形式都绑定到该对象,并且所做的任何更改都会触发属性更改事件。

For more complex scenarios I make an object that has custom events that I can subscribe to, as INotifyPropertyChanged can be lack the subtlety needed in complex scenarios. 对于更复杂的场景,我将创建一个具有可以订阅的自定义事件的对象,因为INotifyPropertyChanged可能缺少复杂场景中所需的微妙之处。 As an example, I have a "Helm" object for my primary navigation form that all of the UI elements subscribe to. 举例来说,我的主导航表单有一个“ Helm”对象,所有UI元素都订阅了该对象。 If a navigation event occurs, the helm does all of the navigation and data loading, and then it triggers a series of events that the UI listens to. 如果发生导航事件,则舵将完成所有导航和数据加载,然后触发UI侦听的一系列事件。

For a simple parent child this can seem like overkill, but this model (having one truth for the current state that the UI simply subscribes to) allows your UI to evolve and for each element of the UI to only worry about its own needs. 对于一个简单的亲子孩子来说,这似乎有些矫kill过正,但是这种模型(对于UI只是订阅的当前状态具有一个事实)允许您的UI不断发展,并且UI的每个元素仅担心自己的需求。

Have a Property called Parent in your child form. 在您的孩子形式中有一个称为“ 父母 ”的财产。 When you create the instance of the Child,Pass the current object( parent form object) to the constructor where you will set it as the Parent property value of child. 创建Child的实例时,将当前对象(父窗体对象)传递给构造函数,在该构造函数中将其设置为child的Parent属性值。 In Parent form Have a Public Method in your Parent form. 在父窗体中,在您的父窗体中有一个公共方法。 P P

parent form 家长表格

Form1 : Form
{    
    decimal _totalPrice;

    public void UpdateTotal(decimal val)
    {
      _totalPrice=_totalPrice+val;
      lblTotal.Text=_totalPrice.ToString();
    }    
}

Child Form 子表格

Form2:Form
{
  public Form1 Parent { set;get;}
  public Form2(Parent parent) 
  { 
    this.Parent =parent;
  }    
}

When creating the object of Child form 创建子窗体的对象时

Form2 objChjild=new Child(this);
//do whatever

Now from child form, if you want to update the total, 现在从子窗体中,如果要更新总数,

call like this 这样叫

this.Parent.UpdateTotal(200);

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

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