.NET フォームを閉じるときにValidatingイベントを走らせないようにするには

Validatingイベントは
フォームの×ボタンをクリックしフォームを閉じようとしたり、
CausesValidatin=Falseを設定しているコんトロールであっても、
そのコントロールの処理内でMe.Closeを記載していたりすると、
Validatingイベントが走ってしまいます。

フォームを閉じるときにValidatingイベントを走らせないようにするには
Public Class Form1
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Const WM_CLOSE As Integer = &H10
Const WM_SYSCOMMAND As Integer = &H112
Const SC_CLOSE As Integer = &HF060

Select Case m.Msg
Case WM_SYSCOMMAND
If m.WParam.ToInt32() = SC_CLOSE Then
'Xボタン、コントロールメニューの「閉じる」、
'コントロールボックスのダブルクリック、
'Atl+F4などにより閉じられようとしている
'このときValidatingイベントを発生させない。
Me.AutoValidate = Windows.Forms.AutoValidate.Disable

End If
Case WM_CLOSE
'Application.Exit以外で閉じられようとしている
'このときValidatingイベントを発生させない。
Me.AutoValidate = Windows.Forms.AutoValidate.Disable
End Select

MyBase.WndProc(m)
End Sub
End Class

GrapeCityTool MultiRow5.0J 基本

セルに値を設定するには


Dim iCurRow As Integer = Me.GcMultiRowSheet.CurrentRow.Index
Dim value As Object = "XXX"
Me.GcMultiRowSheet.SetValue(iCurRow,"textBoxCell1", value )
※Itemプロパティを使用して値を設定するとパフォーマンスが悪い。
Me.GcMultiRowSheet.Item(iCurRow,"textBoxCell1").Value = value

セルの値を取得するには


Dim iCurRow As Integer = Me.GcMultiRowSheet.CurrentRow.Index
Dim value As Object = Me.GcMultiRowSheet.GetValue(iCurRow,"textBoxCell1")
※Itemプロパティを使用して値を設定するとパフォーマンスが悪い。
Dim value As Object = Me.GcMultiRowSheet.Item(iCurRow,"textBoxCell1").Value


ボタンセルの背景色を変更するには


ボタンセルのFlatStyleをSystem以外に設定する。
注意
FlatStyleをSystem以外に設定した場合、SelectionBackColor(選択色)をTransparentに設定しないと
ボタンセルにフォーカスした際に背景色が選択色(デフォルト紺)になってしまいます。


セルのイベント発生順序


1.以下の操作で発生するイベント
Enterキーで編集状態にする
セルに入力
Enterキーで編集状態を終了する
Tabキーでセル移動

CellEnter
CellBeginEdit
CellEditingControlShowing
CellParsing
CellValueChanged ※セルの値が変更された場合のみ発生する。
CellEndEdit
CellLeave
CellValidating
CellValidated

2.以下の操作で発生するイベント
マウスダブルクリックで編集状態にする
セルに入力
マウスクリックでセル移動
CellEnter
CellBeginEdit
CellEditingControlShowing
CellLeave
CellValidating
CellParsing
CellValueChanged ※セルの値が変更された場合のみ発生する。
CellValidated
CellEndEdit

CellValueChanged以降のイベントでセルの入力値をGetValueで取得できます。


セルの値を検証するには


CellValidatingイベントを使用します。
入力値はe.FormattedValueを使用します。
※上記のイベント発生順序の2番ではGetValueメソッドを使用して入力値を取得できません。
入力値が異常でセルを移動させたくない場合はe.CancelプロパティにTrueを設定します。
注意
WindowsControlのValidating同様にフォームの×ボタンやMe.CloseでCellValidatingイベントが発生します。
フォームを閉じるときにValidatingイベントを走らせないようにするにはなどの対策が必要です。

.NET ストアドファンクションをキックするには?

ストアドプロシージャではなくストアドファンクションをキックする方法です。

