5

.Net Framework慣れていないので、Windows フォーム アプリケーションにバリデーションを追加したいと考えていますVisual Studio 2010 IDE。それを行うさまざまな方法を検索しましたが、そのコードをフォームのどこに追加できるかわかりません。例の 1 つが以下のコードです。

このコードをフォーム ロード メソッド、送信ボタン、またはその他の場所に追加しますか?

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

2 に答える 2

1

(number、Text など) と allTextBoxのパブリック プロパティを使用してカスタムを作成し、各型の実装を作成してみてください。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。サンプルは数字とテキストのみを示していますが、電話、電子メール、およびすべてのものを拡張できます。

編集

enum のデフォルト タグにすることもできます。これにより、通常のTextbox. この場合、イベントのリンクを解除することを忘れないでください。

それが役に立てば幸い。

于 2013-02-25T11:06:29.207 に答える
0

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

}
于 2013-02-25T11:38:33.427 に答える