让VS.Net根据表结构自动生成界面和C#代码

Visual Studio根据表结构自动生成查询画面和C#代码

原理:

1.通过C#编写VS的插件;调用VS的设计器;创建控件和代码

2.SQL代码通过正规表达式解析出查询条件并生成控件

3.通过SQL代码获得查询的DataTable并获得DataColumn;并创建Grid

 

具体代码如下

IDesignerHost host;VS设计器的host

Form forhm = (Form)host.RootComponent; 获得设计器的界面

 

创建控件两种方法:

1.通过C#的动态创建控件(如:new Button())

PropertyDescriptor poss;
Button btn1 = new Button();
btn1.Name = "btn_OK";
btn1.Text = "查询";
btn1.Size = new System.Drawing.Size(100, 33);
btn1.Location = new System.Drawing.Point(662, (Line + 1) * 10 + (Line + 1) * 21 + 10);
pl1.Controls.Add(btn1);
forhm.Container.Add(btn1);
poss = TypeDescriptor.GetProperties(btn1).Find("Name", true);
poss.SetValue(btn1, "btn_OK");

2.通过反射动态创建控件

Control cl = (Control)Assembly.LoadFrom(compath).CreateInstance(ClassName);
cl.Location = new System.Drawing.Point(0, y);
cl.Name = "AAAA";
cl.RightToLeft = System.Windows.Forms.RightToLeft.No;
cl.Size = new System.Drawing.Size(120, 21);
cl.Location = new System.Drawing.Point(70 + 70 + ColV * 220 - cl.Size.Width - 5, (LineV + 1) * 10 + LineV * 20 + 5);
form.Container.Add(cl);
PropertyDescriptor pop = TypeDescriptor.GetProperties(cl).Find("Parent", true);
pop.SetValue(cl, form);
 PropertyDescriptor valu = TypeDescriptor.GetProperties(cl).Find("Name", true);
valu.SetValue(cl, ClassName_Start + rd[1].ToString());

动态创建代码

public static DTE AplictionDTE;

Document activeDocument = AplictionDTE.ActiveDocument;
ProjectItem projectItem = activeDocument.ProjectItem;
FileCodeModel fileCodeModel = projectItem.FileCodeModel;
CodeElements codeElements = fileCodeModel.CodeElements;

TextSelection ts = (TextSelection)Connect.AplictionDTE.ActiveDocument.Selection;
EditPoint ep = ts.ActivePoint.CreateEditPoint();

ep.Insert("            InitControl();\r\n");
ep.Insert("            btn_Cancel.Click += new EventHandler(btn_Cancel_Click);\r\n");
ep.Insert("\r\n");

注意:

创建控件前需要将此控件的使用到的DLL引用到VS的项目中

 

运行截图如下:

1.VS的原始设计界面

2.启动VS插件并设置SQL语句

3.设置SQL的条件的名称

4.自动生成的界面

5.自动生成的C#代码

 

转载于:https://www.cnblogs.com/DesignIvan/archive/2013/06/09/VSNet.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值