Using cnn As DbConnection = 略
Using cmd As DbCommand = 略
cmd.Connection = cnn 
cmd.CommandType = CommandType.Text
cmd.CommandText = "BEGIN :nReturnValue := FuncAAA.FuncAAA(:nCd); END;"

Dim pRet As DbParameter = 略
pRet.ParameterName = "nReturnValue"
pRet.Direction = ParameterDirection.ReturnValue
pRet.DbType = DbType.Int32
cmd.Parameters.Add(pRet)

Dim pCd As DbParameter = 略
pCd .ParameterName = "nCd"
pCd .Value = 123
pCd .Direction = ParameterDirection.Input
pCd .DbType = DbType.Int32
cmd.Parameters.Add(pCd )

cmd.ExecuteNonQuery()

Console.WriteLine(pRet.Value.ToString)
End Using
End Using

.NET DateTimePickerで1桁入力しTabキーで移動すると入力値が取得できない

DateTimePickerで日付などを1桁入力し、Tabキーで移動するとDateTimePickerのValidatingイベント、Validatedイベントで入力値を取得することができません。

VS2005、VS2008で確認しました。
このような画面でDateTimePickerの日付部分に1桁「1」を入力し、Tabキーでコントロールを移動します。


Formには以下のようなコードを書きました。
Public Class Form1

Private Sub DateTimePicker1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Leave
Console.WriteLine("Leave:" & Me.DateTimePicker1.Value.ToString)
End Sub

Private Sub DateTimePicker1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Validated
Console.WriteLine("Validated:" & Me.DateTimePicker1.Value.ToString)
End Sub

Private Sub DateTimePicker1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles DateTimePicker1.Validating
Console.WriteLine("Validating:" & Me.DateTimePicker1.Value.ToString)
End Sub

Private Sub DateTimePicker1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Console.WriteLine("ValueChanged:" & Me.DateTimePicker1.Value.ToString)
End Sub

End Class


実行結果は以下のようになりました。
Leave:2008/10/02 8:54:24
Validating:2008/10/02 8:54:24
Validated:2008/10/02 8:54:24
ValueChanged:2008/10/01 8:54:24

これは年月日のいずれでも1桁入力し、Tabキーで移動した場合に発生します。

回避方法はLeaveイベントでMe.ActiveControl = Me.ActiveControlを記述します。
Public Class Form1

Private Sub DateTimePicker1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Leave
     '1桁入力しTabキーで移動した際に入力値が取得できない問題を回避します。
     Me.ActiveControl = Me.ActiveControl
Console.WriteLine("Leave:" & Me.DateTimePicker1.Value.ToString)
End Sub

Private Sub DateTimePicker1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.Validated
Console.WriteLine("Validated:" & Me.DateTimePicker1.Value.ToString)
End Sub

Private Sub DateTimePicker1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles DateTimePicker1.Validating
Console.WriteLine("Validating:" & Me.DateTimePicker1.Value.ToString)
End Sub

Private Sub DateTimePicker1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
Console.WriteLine("ValueChanged:" & Me.DateTimePicker1.Value.ToString)
End Sub

End Class


実行結果は以下のようになりました。
ValueChanged:2008/10/01 9:12:40
Leave:2008/10/01 9:12:40
Validating:2008/10/01 9:12:40
Validated:2008/10/01 9:12:40

DateTimePickerを継承したカスタムコントロールを作成し、Leaveイベントで以下のようにしておくと便利かと思います。
Public Class CustomControl1
Inherits System.Windows.Forms.DateTimePicker

Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
Me.FindForm.ActiveControl = Me.FindForm.ActiveControl
MyBase.OnLeave(e)
End Sub
End Class

.NET カスタム構成セクションを作成する

方法 : ConfigurationSection を使用してカスタム構成セクションを作成する


アプリケーション構成ファイルに以下のようなカスタム構成セクションを定義し読み込みたいと思います。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<section name="customConfigSection"  type="CustomConfigSample.CustomConfig.CustomConfigSection, CustomConfigSample" />
</configSections>


