Validation of my form

I'm new to .Net Framework, and I want to add validation to my windows forms application in Visual Studio 2010 IDE. I have been looking for different ways to do this, but I'm not sure where I can add this code to my form? One example is the code below.

Add this code to the form upload method or submit button or somewhere else?

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; }
    }
}
+5
source share
2 answers

Try creating a custom one TextBoxwith a public property ControlType(for example, number, text) and that’s it, and then write your implementation for each type. Sample code below.

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. , , .

Edit

, TextBox. .

, .

+1

, IDataErrorInfo (. )

,

:

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";
            }
        }

}
0

All Articles