開発用 TextBox のサンプル

  仕様




1) 通常使わないプロパティは、デザイナに表示させない

2) クラス共通のプロパティを設定すると、validate が発生しない

3) DataType プロパティを実装する
     (0) : なにもしない
     (1) : 9999/99/99
     (2) : 先行ゼロ( MaxLength で桁数、デフォルトは4 )

4) DataTypeによって、以下のコントロールを行う
  • 入力の制限を行う
  • フォーカスを失った時に表示用編集を行う
  • フォーカスを得た時に編集用文字列を取り去る( 日付型のみ )
  • デフォルトの validate チェックを行う( 日付型のみ )
5) AllowChar プロパティを実装する
     指定された文字列に含まれる1つ1つの文字以外入力できなくなる

6) CheckType プロパティを実装する
     (0) : なにもしない
     (1) : 必須入力

7) CheckValue プロパティを実装する
     カンマ区切りで指定された各文字列以外は入力エラーメッセージを表示( validate )


3) 4) は、サンプルとして、YYYY/MM/DD 編集の YYYYMMDD 入力の日付型を簡易的に実装しています。
※ 日付選択のコントロールを使えば良いので、このような制御を最近はしないかもしれませんが、
※ 和暦表示や、キーボード入力を主とするシステムでは有効かもしれません。

LboxText.validateNone = True

として実行すると、validate 処理が発生しないので、キャンセル処理等に使用すると良いと思います




  コード



LboxText.vb
  
Imports System.Windows.Forms

