前提・実現したいこと
データフィールドを必須としたときの必須チェックはいつ行うのでしょうか?
Windows Formアプリケーションで、テキストボックスをふたつ配置し、ひとつのプロパティを作りました。
System.ComponentModel.DataAnnotationsを参照で追加しました。
発生している問題・エラーメッセージ
これで、textBox1のTextをプロパティに設定できますが、その内容が空でもとくに文句を言われません。
たとえば、テキストボックスをふたつ用意したので、tabキーを押してテキストボックスを遷移するようなときにRequiredだとnullですみたいなメッセージが出るのかと期待したのですが、違うようです。
このRequiredは動いているのでしょうか?
それともなにかのイベントで中身を自分で実装しないとダメなのでしょうか?
該当のソースコード
C#
1using System; 2using System.ComponentModel; 3using System.ComponentModel.DataAnnotations; 4using System.Windows.Forms; 5 6namespace WindowsFormsApplication1 7{ 8 public partial class Form1 : Form 9 { 10 public Form1() 11 { 12 InitializeComponent(); 13 } 14 15 [Required] 16 public string text1 { 17 get {return textBox1.Text; } 18 set { textBox1.Text = value; } 19 } 20 } 21}
C#
1namespace WindowsFormsApplication1 2{ 3 partial class Form1 4 { 5 /// <summary> 6 /// 必要なデザイナー変数です。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 使用中のリソースをすべてクリーンアップします。 12 /// </summary> 13 /// <param name="disposing">マネージ リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 private void InitializeComponent() 24 { 25 this.textBox1 = new System.Windows.Forms.TextBox(); 26 this.textBox2 = new System.Windows.Forms.TextBox(); 27 this.SuspendLayout(); 28 // 29 // textBox1 30 // 31 this.textBox1.Location = new System.Drawing.Point(94, 96); 32 this.textBox1.Name = "textBox1"; 33 this.textBox1.Size = new System.Drawing.Size(100, 19); 34 this.textBox1.TabIndex = 0; 35 // 36 // textBox2 37 // 38 this.textBox2.Location = new System.Drawing.Point(94, 164); 39 this.textBox2.Name = "textBox2"; 40 this.textBox2.Size = new System.Drawing.Size(100, 19); 41 this.textBox2.TabIndex = 1; 42 // 43 // Form1 44 // 45 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 46 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 47 this.ClientSize = new System.Drawing.Size(284, 261); 48 this.Controls.Add(this.textBox2); 49 this.Controls.Add(this.textBox1); 50 this.Name = "Form1"; 51 this.Text = "Form1"; 52 this.ResumeLayout(false); 53 this.PerformLayout(); 54 55 } 56 57 private System.Windows.Forms.TextBox textBox1; 58 private System.Windows.Forms.TextBox textBox2; 59 } 60} 61 62
追記
C#
1 ValidationContext vc = new ValidationContext(textbox); 2 var validationResults = new Collection<ValidationResult>(); 3 bool result = false; 4 result = TryValidateObject(textbox, vc, validationResults, result); 5 if (!result) 6 MessageBox.Show("Empty"); 7 8 public static bool TryValidateObject( 9 object instance, 10 ValidationContext validationContext, 11 ICollection<ValidationResult> validationResults, 12 bool validateAllProperties 13 ) 14 { 15 TextBox textbox = instance as TextBox; 16 if (string.IsNullOrEmpty(textbox.Text)) return false; 17 else return true; 18 } 19
補足情報(FW/ツールのバージョンなど)
Visual Studio 2015 Pro
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/03/23 00:37
2018/03/23 00:44
2018/03/23 01:26