在设计时触发事件 [英] Firing Events at Design Time

查看:75
本文介绍了在设计时触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义控件,其中将包含一个TEXTBOX ......
在单击文本框时,我想在其下方添加一个新的文本框....

并且应该在设计时发生……

单击第二个,第三个和其他TEXTBOX'S CLICKS将创建的TEXTBOXES和其他控件,都应添加到与第一个TEXTBOX相同的容器控件中.......

我尝试使用控件的属性(例如"DesignTime"和接口"ISite")来执行此操作,但是它对于Click事件无法正常工作,但是对于控件的调整大小"事件却可以正常工作

以下是我尝试过的代码...

I want to create a custom control which will contain a TEXTBOX......
on the click of the TEXTBOX I want to add a new TEXTBOX right below it.......

AND THIS SHOULD HAPPEN AT DESIGN TIME......

and both the TEXTBOXES and others which will be created on the click of the second, third and other TEXTBOX''S CLICKS should all be added to the same container control on which the first TEXTBOX was .......

I tried doing this using the properties of the control like "DesignTime" and the interface "ISite" but it is not working properly for the Click event but instead its working fine for "Resize" event of the control

Following is the code that i have tried...

public partial class UserControl1 : UserControl
    {
        private MyControl_B myctrl_B;
        public UserControl1()
        {
            InitializeComponent();
            this.myctrl_B = new MyControl_B();
            this.Controls.Add(myctrl_B);
        }
    }

    public class MyControl_B : UserControl
    {
        protected bool IsInDesignMode()
        {
            bool ret = false;
            Control ctrl=this;
            while (ctrl!= null && ret == false)
            {
                ISite site = ctrl.Site;
                if (site != null)
                {
                    ret = site.DesignMode;
                }
                ctrl = ctrl.Parent;
            }
            return ret;
        }

        protected override void OnClick(EventArgs e)
        {
            if (this.IsInDesignMode())
            {
                MessageBox.Show("Test");
            }
            else
            {
                MessageBox.Show("NOTest");
                //base.OnClick(e);
            }
        }
    }

推荐答案

在设计模式下,某些事件在控件看到它们之前被IDE捕获.单击和击键都以这种方式处理.据我所知,您的要求是不可能的.
Some events are captured by the IDE before your control gets to see them, when in design mode. Click and keystrokes are treated in this way. As far as I know, what you''re asking is not possible.


您认为事件访问器是如何设计的?

您需要通过自定义控件的新创建事件来公开子事件.假设我们希望控件的用户能够向其子项TextBox的事件ClickKeyPress添加事件句柄或从中删除事件句柄.看起来像这样:

How do you think event accessors are designed for?

You need to expose the children events through the newly created events of your custom Control. Let''s assume we want the user of your control to be able to add or remove an event handle to/from the events Click and KeyPress of its child TextBox. It would look like this:

public class MyCustomControl : Control {

    public event System.EventHandler TextBoxClick {
        add { myTextBox.Click += value; }
        remove { myTextBox.Click -= value; }
    }

    //using generic EventHandler with derived EventArgs is a bit more complex:
    public event System.EventHandler<KeyPressEventArgs> TextBoxKeyPress {
        add { myTextBox.KeyPress += new KeyPressEventHandler(value); }
        remove { myTextBox.KeyPress -= new KeyPressEventHandler(value); }
    }

    //...

    TextBox myTextBox = new TextBox();

} //MyCustomControl



子控件的所有事件仍然不可见,但是可以通过自定义父控件来设置此事件.公开您想要为此自定义控件的其他子项以及其他子项.

您可以在这里阅读有关事件访问器的信息,例如: http://msdn.microsoft.com/en-us /magazine/cc163533.aspx [ ^ ].

—SA



The child control remains invisible with all its events, but this event becomes settable through your custom parent control. Expose as many other events as you want for this and other children of your custom control.

You can read about event accessors here, for example: http://msdn.microsoft.com/en-us/magazine/cc163533.aspx[^].

—SA


这篇关于在设计时触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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