在子窗体中使用“父级"按钮 [英] Use the Parent button in Child form

查看:106
本文介绍了在子窗体中使用“父级"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉有一个愚蠢的问题,我是C#中的新手..
我的问题是我是否在父表单中有一个按钮,如何在子表单中使用它(该按钮).
我试图在子窗体的属性中找到事件处理程序,但是我看不到任何用于处理按钮事件的属性..

例如:
在我的情况下,父窗体具有添加新",删除"和保存"按钮,并且当子窗体处于活动状态时,由绑定到数据库表的网格控件包含. 添加新"按钮的功能是在表中添加新记录,删除"在表中删除记录,保存"在表中保存所有记录.如何在活动子窗体中访问添加新",删除"和保存"按钮?请至少一个按钮帮助我解决这个问题..

谢谢:)

Sorry for a silly question I''m kinda newbie in c#..
My question is if I have a button in parent form, How to use it(the button) in child form.
I tried to find the event handler in properties at child form but I don''t see any property to handle the button event..

ex:
In my case the parent form have "add new", "delete" and "save" button and when the child form is active is contained by a grid control which bind to database table. the "add new" button function is to add new record in the table, "delete" to delete the record in table and "save" to save all the record in table. How can I access the "add new", "delete" and "save" button in the active child form? Please help me to solve this at least for one button as a sample..

Thanks :)

推荐答案

由于您是新手,因此SA的解释很可能会困扰您.

换句话说,他想说的是你做不到.好吧,你可以,但这不是直截了当的,这是一个可怕的想法.窗体上的控件永远不能被其他窗体代码所触及.

在这种情况下,您通常要做的是处理父表单代码中按钮的Click事件.在该处理程序中,您将在子表单代码中调用一个方法以执行所需的任何操作.

Windows窗体应用程序中的控制流通常是一个层次结构,从上到下,或者从启动窗体到子窗体.类结构也是如此.

数据会传递给孩子,而不会传递给方法调用的父母.如果父母需要数据,则有两种方法可以执行此操作.孩子要么准备数据结构供父级自行选择,要么孩子可以以事件参数的形式将数据传递给父级,通知父级事件已准备好数据,就像单击按钮一样事件.

父级应始终可以选择是否提取数据或订阅事件.孩子们永远不要将数据推向父母的喉咙.
Since you''re a newbie, SA''s explanation was probably WAY over your head.

What he was trying to say, in other words, is you can''t. Well, you could, but it''s not straight forward and is a horrible idea. Controls on a form should never be touched by some other forms code.

What you would normally do in this situation is handle the Click event of the button in the Parent forms code. In that handler, you would call a method in the child form code to do whatever you want.

The flow of control in a Windows Forms app is normally a hierarchy, from the top down, or from the startup form down through child forms. The same goes for class structures.

Data is passed down to children, never up to parents in method calls. If the parent needs data there are two methods to do this. Either the child prepares a structure of data for the parent to pick up itself or the child can pass the data up to the parent in the form of an event argument, notifying the parent that data is ready through an event, just like a button click event.

The parent should always have the option of picking up data or not or subscribing to an event or not. The children should never shove data down the throat of a parent.


首先,我认为您的表格与孩子和父母无关.即使类System.Windows.Forms.Form具有属性System.Windows.Forms.Control.ControlsSystem.Windows.Forms.Control.Parent,表单之间的父子关系实际上并不是可操作的.

(更确切地说,如果您尝试将一个表单作为另一个表单的父表单,则会引发异常.您可以通过为子表单分配属性TopLevel = false并将另一个表单作为其父表单来避免该异常,但是结果丑陋:一个表单位于另一个表单的客户区域内作为控件,您永远都不要这样做,我不是说MDI父级和MDI子级之间的关系;它们实际上不是表单之间的父子关系.谁需要MDI.)
现在,介绍如何访问表单之间的按钮.立即提供对控件的访问是不好的,因为它会违反类封装.这是关于表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
First of all, I don''t think your forms are related as a child and a parent. Even though the class System.Windows.Forms.Form has the properties System.Windows.Forms.Control.Controls and System.Windows.Forms.Control.Parent, the parent-child relationship between forms is not really operational.

(More exactly, if you try to make one form a parent of another, an exception will be thrown. You can avoid it by assigning the property TopLevel = false for a child form and make another form its parent, but the result is ugly: one form sitting inside the client area of another form as a control. You should never do it. I do not mean the relationship between MDI parent and MDI children; they are not actually the parent-child relationships between forms. Besides, who need MDI.)
Now, about the access to the button between forms. It''s not good to provide the access immediately to the control, as it would violate class encapsulation. This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA


这篇关于在子窗体中使用“父级"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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