- - PR -
TabControlのTabPage上のコントロール動作について
1
投稿者 | 投稿内容 | ||||
---|---|---|---|---|---|
|
投稿日時: 2007-07-24 09:45
C#(VS2003)で開発を行っております。
TabContorl上にTabPageを2ページ配置して1ページ目にTextBoxを 配置しているプログラムがあります。 そのTextBoxのLeaveイベントにて入力チェックを行っているのですが、 フォーカスがTextBox上にある段階でTabPageの2ページ目をマウスにて クリックすると不可解な現象が発生しています。 1.TextBoxのLeaveイベントが2回発生する 2.TabPageが2ページ目に移動するのにフォーカスがTextBox上にある 実現したい機能としては、1ページ目のTextBoxでエラーが発生した場合は どんな場合でもフォーカスを移動させたくないと考えています。 サンプルコードを以下に提示します。 どのように解決するのが望ましいかご教授願います。 〜〜〜〜〜〜〜〜〜〜 以下 サンプルコード 〜〜〜〜〜〜〜〜〜〜 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace TEST { /// <summary> /// Form1 の概要の説明です。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.TabPage tabPage2; private System.Windows.Forms.TextBox textBox1; /// <summary> /// 必要なデザイナ変数です。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // } /// <summary> /// 使用されているリソースに後処理を実行します。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows フォーム デザイナで生成されたコード /// <summary> /// デザイナ サポートに必要なメソッドです。このメソッドの内容を /// コード エディタで変更しないでください。 /// </summary> private void InitializeComponent() { this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.tabPage2 = new System.Windows.Forms.TabPage(); this.textBox1 = new System.Windows.Forms.TextBox(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); this.SuspendLayout(); // // tabControl1 // this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Controls.Add(this.tabPage2); this.tabControl1.Location = new System.Drawing.Point(10, 20); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(272, 172); this.tabControl1.TabIndex = 0; // // tabPage1 // this.tabPage1.Controls.Add(this.textBox1); this.tabPage1.Location = new System.Drawing.Point(4, 21); this.tabPage1.Name = "tabPage1"; this.tabPage1.Size = new System.Drawing.Size(264, 147); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "tabPage1"; // // tabPage2 // this.tabPage2.Location = new System.Drawing.Point(4, 21); this.tabPage2.Name = "tabPage2"; this.tabPage2.Size = new System.Drawing.Size(264, 147); this.tabPage2.TabIndex = 1; this.tabPage2.Text = "tabPage2"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(14, 12); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 0; this.textBox1.Text = ""; this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 12); this.ClientSize = new System.Drawing.Size(292, 204); this.Controls.Add(this.tabControl1); this.Name = "Form1"; this.Text = "Form1"; this.tabControl1.ResumeLayout(false); this.tabPage1.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// アプリケーションのメイン エントリ ポイントです。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void textBox1_Leave(object sender, System.EventArgs e) { if (this.textBox1.Text.TrimEnd() == "") { MessageBox.Show("入力必須"); textBox1.Focus(); } } } } 〜〜〜〜〜〜〜〜〜〜 以上 サンプルコード 〜〜〜〜〜〜〜〜〜〜 | ||||
|
投稿日時: 2007-07-24 10:24
フォーカス遷移のキャンセルは普通は Validating イベントのイベント引数を使いますよね。 Leave イベントにしなければならない理由があるのでしょうか?
_________________ C# と VB.NET の入門サイト じゃんぬねっと日誌 | ||||
|
投稿日時: 2007-07-24 10:32
じゃんぬねっとさん ご返答ありがとうございます。
上記の返答になるかどうかは分かりかねますが、 実はコントロールを拡張しておりまして、Leaveイベント時(OnLeave)に 各チェックを行っている仕組みのコントロールを使用しております。 そのためLeaveイベントにて仕組みを実現したいと考えております。 何とか方法はない物でしょうか? | ||||
|
投稿日時: 2007-07-24 10:40
それはそのコントロールの作り方が間違っています。 フォーカスを移してよいかのチェックはValidatingで行うべきです。 それ以外の方法で行ってもできなくはないはずですが、 とても追いきれないほどの様々な問題が発生します。 たとえ動いたとしても怖くてとても使えません。 Leaveでチェック行い、結果をどこかに保存して ダメな場合はValidatingでCancelするという方法でいけそうな気がしますが、 フォーカスを移してよいかの判断はValidatingで行うべきというのは変わりません。 コントロールを作り直すのが早いと思いますよ。 |
1