developers tips ..... "C# tips"
  TOP  BACK
 
C# tips ..... C#言語 プログラミングテクニック  >  フォーム (Microsoft .NET Framework)
 
 フォーム
 
ウインドウを表示する。
 
  System.Windows.Forms.Formクラスを継承してインスタンスを生成し、アプリケーションフォームとして実行します。
using System.Windows.Forms;
class Test1:Form {
    public static void Main(string[] args) {
        Application.Run(new Test1());
    }
    public Test1() {
        this.Text = "TestForm";
        this.Height = 100;
        this.Width = 100;
    }
}

アプリケーションフォームとして実行すると、フォームの終了と同時にアプリケーションも終了します。アプリケーションフォームとしない場合は、インスタンスに対してVisibleプロパティにtrueを設定する事で表示できます。
        Form form2 = new Form();
        form2.Visible = true;

ウインドウの終了イベントをハンドルする。
 
  System.Windows.Forms.FormクラスのClosingイベントに、System.ComponentModel.CancelEventHandlerデリゲートを設定します。System.ComponentModel.CancelEventHandlerに終了メソッドを引き渡します。
using System;
using System.ComponentModel;
using System.Windows.Forms;
class Test1:Form {
    public static void Main(string[] args) {
        Application.Run(new Test1());
    }
    public Test1() {
        this.Text = "TestForm";
        this.Height = 100;
        this.Width = 100;
        this.Closing += new CancelEventHandler(this.formclosing);
    }
    public void formclosing(object sender,CancelEventArgs e){
          :
        終了時の処理を記述
          :
    }
}

終了イベントをキャンセルしてフォームの動作を復帰させる時は、イベントメソッド引数CancelEventArgsクラスのCancelプロパティにtrueを設定します。
    public void formclosing(object sender,CancelEventArgs e){
          :
        e.Cancel = true;
          :
    }

ウインドウ終了後のイベントをハンドルするには、Closedイベントを設定します。
    public Test1() {
          :
        this.Closed += new EventHandler(this.formclosed);
          :
    }
    public void formclosed(object sender,EventArgs e) {
          :
        終了後の処理を記述
          :
    }

ウインドウの開始イベントをハンドルする。
 
  System.Windows.Forms.FormクラスのLoadイベントに、System.ComponentModel.EventHandlerデリゲートを設定します。System.ComponentModel.EventHandlerに開始メソッドを引き渡します。
using System;
using System.ComponentModel;
using System.Windows.Forms;
class Test1:Form {
    public static void Main(string[] args) {
        Application.Run(new Test1());
    }
    public Test1() {
          :
        this.Load += new EventHandler(this.formload);
          :
    }
    public void formload(object sender,EventArgs e) {
          :
        開始時の処理を記述
          :
    }
}

ウインドウのサイズ変更イベントをハンドルする。
 
  System.Windows.Forms.FormクラスのSizeChangedイベントに、System.ComponentModel.EventHandlerデリゲートを設定します。System.ComponentModel.EventHandlerに開始メソッドを引き渡します。
using System;
using System.ComponentModel;
using System.Windows.Forms;
class Test1:Form {
    public static void Main(string[] args) {
        Application.Run(new Test1());
    }
    public Test1() {
          :
        this.SizeChanged += new EventHandler(this.formsize);
          :
    }
    public void formsize(object sender,EventArgs e) {
          :
        サイズ変更時の処理を記述
          :
    }
}

メッセージボックスを表示する。
 
  System.Windows.Forms.MessageBoxクラスのShowメソッド(static)でメッセージボックスを表示します。
using System.Windows.Forms;
class Test1:Form {
    public static void Main(string[] args) {
        MessageBox.Show("メッセージ");
    }
}

メッセージボックスのダイアログにタイトルを設定するには、Show(string,string)メソッドを使用します。
using System.Windows.Forms;
class Test1:Form {
    public static void Main(string[] args) {
        MessageBox.Show("メッセージ","タイトル");
    }
}

メッセージボックスのダイアログにOKボタン以外のボタンを配置するには、Show(string,string,MessageBoxButtons)メソッドを使用します。MessageBoxButtons列挙体には、次の値があります。
AbortRetryIgnore
OK
OKCancel
RetryCancel
YesNo
YesNoCancel
「中止」「再試行」「無視」ボタンを配置します。
「OK」ボタンを配置します。
「OK」「キャンセル」ボタンを配置します。
「再試行」「キャンセル」ボタンを配置します。
「はい」「いいえ」ボタンを配置します。
「はい」「いいえ」「キャンセル」ボタンを配置します。

どのボタンが押されたかは、Showメソッドの戻り値であるDialogResult列挙体で判定します。DialogResult列挙体には、次の値があります。
Abort
Cancel
Ignore
No
None
OK
Retry
Yes
「中止」
「キャンセル」
「無視」
「いいえ」
未選択
「OK」
「再試行」
「はい」

コード例は次のようになります。
using System.Windows.Forms;
class Test1:Form {
    public static void Main(string[] args) {
        if (MessageBox.Show("メッセージ","タイトル",MessageBoxButtons.YesNo) == DialogResult.Yes) {
            // Yes の処理
        } else {
            // No の処理
        }
    }
}

 


  TOP  BACK
 
 
 
 
製作・著作 たぐぴょん
当サイトで紹介している製品・書籍等の著作権などの権利は、各権利者にあります。

55 STREET / 0574 W.S.R / STRAWBERRY7 / アレコレネット / モノショップ / ミツケルドット