Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating my form

I am new to .Net Framework and I want to add validations to my windows form application in Visual Studio 2010 IDE. I have searched for different ways to do it but I am not sure where can i add that code in my form? One of the example being the code below.

Do I add this code on form load method or on 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; }
    }
}
like image 445
ankita alung Avatar asked Nov 12 '22 10:11

ankita alung


1 Answers

Try creating a custom TextBox with a public property of ControlType (like number, Text) and all and then write your implementation for each type. Code sample given 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,
}

Build your Solution. Pick the newly created control from Toolbox. Drag in the form and then change ControlType property from Property Window. Sample only shows number and text but you can extend things for Phone, email and all.

Edit

Can also a default tag in enum which will make it a normal Textbox. In this case, dont forget to delink the events.

Hope it helps.

like image 133
Sandy Avatar answered Nov 15 '22 06:11

Sandy