- 題名: DataGridViewのコンボボックスにユーザーが文字列を入力した時のカーソル移動
- 日時: 2010/06/07 10:56:15
- ID: 26909
- この記事の返信元:
- (なし)
- この記事への返信:
- [26914] Re[1]: DataGridViewのコンボボックスにユーザーが文字列を入力した時のカーソル移動2010/06/08 4:04:41
- ツリーを表示
管理人様
ご回答ありがとうございます。
管理人様のヒントを元に、ヘルプなどを参照し、
自分なりに実装出来ましたので、ソースコードを提示いたします。
クラスを三個つくって、DataGridViewに自作の列のクラスを追加するように
しました。
この度は、誠にありがとうございました。
------ 以下、ソースコード ---------
'データグリッドビューに列を追加する処理
With DataGridView1
:
:
Dim col As DataGridViewColumn
col = New MitumoriConboBoxColumn
col.Name = "品名"
col.DataPropertyName = "品名"
.Columns.Add(col)
:
:
End With
'見積の品名を入力するコンボボックス列のクラス
Public Class MitumoriConboBoxColumn
Inherits DataGridViewComboBoxColumn
Sub New()
CellTemplate = New MitumoriComboboxCell()
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
If (value IsNot Nothing) AndAlso _
Not value.GetType().IsAssignableFrom(GetType(MitumoriComboboxCell)) _
Then
Throw New InvalidCastException("Must be a MitumoriComboboxCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
'品名を入力するコンボボックス列のセルのクラス
Public Class MitumoriComboboxCell
Inherits DataGridViewComboBoxCell
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _
ByVal initialFormattedValue As Object, _
ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
Dim ctl As MutumoriComboEditControl = _
CType(DataGridView.EditingControl, MutumoriComboEditControl)
ctl.Text = Me.Value.ToString()
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
Return GetType(MutumoriComboEditControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
Return GetType(String)
End Get
End Property
End Class
'品名列のエディットコントロールのクラス
Public Class MutumoriComboEditControl
Inherits DataGridViewComboBoxEditingControl
Implements IDataGridViewEditingControl
Public Overrides Function EditingControlWantsInputKey(ByVal key As Keys, _
ByVal dataGridViewWantsInputKey As Boolean) As Boolean
Select Case key And Keys.KeyCode
Case Keys.Left '左矢印キー
If SelectionStart = 0 Then '編集中テキストの先頭にカーソルがあるなら
Return False '隣のセルにカーソルを移動する
Else
Return True
End If
Case Keys.Right '右矢印キー
If Text = "" Then 'テキストが入力されていないなら
Return False '隣のセルにカーソルを移動する
Else
If Text.Length = SelectionStart Then '編集中テキストの末尾にカーソルがあるなら
Return False '隣のセルにカーソルを移動する
Else
Return True
End If
End If
Case Else
Return Not dataGridViewWantsInputKey
End Select
End Function
End Class
分類:[.NET]
いつもお世話になっています。
早速ですが、質問させて下さい。
こちらのサイトで紹介されています、
「DataGridViewのコンボボックスにユーザーが文字列を入力できるようにする」
http://dobon.net/vb/dotnet/datagridview/comboboxdropdownstyle.html
の記事を参考にさせていただきプログラムを作成しました。
そのプログラムは見積の明細部分を入力するもので、
番号 品名 数量 単位 単価 金額 の列が有ります。
品名の列がコンボボックスで、頻繁に出てくる品名をコンボボックスで選択し、
また、任意の品名をユーザーが手入力出来ると言うものです。
大体の目的は達成出来たのですが、ちょっと気になる部分がございます。
品名のコンボボックス列の文字列を編集したいとき、キーボードの左右矢印キーで
テキスト部分の入力カーソルを左右に移動させようとすると、隣の列にカーソルが移動してしまい、文字単位でのカーソル移動ができません。
Googleでいろいろと探したのですが解決策が見つからず、質問させていただいた次第です。
フォーム上に単独でコンボボックスがある場合には出来るようなのですが、
DataGridView上のコンボボックスではどうしても隣の列に移動してしまいます。
お手数ですが、解決策をご存知でしたらご教示願えないでしょうか。
よろしくお願いいたします。