无法在自定义控件中设置属性的默认值?

时间:2013-04-30 02:11:38

标签: c# winforms properties custom-controls default

问题是我已经在我的自定义控件构造函数中初始化了我的属性的所有初始值,例如,PropA = true。但是,当将最终的Custom控件拖放到表单上时,有些值会更改为不同的值(例如:PropA = false)。

我可以理解为什么会发生这种情况,这是因为自动生成的代码会执行非常多余的工作。我的意思是,只有我们(程序员)在属性窗口中更改的属性应该在designer.cs文件中添加自动生成的代码。为什么必须在designer.cs文件中添加冗余代码(有时候,我的情况下需要不需要的代码)。以下是代码执行顺序的流程,它使我的默认值消失:

public Form(){
   //I replace the InitializeComponent() method by its content right in here
   myCustomControl = new MyCustomControl(); <---- everything is already
   //set up OK at here, no need auto-generated code for my custom properties.

   SuspendLayout();

   /////Initialize properties
   //My custom properties go here, they are already set to default values in my
   //custom control constructor (the line right at the very top above).
   //And that means, all the auto-generated code below are preparing to erase
   //all those default values unexpectedly.

   myCustomControl.PropA = false; //While, PropA is already set to true 
   //in MyCustomControl() constructor and what I want is it should be true, not false
   //but the designer doesn't understand me or I have to understand it????

   //The same for other custom properties of mine
   ....
   ....
   ResumeLayout(false);
   PerformLayout();
}

我想知道如何理解设计师或如何让它理解我?

非常感谢您的帮助!

提前谢谢!

0 个答案:

没有答案