Public Class LboxText

	Public Shared validateNone As Boolean = False

	' ****************************************************** 
	' validate コントロール用
	' ****************************************************** 
	Protected Overloads Overrides Sub OnValidating( _
	  ByVal e As System.ComponentModel.CancelEventArgs)

		' ****************************************************** 
		' validateNone が True ならば、Valdating は発生しない 
		' ****************************************************** 
		If Not validateNone Then
			MyBase.OnValidating(e)
		End If

	End Sub

	Protected Overloads Overrides Sub OnValidated( _
	 ByVal e As System.EventArgs)

		' ****************************************************** 
		' validateNone が True ならば、Validated は発生しない 
		' ****************************************************** 
		If Not validateNone Then
			MyBase.OnValidated(e)
		End If

	End Sub

	' ****************************************************** 
	' カスタムデータ型
	' ****************************************************** 
	Private _DataType As Integer = 0
	<System.ComponentModel.Description( _
	"0:文字列" & Chr(10) & _
	"1:YYYY/MM/DD" & Chr(10) & _
	"2:先行ゼロ文字列(長さはMaxLengthで指定)" _
	), _
	 System.ComponentModel.DefaultValue(0)> _
	  Public Property DataType() As Integer
		Get
			Return _DataType
		End Get
		Set(ByVal value As Integer)
			_DataType = value
		End Set
	End Property

	' ****************************************************** 
	' カスタムチェックタイプ
	' ****************************************************** 
	Private _CheckType As Integer = 0
	<System.ComponentModel.Description("0:なし" & Chr(10) & "1:必須入力"), _
	 System.ComponentModel.DefaultValue(0)> _
	Public Property CheckType() As Integer
		Get
			Return _CheckType
		End Get
		Set(ByVal value As Integer)
			_CheckType = value
		End Set
	End Property

	' ****************************************************** 
	' 編集文字列を省く 
	' ****************************************************** 
	Private Sub LboxText_Enter(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Enter

		If Me._DataType = 1 Then
			Me.MaxLength = 8
			Dim str As String = Me.Text
			str = str.Replace("/", "")
			Me.Text = str
		End If

	End Sub

	' ****************************************************** 
	' 編集する 
	' ****************************************************** 
	Private Sub LboxText_Leave(ByVal sender As System.Object, _
	 ByVal e As System.EventArgs) Handles MyBase.Leave

		If Me._DataType = 1 Then
			If Me.Text <> "" Then
				Dim str As String = _
				 Integer.Parse(Me.Text).ToString("0000/00/00")
				Me.Text = str
			End If
		End If

		If Me._DataType = 2 Then
			If Me.Text <> "" Then
				If Me.MaxLength > 1000 Then
					Me.MaxLength = 4
				End If
				Dim formatString = ""
				For i As Integer = 1 To Me.MaxLength
					formatString += "0"
				Next
				Dim str As String = _
				 Integer.Parse(Me.Text).ToString(formatString)
				Me.Text = str
			End If
		End If

	End Sub

	' ****************************************************** 
	' カスタム入力制限
	' ****************************************************** 
	Private _AllowChar As String = ""
	<System.ComponentModel.Description("セットした文字列のみ入力可能とする"), _
	 System.ComponentModel.DefaultValue("")> _
	Public Property AllowChar() As String
		Get
			Return _AllowChar
		End Get
		Set(ByVal value As String)
			_AllowChar = value
		End Set
	End Property

	' ****************************************************** 
	' カスタム入力文字列値チェック
	' ****************************************************** 
	Private _CheckValue As String = ""
	<System.ComponentModel.Description("カンマで区切られたそれぞれの文字列以外入力不可とする"), _
	 System.ComponentModel.DefaultValue("")> _
	Public Property CheckValue() As String
		Get
			Return _CheckValue
		End Get
		Set(ByVal value As String)
			_CheckValue = value
		End Set
	End Property

	' ****************************************************** 
	' 対象文字列以外は入力不可にする
	' ****************************************************** 
	Private Sub LboxText_KeyPress(ByVal sender As System.Object, _
	ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress

		If Me._DataType = 1 Or Me._DataType = 2 Then
			If (ControlChars.Back + _
			 "0123456789").IndexOf(e.KeyChar.ToString()) < 0 Then
				e.Handled = True
			Else
				Return
			End If
		End If

		If Me._AllowChar <> "" Then
			If (ControlChars.Back + _
			 Me._AllowChar).IndexOf(e.KeyChar.ToString()) < 0 Then
				e.Handled = True
			End If
		End If

	End Sub

	' ****************************************************** 
	' 全体のチェック
	' ****************************************************** 
	Private Sub LboxText_Validating(ByVal sender As System.Object, _
	ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating

		If Me._CheckType = 1 Then
			If (Me.Text).Trim() = "" Then
				Me.Text = ""
			End If
			If Me.Text = "" Then
				MessageBox.Show("必須入力です   ", _
				"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
				e.Cancel = True
			End If
		End If

		If Me._DataType = 1 Then
			If Me.Text <> "" Then
				Try
					Dim dt As Date = Date.Parse(Me.Text)
				Catch ex As Exception
					MessageBox.Show("日付を正しく入力して下さい   ", _
				   "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
					Me.SelectAll()
					e.Cancel = True
				End Try
			End If
		End If

		If Me.Text <> "" And Me._CheckValue <> "" Then
			Dim delimStr As String = ","
			Dim delimiter As Char() = delimStr.ToCharArray()
			Dim split As String() = Me._CheckValue.Split(delimiter)
			Dim strValue As String
			Dim flg As Boolean = False

			For Each strValue In split
				If Me.Text = strValue Then
					flg = True
				End If
			Next
			If Not flg Then
				MessageBox.Show("入力された値は正しくありません   ", _
				"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
				Me.SelectAll()
				e.Cancel = True
			End If

		End If

	End Sub

End Class
  


LboxTextProp.vb
  
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Drawing

Partial Class LboxText

	' ****************************************************** 
	' 背景色 
	' ****************************************************** 
	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property BackColor() As Color
		Get
			Return MyBase.BackColor
		End Get
		Set(ByVal value As Color)
			MyBase.BackColor = value
		End Set
	End Property

	' ****************************************************** 
	' フォント 
	' ****************************************************** 
	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property Font() As Font
		Get
			Return MyBase.Font
		End Get
		Set(ByVal value As Font)
			MyBase.Font = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property AllowDrop() As Boolean
		Get
			Return MyBase.AllowDrop
		End Get
		Set(ByVal value As Boolean)
			MyBase.AllowDrop = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property Anchor() As AnchorStyles
		Get
			Return MyBase.Anchor
		End Get
		Set(ByVal value As AnchorStyles)
			MyBase.Anchor = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property AutoScrollOffset() As Point
		Get
			Return MyBase.AutoScrollOffset
		End Get
		Set(ByVal value As Point)
			MyBase.AutoScrollOffset = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property AutoSize() As Boolean
		Get
			Return MyBase.AutoSize
		End Get
		Set(ByVal value As Boolean)
			MyBase.AutoSize = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property BackgroundImage() As Image
		Get
			Return MyBase.BackgroundImage
		End Get
		Set(ByVal value As Image)
			MyBase.BackgroundImage = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property BackgroundImageLayout() As ImageLayout
		Get
			Return MyBase.BackgroundImageLayout
		End Get
		Set(ByVal value As ImageLayout)
			MyBase.BackgroundImageLayout = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property BindingContext() As BindingContext
		Get
			Return MyBase.BindingContext
		End Get
		Set(ByVal value As BindingContext)
			MyBase.BindingContext = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Protected Overloads Overrides ReadOnly Property CanRaiseEvents() As Boolean
		Get
			Return MyBase.CanRaiseEvents
		End Get
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property ContextMenu() As ContextMenu
		Get
			Return MyBase.ContextMenu
		End Get
		Set(ByVal value As ContextMenu)
			MyBase.ContextMenu = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property ContextMenuStrip() As ContextMenuStrip
		Get
			Return MyBase.ContextMenuStrip
		End Get
		Set(ByVal value As ContextMenuStrip)
			MyBase.ContextMenuStrip = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Protected Overloads Overrides ReadOnly Property CreateParams() As CreateParams
		Get
			Return MyBase.CreateParams
		End Get
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property Cursor() As Cursor
		Get
			Return MyBase.Cursor
		End Get
		Set(ByVal value As Cursor)
			MyBase.Cursor = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Protected Overloads Overrides Property DoubleBuffered() As Boolean
		Get
			Return MyBase.DoubleBuffered
		End Get
		Set(ByVal value As Boolean)
			MyBase.DoubleBuffered = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property Dock() As DockStyle
		Get
			Return MyBase.Dock
		End Get
		Set(ByVal value As DockStyle)
			MyBase.Dock = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property ForeColor() As Color
		Get
			Return MyBase.ForeColor
		End Get
		Set(ByVal value As Color)
			MyBase.ForeColor = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property MaximumSize() As Size
		Get
			Return MyBase.MaximumSize
		End Get
		Set(ByVal value As Size)
			MyBase.MaximumSize = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property Site() As ISite
		Get
			Return MyBase.Site
		End Get
		Set(ByVal value As ISite)
			MyBase.Site = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property ShortcutsEnabled() As Boolean
		Get
			Return MyBase.ShortcutsEnabled
		End Get
		Set(ByVal value As Boolean)
			MyBase.ShortcutsEnabled = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property SelectionLength() As Integer
		Get
			Return MyBase.SelectionLength
		End Get
		Set(ByVal value As Integer)
			MyBase.SelectionLength = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property SelectedText() As String
		Get
			Return MyBase.SelectedText
		End Get
		Set(ByVal value As String)
			MyBase.SelectedText = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property Multiline() As Boolean
		Get
			Return MyBase.Multiline
		End Get
		Set(ByVal value As Boolean)
			MyBase.Multiline = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property RightToLeft() As RightToLeft
		Get
			Return MyBase.RightToLeft
		End Get
		Set(ByVal value As RightToLeft)
			MyBase.RightToLeft = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Overloads Overrides Property MinimumSize() As Size
		Get
			Return MyBase.MinimumSize
		End Get
		Set(ByVal value As Size)
			MyBase.MinimumSize = value
		End Set
	End Property


	' ****************************************************** 
	' Shadows Property 
	' ****************************************************** 
	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property AutoCompleteMode() As AutoCompleteMode
		Get
			Return MyBase.AutoCompleteMode
		End Get
		Set(ByVal value As AutoCompleteMode)
			MyBase.AutoCompleteMode = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property AutoCompleteSource() As AutoCompleteSource
		Get
			Return MyBase.AutoCompleteSource
		End Get
		Set(ByVal value As AutoCompleteSource)
			MyBase.AutoCompleteSource = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property AutoCompleteCustomSource() As AutoCompleteStringCollection
		Get
			Return MyBase.AutoCompleteCustomSource
		End Get
		Set(ByVal value As AutoCompleteStringCollection)
			MyBase.AutoCompleteCustomSource = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property TabStop() As Boolean
		Get
			Return MyBase.TabStop
		End Get
		Set(ByVal value As Boolean)
			MyBase.TabStop = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property Margin() As Padding
		Get
			Return MyBase.Margin
		End Get
		Set(ByVal value As Padding)
			MyBase.Margin = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property UseSystemPasswordChar() As Boolean
		Get
			Return MyBase.UseSystemPasswordChar
		End Get
		Set(ByVal value As Boolean)
			MyBase.UseSystemPasswordChar = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property WordWrap() As Boolean
		Get
			Return MyBase.WordWrap
		End Get
		Set(ByVal value As Boolean)
			MyBase.WordWrap = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property UseWaitCursor() As Boolean
		Get
			Return MyBase.UseWaitCursor
		End Get
		Set(ByVal value As Boolean)
			MyBase.UseWaitCursor = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property ScrollBars() As ScrollBars
		Get
			Return MyBase.ScrollBars
		End Get
		Set(ByVal value As ScrollBars)
			MyBase.ScrollBars = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property Lines() As String()
		Get
			Return MyBase.Lines
		End Get
		Set(ByVal value As String())
			MyBase.Lines = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property HideSelection() As Boolean
		Get
			Return MyBase.HideSelection
		End Get
		Set(ByVal value As Boolean)
			MyBase.HideSelection = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property CharacterCasing() As CharacterCasing
		Get
			Return MyBase.CharacterCasing
		End Get
		Set(ByVal value As CharacterCasing)
			MyBase.CharacterCasing = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows ReadOnly Property DataBindings() As ControlBindingsCollection
		Get
			Return MyBase.DataBindings
		End Get
	End Property


	' ****************************************************** 
	' 外側罫線
	' ****************************************************** 
	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property BorderStyle() As BorderStyle
		Get
			Return MyBase.BorderStyle
		End Get
		Set(ByVal value As BorderStyle)
			MyBase.BorderStyle = value
		End Set
	End Property

	' ****************************************************** 
	' ユーザー補助 
	' ****************************************************** 
	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property AccessibleDescription() As String
		Get
			Return MyBase.AccessibleDescription
		End Get
		Set(ByVal value As String)
			MyBase.AccessibleDescription = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property AccessibleName() As String
		Get
			Return MyBase.AccessibleName
		End Get
		Set(ByVal value As String)
			MyBase.AccessibleName = value
		End Set
	End Property

	<System.ComponentModel.Browsable(False)> _
	Public Shadows Property AccessibleRole() As AccessibleRole
		Get
			Return MyBase.AccessibleRole
		End Get
		Set(ByVal value As AccessibleRole)
			MyBase.AccessibleRole = value
		End Set
	End Property

End Class
  



  LboxText.Designer.vb

  
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class LboxText
	Inherits System.Windows.Forms.TextBox

	'UserControl はコンポーネント一覧をクリーンアップするために dispose をオーバーライドします。
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing AndAlso components IsNot Nothing Then
			components.Dispose()
		End If
		MyBase.Dispose(disposing)
	End Sub

	'Windows フォーム デザイナで必要です。
	Private components As System.ComponentModel.IContainer

	'メモ: 以下のプロシージャは Windows フォーム デザイナで必要です。
	'Windows フォーム デザイナを使用して変更できます。  
	'コード エディタを使って変更しないでください。
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		components = New System.ComponentModel.Container()
	End Sub

End Class
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加




SQLの窓  天気  IT用語辞典
Yahoo!ニュース  マルチ辞書
PHP マニュアル  Google URL短縮 


フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