当前位置: 代码迷 >> .NET组件控件 >> 设计器InitializeNewComponent中设置的值如何VS不自动添加代码
  详细解决方案

设计器InitializeNewComponent中设置的值如何VS不自动添加代码

热度:6972   发布时间:2013-02-25 00:00:00.0
设计器InitializeNewComponent中设置的值怎么VS不自动添加代码
C# code
        class MyDesigner : ControlDesigner        {            public override void InitializeNewComponent(                IDictionary defaultValues)            {                 base.InitializeNewComponent(defaultValues);//主要是这里设置默认路径,在设计时可以看到,//但是在运行时应没有效果//而且在属性设计器中更改了Text属性为其他值,在设计时也看到了,但是运//行后应看不到效果,找到InitializeComponent方法,也没有VS自动为其添加的代码//this.FilePathTextBox.Text="设置的值";//怎样才能设计时和运行时都可以                (this.Control as FilePathTextBox).Text = DefaultPath;            }        }
C# code
using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Windows.Forms.Design;namespace cjl.Win.Controls{    [Designer(typeof(MyDesigner))]    public partial class FilePathTextBox : PathTextBox    {        static readonly string DefaultPath = @"C:\My.txt";        public FilePathTextBox()            : base()        {            Filter = "All Files|*.*";            this.InitializeComponent();            //this.Text = DefaultPath;        }        public bool CheckExists { get; set; }        public string Filter { get; set; }        protected override string SelectPath()        {            OpenFileDialog dialog = new OpenFileDialog();            dialog.AddExtension = true;            dialog.Filter = Filter;            dialog.CheckFileExists = CheckExists;            DialogResult result = dialog.ShowDialog();            if (result != DialogResult.OK)            {                return null;            }            return dialog.FileName;        }        #region Designer        class MyDesigner : ControlDesigner        {            public override void InitializeNewComponent(                IDictionary defaultValues)            {                 base.InitializeNewComponent(defaultValues);//主要是这里设置默认路径,在设计时可以看到,//但是在运行时应没有效果                (this.Control as FilePathTextBox).Text = DefaultPath;            }        }        #endregion    }}

C# code
using System;using System.ComponentModel;using System.Drawing.Design;using System.Windows.Forms;using System.Windows.Forms.Design;namespace cjl.Win.Controls{    public abstract partial class PathTextBox : UserControl    {        protected virtual string SelectPath()        {            return null;        }        public PathTextBox()        {            InitializeComponent();        }        [Category("杂项")]        [Browsable(true)]        [Bindable(true)]        [Editor(typeof(FolderNameEditor), typeof(UITypeEditor))]        public override string Text        {            get { return MyTextBox.Text.Trim(); }            set { MyTextBox.Text = value; }        }        public bool OnlySelect        {            get { return MyTextBox.ReadOnly; }            set { MyTextBox.ReadOnly = value; }        }        private void SelectButton_Click(object sender, EventArgs e)        {            string path = SelectPath();            if (path == null)            {                return;            }            Text = path;        }    }}


------解决方案--------------------------------------------------------
从你描述来看,
是由于没有生成 
this.FilePathTextBox.Text="设置的值";
所导致的

代码生成对于你的 Text 属性, 应该是不需要再定制一个代码序列化类的

你先试试为这个属性指定一个默认值试试,
如果 Text 不等于 默认值, 会生成序列化代码

[DefaultValue("")]
  相关解决方案