1

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

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

您的帮助将不胜感激!

先感谢您!

4

0 回答 0