When not using VS designer, should I add code to Form1.designer.cs?
I am not sure about that. I do not want to use designer for many things (but for some I do). Should I write my "manual" code into the designer.cs or to 开发者_JAVA百科form class itself?
No, never.
Because it will be generated again and your written-code will be lost.
In order to add code to a class which has designer, you need to define a partial class with the same name.
public partial class Form1
{
// here write your logic (which will be preserved).
}
For more info: Partial Class (C#)
You should really not do that. Each time you change something in the designer, it will regenerate the InitializeComponent()
method. Therefore, your changes will be overwritten by that code. The only way this would not happen is to write your code in the same way the designer does - which is almost impossible. It could also happen that your designer crashes after you changed the code and you may not know why. You should better stick with the designer.
精彩评论