如何从另一个子表单更新一个子表单 [英] How do I update a child form from another child form

查看:68
本文介绍了如何从另一个子表单更新一个子表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示父表单(表1)和子表单(表2)的应用.在父级上,我单击一个按钮,该按钮调用第二个子窗体(窗体3)作为模式对话框.当我单击对话框窗体上的按钮时,它将使用对话框窗体中的文本更新父窗体上的tableLayoutPanel.我还需要它以相同的信息更新第一个子表单上的tableLayoutPanel.

I have an app that displays a parent (Form 1) and child form (Form 2). On the parent I click a button that calls a second child form (Form 3) as a modal dialog. When I click a button on the dialog form, it updates a tableLayoutPanel on the parent form with text from the dialog form . I need it to also update a tableLayoutPanel on the first child form with the same information.

对于父窗体和对话框窗体,我使用此处提供的解决方案. 从子级获取值到父级

For the parent and dialog form I use the solution provided here. get value from child from to parent form

推荐答案

首先,直接回答您的问题,您可以通过以下方式访问任何表格:

First, to answer your question directly, you can access any form with:

Form frmIWantThisForm = System.Windows.Forms.Application.OpenForms.OfType<Form1>().First();

在此示例中,Form1是所需表单的类名. OpenForms是您的应用程序拥有的表单的集合.此时,您可以访问frmIWantThisForm.somePropertyOfTheForm;

In this example, Form1 is the class name of the form you want. OpenForms is a collection of forms owned by your application. At this point you can access frmIWantThisForm.somePropertyOfTheForm;

例如(在上面的代码设置了我想要的形式之后),我想用一个列表框的背景色名称填充字符串(出于某种原因,我想我只是部分地使用了该列表框的背景色) ):

For example (after the code above which sets the form I want), I want to populate a string with the background color name of a listbox (for some reason I guess I'm just partial to the background color of that listbox):

    //the 'true' causes a search of children as well:
    Control theControl = frmIWantThisForm.Controls.Find("listBox1",true).First();

    string bgColor = ((ListBox)theControl).BackColor.Name;


您可以做的其他事情(仍然很简单,但不是很好的选择,因为随着表单数量的增加,复杂性也随之增加):


Other things you could do are (still simple, but not great options because complexity grows as increasing numbers of forms are talking to each other):

  1. 将表单分配给另一个表单的Tag属性,以便它可以访问调用表单(不是很清楚). tag属性将自身存储为一个对象,因此您需要再次使用强制类型转换(例如,在上面的示例中,当我将控件强制转换为ListBox时).
  2. 更改构造函数或要调用的对话框,以便可以采用其他形式传递.此选项还不错,因为至少可以很容易地看到数据的去向,但是随着您获得更多表格,复杂性仍然会增加.

复杂(但更好)的答案

但是,您遇到了反模式(吓人的编码,请参见Wiki定义)样式可能会在简单的应用程序中起作用,但是复杂度将成倍增加.每个表单都将引用其他所有表单,并且对代码的更新将变得越来越复杂.想象一下进行更新,然后突然您破坏了其他几段代码.

Complicated (but better) Answer

However, you're running into an anti-pattern (scary coding, see Wiki definition) style that will potentially work in a simple application, but the complexity will grow exponentially. Every form will be referencing every other form and updates to the code will get more and more complex. Imagine making an update and suddenly you break several other pieces of code.

相反,我建议您从视图/控制器代码中分离数据模型.启动应用程序时,将数据加载到控制器中.退出时,保存回来.也许最终您会更频繁地这样做.然后,当您调用模式对话框时,如果是用于模型的一部分,则传入该部分,然后让对话框根据该数据编辑模型.这样,无需更新整个代码中的所有控件,而是打开的下一个对话框只是打开并根据模型更新其视图".

Instead, I recommend you separate the data model from your view / controller code. When you start the application, load data into the controller. When you exit, save back. Perhaps eventually you'll do that more often. Then, when you call a modal dialog, if it's for a piece of the model, pass in that part and have the dialog edit the model based on that data. This way instead of updating controls all across your code, the next dialog you open simply opens and updates it's "view" based upon your model.

internal class MortgageAccounts
{
    internal List<Mortgage> Mortgages = new List<Mortgage>();

    internal decimal ComputeAverageAmount()
    {
        decimal amount = 0;
        //code to compute
        return amount;
    }

    internal void Load()
    {
        //Here you load your data from a save file, 
        //database, or some other method of deserializing.
    }

    internal void Save()
    {
        //Here you save your data (serialize in some way)
    }
}

internal class Mortgage
{
    internal int Id;
    internal decimal Amount;
}

您还可以执行其他一些工作来将代码分成概念部分,尽管这超出了问题的范围,但可以考虑研究此应用程序的MVC(模型视图控制器)教程. 代码项目"有一个教程可以获取您开始了.

There is additional work you can do to separate your code into conceptual segments, and while this goes far beyond the scope of the question, consider looking into MVC (Model View Controller) tutorials for this application. Code Project has a tutorial to get you started.

在本文中,我们对这些概念进行了以下描述:

From this article, we have the following description of these concepts:

  • 模型-这应该处理所有处理以下操作所需的数据(换句话说,业务逻辑)的操作: 从现在开始实施MVC模型的应用程序 将由MVC应用程序引用.这些操作可能意味着 读取数据,将数据写入数据库,从中获取信息 通过网络进行远程机器,耗时的操作等.模型 还应告知视图中发生的数据的任何更改 背景.
  • 视图-该组件负责将数据呈现给用户.关于本文的上下文,即WinForms, 视图类将与将显示的Form绑定在一​​起 给用户.
  • 控制器-这是MVC模式的中心和重要组成部分,因为它将模型和视图联系在一起.该模型 操纵数据和将数据呈现给 用户不知道彼此的存在或进行交互 直接彼此之间.正是控制器充当了 中介,并将它们联系在一起.例如,控制器将 来自用户的输入(例如按钮单击)并通知模型 采取适当的行动,如果应该采取的行动需要 启动以操纵项目数据.
  • The Model - This should take care of all the operations which deal with the data required (in other words the business logic) for the application, which is implementing the MVC model from now onwards will be referred to as by the MVC application. The operations can mean reading, writing data into the database, getting information from remote machines via network, time consuming operations etc. The model should also inform the view about any changes to the data happening in the background.
  • The View - This component takes care of presenting the data to the user. With respect to the context of this article, i.e., WinForms, the view class will be tied around with the Form which will be shown to the user.
  • The Controller - This is the center and important component of the MVC pattern as it ties the Model and View together. The Model which manipulates the data and the View which presents the data to the user does not know the existence of each other or they interact directly with each other. It is the controller which acts as an intermediary and ties them together. For example, the controller takes the input from the user such as a button click and informs the model to take appropriate action, if there should be an action that needs to be initiated to manipulate the project data.

有关紧密耦合还是松散耦合紧密耦合是指对象之间相互了解很多,而松散耦合是指对象之间不需要了解很多.前者很难维护和更新,而后者通常是首选.

Here's some additional reading on tight vs loose coupling. Tight coupling is when objects need to know a lot about each other, and loose coupling is when they do not need to know a lot. The former is hard to maintain and update while the latter is generally preferred.

这篇关于如何从另一个子表单更新一个子表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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