<customConfigSection>
<childElemnts>
<add key="key1" value1="Value1_1" value2="Value2_1" />
<add key="key2" value1="Value1_2" value2="Value2_2" />
<add key="key3" value1="Value1_3"  />
</childElemnts >
</customConfigSection >

</system.diagnostics>
</configuration>


1、Windowsアプリケーション「CustomConfigSample」を作成します。

2、参照設定でSystem.configurationの参照を追加します。

3、カスタム構成セクションクラスを作成します。
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml

Namespace CustomConfig

''' <summary>
''' カスタムセクション
''' </summary>
''' <remarks></remarks>
Public Class CustomConfigSection
Inherits ConfigurationSection

''' <summary>
''' コンストラクタ
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub

''' <summary>
''' 子要素を表すコレクションです。
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("childElemnts")> _
Public Property ChildElemnts() As CustomConfigElementCollection
Get
Return CType(Me("childElemnts"), CustomConfigElementCollection)
End Get
Set(ByVal value As CustomConfigElementCollection)
Me("childElemnts") = CType(value, CustomConfigElementCollection)
End Set
End Property
End Class


End Namespace


3、カスタム構成セクションの子要素クラスを作成します。
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml

Namespace CustomConfig

''' <summary>
''' カスタムセクション子要素を定義するクラスです。
''' </summary>
''' <remarks></remarks>
Public Class CustomConfigElement
Inherits ConfigurationElement

''' <summary>
''' コンストラクタ
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub

''' <summary>
''' キー
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("key", IsRequired:=True)> _
Public Property Key() As String
Get
Return CStr(Me("key"))
End Get
Set(ByVal value As String)
Me("key") = value
End Set
End Property

''' <summary>
''' 値1
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("value1", IsRequired:=True)> _
Public Property Value1() As String
Get
Return CStr(Me("value1"))
End Get
Set(ByVal value As String)
Me("value1") = value
End Set
End Property

''' <summary>
''' 値2
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
<ConfigurationProperty("value2", IsRequired:=False)> _
Public Property Value2() As String
Get
Return CStr(Me("value2"))
End Get
Set(ByVal value As String)
Me("value2") = value
End Set
End Property

End Class
End Namespace


4、カスタム構成セクションの子要素のコレクションクラスを作成します。
Imports System
Imports System.Collections
Imports System.Text
Imports System.Configuration
Imports System.Xml

Namespace CustomConfig

''' <summary>
''' CustomConfigElementのコレクションを扱うためのクラスです。
''' </summary>
''' <remarks></remarks>
Public Class CustomConfigElementCollection
Inherits ConfigurationElementCollection

''' <summary>
''' コンストラクタ
''' </summary>
''' <remarks></remarks>
Public Sub New()
End Sub

''' <summary>
''' 指定したインデックスのCustomConfigElementを取得します。
''' </summary>
''' <param name="index"></param>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Default Public Shadows Property Item(ByVal index As Integer) As CustomConfigElement
Get
Return CType(BaseGet(index), CustomConfigElement)
End Get
Set(ByVal value As CustomConfigElement)
If Not (BaseGet(index) Is Nothing) Then
BaseRemoveAt(index)
End If
BaseAdd(index, value)
End Set
End Property

''' <summary>
''' 指定した名称のCustomConfigElementを取得します。
''' </summary>
''' <param name="Name"></param>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Default Public Shadows ReadOnly Property Item(ByVal Name As String) As CustomConfigElement
Get
Return CType(BaseGet(Name), CustomConfigElement)
End Get
End Property

''' <summary>
''' 新しい ConfigurationElement を作成します。  
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Protected Overloads Overrides Function CreateNewElement() As ConfigurationElement
Return New CustomConfigElement()
End Function

''' <summary>
''' 指定したConfigurationElementを特定するキーを取得します。  
''' </summary>
''' <param name="element"></param>
''' <returns></returns>
''' <remarks></remarks>
Protected Overrides Function GetElementKey(ByVal element As ConfigurationElement) As Object
Return CType(element, CustomConfigElement).Key
End Function

