动态code片段C#的Visual Studio [英] Dynamic code snippet c# visual studio

查看:178
本文介绍了动态code片段C#的Visual Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个WinForms项目日常一些重复的工作。因此,我认为创造codea的片段会帮助我,但它仅用于固定的code。

我要动态地创建code段,根据控制的名称和一些条件。

我想补充的code,一旦设计部分已经完成。我这样定义 intTextboxAge 控件名称。该段应增加自动验证所有文本框,使用下面定义的函数的恒等式。

有必须要基于控件的名字preFIX(INT,STR,斗,DEC)不同的控制。像这样的:

 公共无效自动code()
{
    INT I = 0;
    的foreach(在所有控件)
    {
        如果(对照为文本框或组合框)
        {
            如果(control.text开始INT)
            {
                A [1] = Validation.ValidateInt(labelError,control.text,VAL => acdnt.date = VAL);
            }
        }
    }
}
 

我希望有一个自动生成的code段,图书馆将无法帮助我。

我的动机是不产生code进行验证只能通过上面的例子,我们只是如何能做到这一点。

我要自动生成我所有的业务逻辑$ C $下主胜形式,如

  1. 验证
  2. 在创建新类变量
  3. Datafilling的验证后级
  4. 自动创建数据库函数插入和更新

由于在上述所有任务,唯一的变量名称更改其余业务任务依然不变。我们怎样才能实现

自动创建类的 - 类将与窗体名称+类和变量类型创建将前3个字符识别并命名相同的控制名称

自动创建数据库函数插入和更新的。 - 将其命名数据库表名相同的形式名和列名相同控件名称,以便它可以动态地创建插入和更新查询还

为什么我不想用类库,因为在这种情况下,它执行的所有操作在运行时,将地方吃我的表现。

有了这个,我们可以节省大量的时间和编码世界的努力。

解决方案
  

我想补充的code,一旦设计部分已经完成。我这样定义intTextboxAge控制名字。该段应增加自动验证所有文本框,使用下面定义的功能。

这将是最好有CustomControls用自己的验证,这样就不需要添加code的设计部分完成后:

  //整数输入,验证标签CustomControl(见上图)
公共部分类intTextBox:用户控件
{
    公共布尔的IsValid {获取;集;}
    公共intTextBox()
    {
        的InitializeComponent();
        this.textBox1.TextChanged + = this.intTextBox_TextChanged;
    }

    私人无效intTextBox_TextChanged(对象发件人,EventArgs的)
    {
        INT N;
        的IsValid = int.TryParse(this.textBox1.Text,n个);
        label1.Visible = IsValid的!;
    }
}
 


  

有必须要基于控件的名字preFIX(INT,STR,斗,DEC)

不同的控件

虽然可以使用控件名称preFIX的,我建议创建自己的用户控件的通过派生关闭基地控制的和简单的测试控制类型:

  //十进制输入用户控件
公共类decTextBox:文本框
{
    公共字符串文本
    {
        得到 {
            返回this.Text;
        }
    }

    公共布尔的IsValid()
    {
        小数N;
        布尔isDecimal = decimal.TryParse(this.Text,n个);
        返回isDecimal;
    }
}
 

...

 公共控制自动code(控制forEgTheForm)
{
    //提示:您可能需要递归调用这个函数来检查孩子的控制是有效的
    的foreach(在forEgTheForm.Controls VAR CTRL){
        如果(CTRL是TextBoxBase){
             如果(CTRL是decTextBox){
                 decTextBox TXT =((decTextBox)CTRL);
                 //如果它不是一个有效的值,然后设置[I] =真/假或在本例中返回控制...
                 如果返回CTRL(txt.IsValid()!);
             }
        }
    }
}
 


  

我要动态地创建code段,根据控制的名称和一些条件。

如果您要创建code片段做它根据控制类型,而不是控件的名称preFIX的,所以你不需要他们是动态的。

...


这将是更优雅不写任何代码段或验证code。这是理想的解决方案。我建议通过使用正确类型的控件这样做。

文本框是良好的字符串,但对于int的,DEC的和后海湾干线的你最好使用NumericUpDown控件。这样,用户将直观地知道他们需要输入数字(和将无法输入字母数字字符)。设置几个NumericUpDown控件的属性(在设计时或运行时)后,你不会需要code的验证:


