验证我的表格

时间:2013-02-25 09:59:49

标签: c# .net winforms textbox

我是.Net Framework的新用户,我想在Visual Studio 2010 IDE中为我的Windows表单应用程序添加验证。我已经搜索了不同的方法,但我不确定在哪里可以添加我的表单中的代码?其中一个例子是下面的代码。

我是否在表单加载方法或提交按钮或其他位置添加此代码?

using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }

        [Required(ErrorMessage = "Title is required")]
        public string Title { get; set; }

        [Required(ErrorMessage = "Date is required")]
        public DateTime ReleaseDate { get; set; }

        [Required(ErrorMessage = "Genre must be specified")]
        public string Genre { get; set; }

        [Required(ErrorMessage = "Price Required")]
        [Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
        public decimal Price { get; set; }

        [StringLength(5)]
        public string Rating { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }
}

2 个答案:

答案 0 :(得分:1)

尝试使用公共属性TextBox(如数字,文本)和所有内容创建自定义ControlType,然后为每种类型编写实现。代码示例如下。

class CustomTextbox : TextBox
{
    private ControlType _controlType;

    public CustomTextbox()
    {
        Controltype = ControlType.Number;
    }

    public ControlType Controltype
    {
        get { return _controlType; }
        set
        {
            switch (value)
            {
                case ControlType.Number:
                    KeyPress += textboxNumberic_KeyPress;
                    MaxLength = 13;
                    break;

                case ControlType.Text:
                    KeyPress += TextboxTextKeyPress;
                    MaxLength = 100;
                    break;
            }
            _controlType = value;
        }
    }

    private void textboxNumberic_KeyPress(object sender, KeyPressEventArgs e)
    {
        const char delete = (char)8;
        const char plus = (char)43;
        e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != delete && e.KeyChar != plus;
    }

    private void TextboxTextKeyPress(object sender, KeyPressEventArgs e)
    {
        const char delete = (char)8;
        const char plus = (char)43;
        e.Handled = Char.IsDigit(e.KeyChar);
    }

}

public enum ControlType
{
    Number,
    Text,
}

构建您的解决方案。从Toolbox中选择新创建的控件。在表单中拖动,然后从ControlType更改Property Window属性。示例仅显示数字和文本,但您可以扩展电话,电子邮件和所有内容。

修改

枚举中的默认标记也可以使其成为正常的Textbox。在这种情况下,不要忘记将事件脱钩。

希望它有所帮助。

答案 1 :(得分:0)

我认为您应该使用IDataErrorInfo界面(请参阅here

Here是一个关于如何实现它的示例

它是这样的:

public class Movie : IDataErrorInfo
{
   public int ID { get; set; }

  //other properties removed for clearyfication

       private string _lastError = "";

        public string Error
        {
            get { return _lastError; }
        }

        public string this[string columnName]
        {
            get 
            {
               if(columnName == "ID" && ID < 0)
                 _lastError = "Id must be bigger that zero";
            }
        }

}