End Class
End Namespace


5、アプリケーション構成ファイルのカスタムセクションをアプリケーションから読み込みます。
Public Class Main


Public Shared Sub Main()

Dim section As CustomConfig.CustomConfigSection
section = CType(System.Configuration.ConfigurationManager.GetSection("customConfigSection"), CustomConfig.CustomConfigSection)

Console.WriteLine("customConfigSectionの要素数={0}", section.ChildElemnts.Count)

Console.WriteLine("customConfigSectionの0番目要素のKey={0}", section.ChildElemnts.Item(0).Key)

Console.WriteLine("customConfigSectionの[key2]要素のValue1={0}", section.ChildElemnts.Item("key2").Value1)

End Sub

End Class

.NET VisualStudioからAccessを操作する際の注意点

VisualSutdioからAccessを操作する場合、知らないと色々とハマることが多いです。

1.DbParameterでAccessの時間を含む日付型フィールドのパラメータを作成できない

2.SQLのJOIN句には括弧が必要。
SELECT * FROM ((TABLE1
INNER JOIN TABLE2 ON TABLE1.CD = TABLE2.CD)
INNER JOIN TABLE3 ON TABLE1.CD = TABLE3.CD)

3.名前付きパラメータであっても、設定する順番が重要。疑問符(?)で指定する位置指定パラメータが適用される。
Dim sql As String = "SELECT * FROM TABLE1 WHERE CD=@CD AND NAME=@NAME"
Dim cmd AS DbCommand = 略
cmd.CommandText = sql

Dim prm1 As DbParameter = cmd.CreateParameter
prm1.ParameterName = "CD"

Dim prm2 As DbParameter = cmd.CreateParameter
prm2.ParameterName ="NAME"

'Sqlのパラメータの順序通りに追加する必要がある。
cmd.Parameters.Add(prm1)
cmd.Parameters.Add(prm2)
’順番を変えるとエラーになる。
cmd.Parameters.Add(prm2)
cmd.Parameters.Add(prm1)

.NET DbParameterでAccessの時間を含む日付型フィールドのパラメータを作成できない

Accessデータベースの日付型フィールドを、System.Data.Common.DbParameterで処理する際
DbTypeプロパティの設定に注意する必要があります。

日付型フィールドに時間を含まない場合、
DbParameterのDbTypeプロパティはDbType.Dateで問題なく動作しますが
日付型フィールドに時間を含む場合、
DbParameterのDbTypeプロパティをDbType.DateTimeにすると「OleDbException:抽出条件でデータ型が一致しません。」と例外が発生します。


DbParameterをOleDbParameterにキャストしOleDbType.Dateを指定しなければいけません。

Dim setting As ConnectionStringSettings
setting = ConfigurationManager.ConnectionStrings("MyAccessDb")

Dim factory As DbProviderFactory
factory = DbProviderFactories.GetFactory(setting.ProviderName)

Using cnn As DbConnection = factory.CreateConnection
cnn.ConnectionString = setting.ConnectionString
Using cmd As DbCommand = factory.CreateCommand
cmd.Connection = cnn
cmd.CommandText = "INSERT INTO MyTable (MyDate) Values (@MyDate)"
Dim prm As DbParameter = factory.CreateParameter
prm.ParameterName = "MyDate"
'時間を含む日付パラメータ
prm.DbType = DbType.DateTime
     If TypeOf (prm) Is OleDb.OleDbParameter AndAlso dbtype = Data.DbType.DateTime Then     
CType(prm, OleDb.OleDbParameter).OleDbType = OleDb.OleDbType.Date
End If
prm.Value = Me.DateTimePicker1.Value
cmd.Parameters.Add(prm)

cnn.Open()
Try
cmd.ExecuteNonQuery()
Finally
cnn.Close()
End Try
End Using
End Using