ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文

  メンテナンス 前画面に戻る

対象スレッド 件名: Form のカスタマイズサンプル
名前: lightbox
処理選択
パスワード

件名 Form のカスタマイズサンプル
名前 lightbox
コメント
@C:GREEN(
● 閉じるボタンをクリックしても、反応しないようにする
● そのかわりに、ESC キーで確認ダイアログを出して終了させる
● 共通の初期化処理
● Enter キーで 次のコントロールに移動するようにする
)

@DIV
@C:red(Partial) Class Form1

	' ******************************************************
	' 閉じるボタンを無効にする
	' ******************************************************
	Protected Overrides Sub @C:red(WndProc)( _
	 ByRef message As System.Windows.Forms.Message)

		' WinUser.h
		' #define WM_SYSCOMMAND		0x0112
		' #define SC_CLOSE			0xF060

		If message.Msg = &H112 And message.WParam.ToInt32() = &HF060 Then
			Return
		End If

		MyBase.WndProc(message)

	End Sub

	' ******************************************************
	' KeyPress での処理を有効にする為の割り込み
	' ******************************************************
	Protected Overrides Sub @C:red(OnLoad)(ByVal e As System.EventArgs)

		MyBase.KeyPreview = True
		MyBase.OnLoad(e)

	End Sub

	' ******************************************************
	' Enter キーで次のコントロールへ移動
	' ( フォームの KeyPreview : True )
	' SelectNextControl パラメータの説明
	' 【forward】
	' タブ オーダー内を前方に移動する場合は true。後方に移動する場合は false。 
	' 【tabStopOnly】
	' TabStop プロパティが false に設定されているコントロールを無視する場合は true。
	' 【nested】
	' 入れ子になった (子コントロールの子) 子コントロールを含める場合は true。 
	' 【wrap】
	' タブ オーダーの最後のコントロールに到達した後、
	' タブ オーダーの最初のコントロールから検索を続行する場合は true 
	'
	' ★ ボタンの場合は押された事になります
	' ******************************************************
	Protected Overrides Sub @C:red(OnKeyPress)(ByVal e As System.Windows.Forms.KeyPressEventArgs)

		If e.KeyChar = ControlChars.Cr Then
			Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
			e.Handled = True
		End If

		MyBase.OnKeyPress(e)

	End Sub

	' ******************************************************
	' ESC による終了処理
	' ******************************************************
	Protected Overrides Sub @C:red(OnKeyDown)(ByVal e As System.Windows.Forms.KeyEventArgs)

		If e.KeyCode = Keys.Escape Then
			If MessageBox.Show( _
			 "終了しますか?", _
			 "ESC", _
			 MessageBoxButtons.YesNo, MessageBoxIcon.Question _
			 ) = Windows.Forms.DialogResult.Yes Then
				Application.Exit()
			End If

		End If

		MyBase.OnKeyDown(e)
	End Sub


End Class
@END

@C:GREEN(
● 初期処理として、起動していない場合は MySQL をサービスで実行させる
● 一つのボタンに対して二つの処理を行う
)

@DIV
' ↓参照設定が必要
Imports System.ServiceProcess

@C:red(Public) Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, _
	 ByVal e As System.EventArgs) Handles MyBase.Load

		' サービスが実行されていない場合にサービスを実行
		Dim sc As New ServiceController()
		sc.ServiceName = "MySQL51"
		If sc.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
			sc.Start()
		End If

	End Sub

	Private Sub @C:red(Button1_Click)(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles @C:green(Button1.Click)

		MessageBox.Show("Push")

	End Sub

	Private Sub @C:red(Button2_Click)(ByVal sender As System.Object, _
	 ByVal e As System.EventArgs) Handles @C:green(Button1.Click)

		Application.Exit()

	End Sub

End Class
@END