Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';是否在自定义控件中设置属性的默认值?_C#_Winforms_Properties_Custom Controls_Default - Fatal编程技术网

C# Can';是否在自定义控件中设置属性的默认值?

C# Can';是否在自定义控件中设置属性的默认值?,c#,winforms,properties,custom-controls,default,C#,Winforms,Properties,Custom Controls,Default,问题是我已经在自定义控件构造函数中初始化了属性的所有初始值,例如,PropA=true。但是,当将最终自定义控件拖放到窗体上时,有些值会更改为不同的值(例如:PropA=false) 我能理解为什么会发生这种情况,那是因为自动生成的代码做了非常冗余的工作。我的意思是,只有我们(程序员)在properties窗口中更改的属性才应该在designer.cs文件中添加自动生成的代码。为什么它必须向designer.cs文件中添加冗余代码(有时是我的情况中不需要的代码)。下面是代码执行顺序的流程,它使我

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

我能理解为什么会发生这种情况,那是因为自动生成的代码做了非常冗余的工作。我的意思是,只有我们(程序员)在properties窗口中更改的属性才应该在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();
}
公共表单(){
//我在这里用InitializeComponent()方法的内容替换它

myCustomControl=新的myCustomControl();也许这会有帮助不,DefaultAttribute只是在属性窗口中标记粗体或不粗体。但是我认识到,也许我必须了解设计器,问题部分是我自己造成的。在构造函数中初始化属性会起作用。查看此链接。我相信你需要在obje中标记默认值让设计师知道默认值应该是什么。试试看!!