使用Custome事件创建Custome控件 [英] Creating Custome controls with Custome events

查看:550
本文介绍了使用Custome事件创建Custome控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
在所有博客中,我阅读了如何通过在用户的子控件的回发事件(如Button_Click事件等)中调用< EventName>(< senderObject>,< eventArguments>)来引发自定义事件.控制.但是我想引发此事件而不调用任何子控件的回发事件.
请指导我.只是我在想,如果我们在任何子控件的回发中调用自定义事件< EventName>(< senderObject>,< eventArguments>),则不使用它.取而代之的是,我们可以直接在Child控件回发事件中编写代码.那么自定义事件的用途是什么?
我读了一样的话,开发了一个用户控件

Hi Guys,
In all the blogs i read how to raise custom events, by calling <EventName>(<senderObject>,<eventArguments>) in a child control''s postback event like, Button_Click events and etc..., in user control. But I want raise this event without calling in any child control''s postback event.
Please guide me. Just I am thinking that, if we call our custom event <EventName>(<senderObject>,<eventArguments>) in any child control''s postback, no use of it. Instead of this, directly we can write our code in Child controls postback event. Then what is the use of custom events?
I read the sameples like, developed a user control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MySamples
{
    public delegate void customDeligate(object sender, CustomArgs args);
    public class MyClass
    {
        public event customDeligate customEvent;
        protected virtual void OnCustomEvent(CustomArgs e)
        {
            if (customEvent != null)
                customEvent(this, e);
        }
        protected void OnCustomEvent(object sender, CustomArgs e)
        {
            if (customEvent != null)
                customEvent(sender, e);
        }
    }
    public class CustomArgs : EventArgs
    {
        string _msg;

        public string Message
        {
            get { return _msg; }
            set { _msg = value; }
        }
        public CustomArgs(string message)
            : base()
        {
            this._msg = message;
        }
        public CustomArgs()
            : base()
        {
        }
    }
 public partial class Wall_Papers : System.Web.UI.UserControl
 {
    
        protected void Page_Load(object sender, EventArgs e)
        {
            MyClass objClass = new MyClass();
            objClass.customEvent += new customDeligate(objClass_customEvent);
            
        }

        static void objClass_customEvent(object sender, CustomArgs args)
        {
            Response.Write("Hi Welcome to Custome Events");
        }
    }
}



如何在不使用任何回发子控件(如按钮,下拉菜单等)的情况下引发事件,我想创建自己的控件并用作常规控件.

谢谢&问候
Chaitanya



How can I raise my event, without using any postback child controls like button,dropdown etc., I want create my own control which is working as Regular controls.

Thanks & Regards
Chaitanya

推荐答案

只需调用onCustomEvent方法...

另外,在C#中,您应该以大写字母(CustomEvent,CustomDelegate等)开头的方法,事件,公共属性和委托名称.
Just call the onCustomEvent method ...

Also, in C# you should start method, event, public property and delegate names with a capital letter (CustomEvent, CustomDelegate etc).


您可以调用事件:
You can invoke the event:
public class MyClass {

//...

    private void InvokeCustomEvent(string myMessage) {
        if (customEvent != null)
            customEvent.Invoke(this, new CustomArgs(myMessage));
    }
    private void InvokeCustomEvent() {
        if (customEvent != null)
            customEvent.Invoke(this, new CustomArgs());
    }

} //class MyClass



另外,将CustomArgs的这两个构造函数的访问修饰符更改为internal,因为在任何情况下都不能在程序集外部调用它们.这是因为,即使使用语法,也只能在声明类中调用任何事件,甚至不能在派生类中调用.

我什至演示了方法InvokeCustomEvent仅应在此类中使用,因此我展示了private访问修饰符.

请查看我最近的答案以获取更多详细信息:
由于我们有多播委托,所以为什么我们需要事件吗? [ ^ ]

类实例的用户只能访问事件对象,以使用它通过"+ ="添加一个或多个事件处理程序(或通过-="删除,这相对很少使用).偶数的调用始终从事件的调用列表中调用事件句柄的整个列表.

—SA



Also, change access modifiers of those two constructors of CustomArgs to internal, because there is no situation where you can call them outside of the assembly. This is because, even by syntax, you can only invoke any event in the declaring class, not even in derived classes.

I even demonstrated that the methods InvokeCustomEvent should be used only in this class, so I''ve shown the private access modifiers.

Please see my recent answer for more detail:
Since we have multicast delegates, why do we need events?[^]

The users of the class instance only have access to the event object to use it to add one or more event handlers via "+=" (or remove via "-=" which is relatively rarely used). The invocation of the even always calls the whole list of event handles from the invocation list of the event.

—SA


您应在此处修改代码
you shuold modify your code here
public class MyClass
    {
        //public event customDeligate customEvent;
        public event EventHandler<CustomArgs> customEvent;


    }


这篇关于使用Custome事件创建Custome控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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