我不知道你遇到了性能下降的原因是什么?虽然我建议你将控件绑定到一个类中的一类库所有的业务逻辑,这样你就可以单元测试。对于前端的验证,虽然只是验证输入是否正确,如上图所示是要走的路。

I am working on a WinForms project with some repetitive tasks everyday. So I thought creating code a snippet will help me out, but it works for fixed code only.

I want to dynamically create a code snippet, according to control names and some condition.

I want to add the code once the design part is done. I define the control names like intTextboxAge. The snippet should add auto validation for all textboxes, using the fuction defined below.

There have to be different controls based on the control's name prefix (int, str, dou, dec). Like such:

public void AutoCode()
{
    int i=0;
    foreach(On all controls)
    { 
        if(controls is textbox or combobox)
        {
            if(control.text starts with int)
            {
                a[i] = Validation.ValidateInt(labelError, control.text, val => acdnt.date = val);
            }
        }
    }
}

I want an auto generated code snippet, libraries will not be able to help me.

My motive is not to generate code for validation only by above example is just how we can do this.

I want to auto generate my all business logic code for master win forms like

  1. Validation
  2. Creating new Class for variables
  3. Datafilling in class after validation
  4. Auto creation of database function insert and update

Because in all above task only variable name changes rest business task remains same. How we can implement

Auto Creation of class- Class will created with by form name+"Class" and variable types will identified by first 3 char and will named same as control name.

Auto creation of database function insert and update - Will name database table name same as form name and column name same as control name, so that it can dynamically create insert and update query also.

Why i don't want to with class library because in that case it perform all operation at run time which will somewhere eat my performance.

With this we can save lots of time and efforts of coding world.

解决方案

I want to add the code once the design part is done. I define the control names like intTextboxAge. The snippet should add auto validation for all textboxes, using the function defined below.

It would be better to have CustomControls with their own Validation, this way you don't need to add code after the design part is done:

//Integer input CustomControl with validation label (pictured above)
public partial class intTextBox : UserControl
{
    public bool IsValid {get;set;}
    public intTextBox()
    {
        InitializeComponent();
        this.textBox1.TextChanged += this.intTextBox_TextChanged;
    }

    private void intTextBox_TextChanged(object sender, EventArgs e)
    {
        int n;
        IsValid = int.TryParse(this.textBox1.Text, out n);
        label1.Visible = !IsValid;
    }
}


There have to be different controls based on the control's name prefix (int, str, dou, dec)

While you can use control name prefix's I recommend creating your own UserControls by deriving off base controls and simply testing the control type:

//Decimal input UserControl
public class decTextBox : TextBox
{
    public string Text
    {
        get {
            return this.Text;
        }
    }

    public bool IsValid()
    {
        decimal n;
        bool isDecimal = decimal.TryParse(this.Text, out n);
        return isDecimal;
    }
}

....

public Control AutoCode(Control forEgTheForm)
{
    //Tip: You may need to recursively call this function to check children controls are valid
    foreach(var ctrl in forEgTheForm.Controls) { 
        if(ctrl is TextBoxBase) {
             if(ctrl is decTextBox) {
                 decTextBox txt = ((decTextBox)ctrl);
                 //If its not a valid value then set a[i]=true/false or in this example return the control...
                 if (!txt.IsValid()) return ctrl;
             }
        }
    }
}


I want to dynamically create a code snippet, according to control names and some condition.

If you're going to create code Snippets do it according to control types, not Control name prefix's so you dont need them to be dynamic.

...


It would be even more elegant not to have to write any Snippets or validation code. This is the ideal solution. I recommend doing this by using the right type of controls.

Textboxes are good for string, however for int's, dec's and dbl's you're better off using the NumericUpDown control. This way users will intuitively know they need to enter numbers (and wont be able to enter alphanumeric characters). After setting a couple of NumericUpDown control properties (either at design time or run time) you wont need to code in validation:


I'm not sure about the cause of the performance degradation you're encountering? Though I suggest you bind controls to a class in a class library with all the business logic so that you can Unit Test. For the front-end validation though simply validating inputs are correct as shown above is the way to go.

这篇关于动态code片段C#的Visual Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