从用户Control调用表单方法 [英] Call form method from user Control

查看:101
本文介绍了从用户Control调用表单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我有用户控件,它在我点击按钮后在

Hi. I have User Control and it shows in the main form in

的主窗体中显示。单击控件中的按钮后,如何隐藏此控件。或者如何从控件中调用主窗体的方法?有什么建议吗?
after I click on the button. How I can hide this control after I click on the button that is in the control. OR how can I call main form''s method from control? Any suggestions?

推荐答案

尝试从用户控件调用表单中的任何方法都不是一个好主意,它会锁定控件特定的形式,意味着以后不能再使用,如果不考虑对控制的影响,就不能改变形式。



而是在控件中创建一个表单处理的事件。然后,表单执行必要的操作,并通过公共方法或属性将信息反馈给Control。在这种情况下,创建表单处理的HideRequest事件:

It''s not a good idea to try and call any methods in the form from the user control, it locks the control to a particular form, and means that it can''t be re-used later, and the form can''t be changed without considering the effects on the control.

Instead, create an event in the Control which the Form handles. The form then does what is necessary and feeds information back to the Control either via a public method or a property. In this case, create a HideRequest event that the form handles:
/// <summary>
/// Event to indicate Control wishes to be hidden
/// </summary>
public event EventHandler HideRequest;
/// <summary>
/// Called to signal to subscribers that Control wishes to be hidden
/// </summary>
/// <param name="e"></param>
protected virtual void OnHideRequest(EventArgs e)
    {
    EventHandler eh = HideRequest;
    if (eh != null)
        {
        eh(this, e);
        }
    }
/// <summary>
/// Event to indicate Control wishes to be un-Hidden
/// </summary>
public event EventHandler ShowRequest;
/// <summary>
/// Called to signal to subscribers that Control wishes to be un-Hidden
/// </summary>
/// <param name="e"></param>
protected virtual void OnShowRequest(EventArgs e)
    {
    EventHandler eh = ShowRequest;
    if (eh != null)
        {
        eh(this, e);
        }
    }

你需要在Control中做的就是在适当的时候调用OnHideRequest,表单可以完成其余的工作(或者不是,如果它不是''''对该功能感兴趣)



代码块类型从xml更改为c# - 粘贴错误识别代码 - OriginalGriff [/ edit]

All you need to do in the Control is call the OnHideRequest at the appropriate time, and the form can do the rest (or not, if it isn''t interested in that feature)

[edit]Code block type changed from "xml" to "c#" - paste incorrectly identified the code - OriginalGriff[/edit]


在这方面,用户控件类与任何其他类没有区别,无论是否控制。你总是需要将一些东西传递给控件的实例,这可以被调用。它可能是:1)委托实例,2)使用方法对类进行引用,在本例中为表单实例,但这会强烈滥用封装,3)接口引用,在这种情况下,你应该通过表单类实现一些接口,更好的方法。



但我宁愿建议你反转控制。在用户控件类中定义并实现某个事件,使用户控件的用户可以访问该接口实例(内部 public )。这样,表单类将使用该控件。当实例化控件的实例时,表单类的代码应该将事件处理程序添加到该用户控件事件的调用列表中。在C#中,这是事件实例上的''+ =''运算符。当用户控件的实例调用该事件时,将调用所有事件处理程序。因为一个偶数处理程序是你的表单的方法,它将被调用,它可以调用任何其他方法。



这样,用户控件实例对所调用的方法保持不可知。他们可以是私人的。表单类仍然与调用的细节无关。您需要知道的是事件的使用: http://msdn.microsoft.com/en-us /library/awbftdfh.aspx [ ^ ]。



请阅读控制反转的想法:http://en.wikipedia.org/wiki/Inversion_of_control [ ^ ]。



-SA
In this respect, user control class is no different from any other class, no matter control or not. You always need to pass something to the instance of the control, something which can be called. It could be: 1) delegate instance, 2) a reference to the class with the method(s), in this case, a form instance, but this will strongly abuse encapsulation, 3) an interface reference, in this case, you should implement some interface by your form class, much better approach.

But I would rather advise you to invert control. Define and implement some event in your user control class, make the interface instance accessible to the user of the user control (internal or public). This way, the form class will be using the control. When the instance of the control is instantiated, the code of the form class should add an event handler to the invocation list of that user control event. In C#, this is the ''+='' operator on the event instance. When an instance of the user control invokes the event, all event handlers will be called. As one even handler is your form''s method, it will be called, and it can call any other method(s).

This way, the user control instance remains agnostic to the methods called. They can be private. And the form class remains agnostic to the detail of the call. All you need to know is the use of events: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[^].

And please read about the idea of inversion of control: http://en.wikipedia.org/wiki/Inversion_of_control[^].

—SA


这篇关于从用户Control调用表单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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