简体   繁体   English

用户控件中的vb.net winform调用父窗体上的函数

[英]vb.net winform from a usercontrol call a function on the parent form

I don't know how to put it right, but I have a vb.net winform app in which I want to use a customcontrol so I can re-use the logic on multiple forms. 我不知道如何正确设置,但是我有一个vb.net winform应用程序,我想在其中使用customcontrol,以便可以在多种表单上重复使用逻辑。 I know how to set values in this control from the parent (using properties in the control). 我知道如何从父级(使用控件中的属性)在此控件中设置值。 But now I want to call a specifiek function on the parent form. 但是现在我想在父窗体上调用一个specifiek函数。 Im my case LoadData() which is a procedure of the parentform. 我的情况是LoadData()这是父窗体的过程。 How can I do this? 我怎样才能做到这一点?

I know I can reference the parent form by using Me.ParentForm in the usercontrol. 我知道我可以在用户控件中使用Me.ParentForm来引用父窗体。 But I cannot call the LoadData() procedure in the parentform. 但是我不能在父窗体中调用LoadData()过程。

Any help? 有什么帮助吗? This is a winforms app, not a ASP.NET app. 这是一个winforms应用程序,而不是ASP.NET应用程序。

TIA TIA

[Edit] I could solve my problem using this example found right here. [编辑]我可以使用这里找到的示例来解决我的问题。 This is working fine 这很好

First, a UserControl is for reusing GUI logic, so I hope that is what you meant. 首先, UserControl用于重用GUI逻辑,所以我希望这就是您的意思。 If you are trying to reuse non-GUI logic you might want to create some stand-alone classes for that. 如果尝试重用非GUI逻辑,则可能需要为此创建一些独立的类。

Second, it is generally bad design to call back up to the parent form in the way you describe because it makes the UserControl less reusable and it creates an overly tight binding between the two. 其次,以您描述的方式回调到父窗体通常是不好的设计,因为它使UserControl可重用性降低,并且在两者之间创建了过于紧密的绑定。 You should, if at all possible, push the data down into the UserControl instead. 您应尽可能将数据下推到UserControl

If you can find no way for the Form to push the data down (perhaps because it is based upon a UI interaction within the UserControl ), you have a couple of other options. 如果您找不到让Form向下推送数据的方法(也许是因为它基于UserControl的UI交互),则您还有其他几个选择。 The first is to wrap up the data-loading behavior into an object that can be passed to the UserControl during initialization, and the the UserControl can access the LoadData method on it as needed. 首先是将数据加载行为包装到一个对象中,该对象可以在初始化期间传递给UserControl ,并且UserControl可以根据需要访问其上的LoadData方法。 Another approach is to have the UserControl define a set of Events that can be used to request external data from the form. 另一种方法是让UserControl定义一组事件,这些事件可用于从表单中请求外部数据。 I like to use the "Query" prefix on these kinds of events. 我喜欢在此类事件上使用“查询”前缀。 So when a fetch button is tapped on your UserControl , it raises the "QueryData" event, and the form that is handling that event responds by populating a data container of some sort that is part of the custom EventArgs . 因此,当在UserControl上点击获取按钮时,它会引发“ QueryData”事件,并且处理该事件的表单将通过填充属于自定义EventArgs某种数据容器来响应。

Either of these can be expanded upon if you need more assistance. 如果您需要更多帮助,可以扩展这两种方法。


Upon re-reading the question, I it looks like perhaps my approach is over kill. 重新阅读问题后,我看来我的方法已被淘汰。 I was under the impression that the LoadData was a method in the Form that loaded data into the UserControl. 我的印象是LoadData是Form中的一种将数据加载到UserControl中的方法。 If it is just a simple call then focus on the event portion of my answer and ignore the data container portion. 如果这只是一个简单的调用,则将注意力集中在答案的事件部分,而忽略数据容器部分。

I would add an event to your control and handle it on the form the control is part of. 我会向您的控件添加一个事件,并以该控件所属的形式对其进行处理。

In your control, put something like this: 在您的控制中,输入以下内容:

Public Event LoadData(ByVal sender As Object, ByVal e As System.EventArgs)

And in your form, you can have something like this: 在您的表单中,您可以具有以下内容:

Private Sub UserControl1_LoadData(sender As Object, e As EventArgs) Handles UserControl1.LoadData
    '...Your code here
End Sub

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

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