プロが教えるわが家の防犯対策術!

DataGridView内にコンボボックスを設定しております。

マウスのホイールボタンは、DataGridViewの縦スクロールに
使いたいのですが、DataGridView内のコンボボックスに
カーソルがある場合、コンボボックス内のデータが変わってしまいます。

ユーザー側と打ち合わせた結果、コンボボックスにカーソルが
ある時に、マウスのホイールボタンを無効に出来ないかと
言われております。

色々調べてみましたが、私では分からず、教えて下さい。


(サンプルソース)
Dim column1 As New DataGridViewComboBoxColumn()
With DataGridView1
.Columns.Clear()
.Columns.Add("Col01", "列1")

With column1
.Items.AddRange(New String() {"Item01", "Item02", "Item03"})
.Name = "Col02"
.HeaderText = "列2"
End With
.Columns.Add(column1)

For i As Integer = 0 To 100
.Rows.Add()
Next i
End With

A 回答 (2件)

#1です。


その後調べてみたのですが、WndProcをオーバーライドしてマウスホイールメッセージを無効化する方法をやろうとしてみたのですが、適切なクラスが私には分からなかったので別の方法を考えました。

DataGridViewComboBoxが編集状態にあるときにMouseWheelイベントを感知した場合には編集を終了する。という方法を試してみました。
うまくいきそうでしたので下記のコードを参考にしてみてください。
---------------

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim column1 As New DataGridViewComboBoxColumn()
With DataGridView1
.Columns.Clear()
.Columns.Add("Col01", "列1")

With column1
.Items.AddRange(New String() {"Item01", "Item02", "Item03"})
.Name = "Col02"
.HeaderText = "列2"
End With
.Columns.Add(column1)

For i As Integer = 0 To 100
.Rows.Add()
Next i
End With

End Sub

Private dataGridViewComboBox As DataGridViewComboBoxEditingControl = Nothing

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit

If Not (Me.dataGridViewComboBox Is Nothing) Then
RemoveHandler Me.dataGridViewComboBox.MouseWheel, _
AddressOf dataGridViewComboBox_MouseWheel
Me.dataGridViewComboBox = Nothing

End If
End Sub



Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing

If TypeOf e.Control Is DataGridViewComboBoxEditingControl Then
Dim dgv As DataGridView = CType(sender, DataGridView)

If dgv.CurrentCell.OwningColumn.Name = "Col02" Then
Me.dataGridViewComboBox = _
CType(e.Control, DataGridViewComboBoxEditingControl)

AddHandler Me.dataGridViewComboBox.MouseWheel, _
AddressOf dataGridViewComboBox_MouseWheel

End If

End If

End Sub

Private Sub dataGridViewComboBox_MouseWheel(ByVal sender As Object, _
ByVal e As EventArgs)

Me.DataGridView1.EndEdit()

End Sub

End Class
    • good
    • 1
この回答へのお礼

ありがとうございます。
いま、試してみたらうまくいきました。

ありがとうございました。

お礼日時:2013/09/11 10:07

直接の回答ではないのですが、下記の記事&議論が参考になるのではないでしょうか。



http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.ph …
http://www.atmarkit.co.jp/fdotnet/dotnettips/467 …

キーワードは「承継クラス」、「WndProcをオーバーライド」、「WM_MOUSEWHEEL」です。

詳しくは分かりかねますが、Datagridviewcomboboxcell?クラスの承継クラスを作って、WindProcをオーバーライド、そこでWM_MOUSEWHEELメッセージをはじくようにする。というような流れだと思います。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています