今天想用 .net core做个小程序。却意外的发现没有可视化编辑器!!!(重要的事情用三个感叹号)
虽然我搞了十多年的 .net 开发,但是我确实从来没有自己编辑过 Designer.cs 这个文件。微软不是不让我编辑么!
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
我于是去网上搜,结果,人家确实是编辑Designer.cs文件的。
不过,我还是想动一点小聪明,逃避学习编辑 Designer.cs 。我想到的办法很简单,就是先用老的Windows Form(.net framework)程序,可视化的绘制好界面,然后复制到新的 windows form (.net core)。
绘制 windows form ( .net framework )
我的目标是绘制一个下拉框Combobox, 一个文本框textbox, 两个按钮 Buttons。相信绝大多数人如果直接敲代码绘制这样一个简单的windows form,也要费半天劲。有了工具栏(Toolbox),就简单多了。
别忘了双击两个按钮,把事件也加上。
复制代码
这四个控件的代码,分为四个部分。分别是变量声明部分,变量实例化部分,具体设置部分,添加控件部分。另外事件也要复制过去。
变量声明部分
既给Form四个属性(Property),分别对应四个控件。所以,这段代码肯定在class的下面。具体到Designer.cs文件里,它们位于最底部。
如下:
private System.Windows.Forms.TextBox srtFileTextBox;
private System.Windows.Forms.Button browseButton;
private System.Windows.Forms.Button okayButton;
private System.Windows.Forms.ComboBox tvShowComboBox;
找到Windows Forms(.net core)程序的对应部分,复制过去。
变量实例化部分
这部分位于InitializeComponent函数的最开头。
private void InitializeComponent()
{
this.srtFileTextBox = new System.Windows.Forms.TextBox();
this.browseButton = new System.Windows.Forms.Button();
this.okayButton = new System.Windows.Forms.Button();
this