DesignMode,何时设置Control.Parent? [英] DesignMode, when is Control.Parent set?

查看:65
本文介绍了DesignMode,何时设置Control.Parent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在尝试使用ExtenderProviders并遇到问题.

ExtenderProvider允许您虚拟地将属性添加到现有类.我的项目使用Control作为基类.在DesignTime时,我需要对Form.上的控件的Control.Parent进行引用.
为了进行说明,假设您具有以下普通的ExtenderProvider类(除了说明之外,它没有做任何有意义的事情).该ExtenderProvider扩展Button类.我在窗体中添加了两个按钮,添加了ExtenderProvider(在先前的构建后会在工具箱上弹出).这些按钮具有TestString属性,如预期的那样.

问题是,一旦代码进入"EndInit",每个按钮的Control.Parent仍未设置. InitializeComponent建议应设置它.并且,在运行时设置了它,但没有设置设计时间.

这是设计使然吗?还是我做错了什么?

下面复制了ExtenderProvider和InitializeComponent的代码(来自我的Form):

Hi there

I am experimenting with ExtenderProviders and run into a problem.

ExtenderProvider allows you to virtualy add properties to existing classes. My project uses Control as the base class. At DesignTime, I need a reference to Control.Parent for controls on a Form.

To illustrate, suppose you had the following trivial class ExtenderProvider (it doesn''t do anything of interest except illustrate). This ExtenderProvider extends the Button class. I added two buttons to a Form, added the ExtenderProvider (which pops up on the toolbox after a previous build). The buttons have the TestString property as expected.

The issue is that, once the code goes into "EndInit", the Control.Parent of each button is still not set. InitializeComponent suggests that it should be set. And, at Run Time, it is set, but not Design Time.

Is this by design? Or am I doing something wrong?

The code forExtenderProvider, and for InitializeComponent (from my Form) is reproduced below:

using System.ComponentModel;
using System.Windows.Forms;
using System.Collections.Generic;

namespace ExtenderProviderDemo
{
    [ProvideProperty("TestString", typeof(Button))]
    public class ExtenderProvider:Component, IExtenderProvider, ISupportInitialize
    {
        Dictionary<control,> testStrings = new Dictionary<control,string>();

        public ExtenderProvider() { }

        public string GetTestString (Control extendee) {  
            if(testStrings.ContainsKey(extendee))
                return testStrings[extendee];
            else
                return string.Empty;
        
        }

        public void SetTestString(Control extendee, string testString) {
            if (testStrings.ContainsKey(extendee)) {
                testStrings[extendee] = testString;
            }

            else
                testStrings.Add(extendee, testString);
        }

        #region IExtenderProvider Members
        public bool CanExtend(object extendee)  {
            return extendee is Button;
        }
        #endregion

        #region ISupportInitialize Members

        public void BeginInit()  {
        }

        public void EndInit() {
            foreach (KeyValuePair<control,> pair in testStrings) {
                MessageBox.Show("Control " + pair.Key.Name + " : Parent null ? " + (pair.Key.Parent == null).ToString());
            } 
        }

        #endregion
    }
}

private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.extenderProvider1 = new ExtenderProviderDemo.ExtenderProvider();
            ((System.ComponentModel.ISupportInitialize)(this.extenderProvider1)).BeginInit();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(102, 80);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.extenderProvider1.SetTestString(this.button1, "");
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(115, 120);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.extenderProvider1.SetTestString(this.button2, "Test");
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.extenderProvider1)).EndInit();
            this.ResumeLayout(false);

        }

推荐答案

您说您是从Control继承的,但我看到了Component.
无论如何都没关系.
我认为这可能就是您要寻找的 [ this.SuspendLayout [ ^ ]和 this.ResumeLayout [
You say you Inherit from Control, but I see Component.
Anyway, it shouldn''t matter.
I think this might be what you are looking for[^].
I didn''t write it, but it helped me a lot when creating a custom Component :)
Other than that I see a this.SuspendLayout[^] and a this.ResumeLayout[^] in the InitializeComponent. That could be a problem. Since the InitializeComponent is auto-generated it won''t do much good to edit it though.
Try the link to get the parent Form.
Hope it helps, good luck!


以防万一:

您知道吗,例如:

Asking just in case:

Do you know that, for example:

this.Controls.Add(this.button2);




具有完全相同的效果



has strictly the same effect as

this.button2.Parent = this;



—SA



—SA


这篇关于DesignMode,何时设置Control.Parent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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