cellの移動で問題が発生しています
- cellをEnterで横に移動する際に、CurrentCellの値を変更すると、真下のcellに移動してしまう現象が発生しています。
- DataGridViewのKeyDownイベントを使用してEnterキーの押下を検知し、移動処理を行っていますが、原因が分かりません。
- 問題の解決のために、DataGridViewのKeyDownイベントの記述内容を見直し、移動先のcellを正しく指定する必要があります。
- ベストアンサー
cellをenterで横移動
DataGridViewのcellをEnterキーで横移動しています。Enterキーのみ続けて押している場合は、横へ動くのですが、CurrentCellの値を変更しEnterキーを押した場合、新規行のcellに値を入力しEnterキーを押した場合、横ではなく真下のcellへ移動してしまいます。 原因がわかりません。 Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown Try If (e.KeyCode = Keys.Enter) Then If (DataGridView1.CurrentCell.ColumnIndex <> DataGridView1.ColumnCount - 1) Then DataGridView1.CurrentCell = DataGridView1(DataGridView1.CurrentCell.ColumnIndex + 1, DataGridView1.CurrentCell.RowIndex) e.Handled = True ElseIf (DataGridView1.CurrentCell.RowIndex <> DataGridView1.RowCount - 1) Then DataGridView1.CurrentCell = DataGridView1(1, DataGridView1.CurrentCell.RowIndex) Else DataGridView1.CurrentCell = DataGridView1(1, 0) e.Handled = True End If End If Catch ex As Exception MessageBox.Show(ex.Message) End Try
- Visual Basic
- 回答数1
- ありがとう数0
- みんなの回答 (1)
- 専門家の回答
質問者が選んだベストアンサー
お世話になります。 DataGridView を継承して、拡張しましょう。 ProcessDataGridViewKey 等をオーバーライドします。 [[VS2005] DataGridViewのCell移動をEnterで] http://www.vbstation.net/tips/datagridviewenter.htm
関連するQ&A
- DataGridViewのセルをenterキーで横移動したいです。
VB.NET2005にてDataGridviewのセルを横移動させたいんです。 いろんな物を参考に以下のプログラムを何とかプログラムを組んでみましたが、ビルド時セル(0,0)→セル(0,1)→セル(0,2)→セル(0,3)・・・と1行目がenterキーで移動できず、2行目へ下がってしまいます。2行目からは横移動できるようになりました。 1週間近く試行錯誤しましたが、うまく動きません。 訂正箇所を教えていただけないでしょうか? よろしくお願いします。 いまの行と列の取得 Dim currentRow As Integer = Me.DataGridView1.CurrentRow.Index Dim currentColumn As Integer = Me.DataGridView1.CurrentCell.ColumnIndex Dim nextColumn As Integer = currentColumn + 1 Dim nextRow As Integer = currentRow '押したキーがENTERだったら Dim t As Object t = e.KeyCode() If t = 13 Then If nextColumn = 6 Then '列数が最終列を超えたとき nextColumn = 0 '最初の列数へ Else nextRow -= 1 End If MessageBox.Show("nextColumn =" & nextColumn & "nextRow = " & nextRow) Try Me.DataGridView1.CurrentCell = Me.DataGridView1(nextColumn, nextRow) Catch If Not (Me.DataGridView1.CurrentCell Is Nothing) Then nextColumn = 0 nextRow = 0 Me.DataGridView1.CurrentCell = Me.DataGridView1(nextColumn, nextRow) Exit Sub Else Me.DataGridView1.CurrentCell = Me.DataGridView1(nextColumn, nextRow) Exit Sub End If End Try End If
- ベストアンサー
- Visual Basic
- データグリッドビュー数字と小数点は入力可能
VB2010にて、データグリッドビューを編集不可にしています。 数字と小数点は入力可能にしたいのですが、 下記のコードを実行すると、 1キーを押すと49と表示されます。 どうしたら、キー値を表示できるようになるのでしょうか? ご存知の方教えてください。よろしくお願いします。 Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown '数字と小数点は入力可能 If e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9 Or e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9 Or e.KeyCode = Keys.Decimal Or e.KeyCode = 190 Then e.Handled = True strTemp = Me.DataGridView1(Me.DataGridView1.SelectedCells(0).ColumnIndex, Me.DataGridView1.SelectedCells(0).RowIndex).Value strTemp = strTemp & e.KeyValue Me.DataGridView1(Me.DataGridView1.SelectedCells(0).ColumnIndex, Me.DataGridView1.SelectedCells(0).RowIndex).Value = strTemp End If 'バックスペース機能追加 If e.KeyCode >= Keys.Back Then e.Handled = True strTemp = Me.DataGridView1(Me.DataGridView1.SelectedCells(0).ColumnIndex, Me.DataGridView1.SelectedCells(0).RowIndex).Value If strTemp = "" Then Exit Sub strTemp = Microsoft.VisualBasic.Left(strTemp, Len(strTemp) - 1) Me.DataGridView1(Me.DataGridView1.SelectedCells(0).ColumnIndex, Me.DataGridView1.SelectedCells(0).RowIndex).Value = strTemp End If If e.Control And e.KeyCode = Keys.V Then 'Ctrl+V押下時は貼付 DGV.Paste(DataGridView1) ElseIf e.KeyCode = Keys.Delete Then 'Delete押下時は削除 DGV.Delete(DataGridView1) End If End Sub
- ベストアンサー
- Visual Basic
- イベント内で別イベントを呼ぶ方法
早速質問させていただきます。 Form上にDataGridView(dgv)があります。 dgvのセルをマウスでダブルクリックした時に処理を入れております。 Private Sub dgv_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellDoubleClick If e.RowIndex = -1 Then Exit Sub End If '//処理 End Sub このdgv上でEnterキーを押した際に、ダブルクリックしたのと同じ処理を 行いたいのですが、このような場合、どのような実装をするのが一般的なのでしょうか? 例えば、下記の様にCall dgv_CellDoubleClick(sender, e)をするとEventArgsが合わないためエラーになってしまいます。 適切な方法をご教授お願い致します。 Private Sub dgv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgv.KeyDown If e.KeyCode = Keys.Enter Then Call dgv_CellDoubleClick(sender, e) End If End Sub
- ベストアンサー
- Visual Basic
- データグリッドビュー右クリック選択について
書き方法で右クリックにて セルの選択をしてコンテキストメニューを表示させて cellに追加文字を追加させることをしてます。 Multiselect=false で 選択はfullselectrow です。 データバインドでdatatableをバインドさせてます。 .CurrentRowが前のままになってしまって データがずれてしまいます。 どのようにすれば currentrowがきちんと取得できるでしょうか? Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown If e.Button = Windows.Forms.MouseButtons.Right Then DataGridView1.ClearSelection() If e.RowIndex < 0 Then Exit Sub DataGridView1.Rows(e.RowIndex).Selected = True End If End Sub
- ベストアンサー
- Visual Basic
- VB2010 DataGridView Enter
現在、表を使ったプログラムを作成して居ます。 DataGridViewでデーターをセルに入れると、下のセルにフォーカスが移ってしまいます。 次に入れるデーターはどこに成るかはユーザーが決定したいので、出来れば入力したセルからフォーカスを移動させたく無いのですが、何か方法はあるでしょうか。 下記の様に、入力された時、右にフォーカスさせる方法は見つけましたが、フォーカスが移らない方法がありましたらお教え下さい。 宜しくお願い致します。 Public Class DataGridViewEx Inherits DataGridView <System.Security.Permissions.UIPermission( _ System.Security.Permissions.SecurityAction.Demand, _ Window:=System.Security.Permissions.UIPermissionWindow.AllWindows)> _ Protected Overrides Function ProcessDialogKey( _ ByVal keyData As Keys) As Boolean 'Enterキーが押された時は、Tabキーが押されたようにする If (keyData And Keys.KeyCode) = Keys.Enter Then Return Me.ProcessTabKey(keyData) End If Return MyBase.ProcessDialogKey(keyData) End Function <System.Security.Permissions.SecurityPermission( _ System.Security.Permissions.SecurityAction.Demand, _ Flags:=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)> _ Protected Overrides Function ProcessDataGridViewKey( _ ByVal e As KeyEventArgs) As Boolean 'Enterキーが押された時は、Tabキーが押されたようにする If e.KeyCode = Keys.Enter Then Return Me.ProcessTabKey(e.KeyCode) End If Return MyBase.ProcessDataGridViewKey(e) End Function End Class
- ベストアンサー
- Visual Basic
- DataGridViewの特定のセルで選択状態を保ちたい VB2005
DataGridViewの件で質問したいのですが。 よろしくお願いします。 VB2005で、DataGridViewの特定のセルでカーソルがとどまる(選択状態)ようにしたいのですが 思ったように動いてくれません。例えば、下のようなDataGtridViewがあったとして, 例 ------------------ |ID | X | Y | ------------------ |001| 100 | | ------------------ |002| 150 | | ------------------ |003| 105 | | ------------------ ID が002の行で、Y列のセルにカーソルが入りXと違う数を入力したら、メッセージを 表示させて、そのセルにカーソルがとどまる処理を作りたいのですが。下の行のセルに 移動してしまいます。 DataGridView_CellEndEditイベントやDataGridView1_CellValueChangedなどで カーソルがとどまる処理をしているつもりなのです下の行に移動してしまいます。 DataGridViewの初期設定の問題でしょうか? Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged Dim DgvRow As Integer '行をセット DgvRow = e.RowIndex ' X と Yが異なれば、メッセージ表示 If DataGridView1("X", e.RowIndex).Value <> DataGridView1("Y", e.RowIndex).Value Then MsgBox("X <> Y", MsgBoxStyle.OkOnly) Me.DataGridView1.CurrentCell = Me.DataGridView1("Y", DgvRow) ' これも駄目だった ↓ 'Me.DataGridView1.CurrentCell = Me.DataGridView1("Y", e.RowIndex - 1) End If End Sub アドバイスいただけたら幸いです。よろしくお願い致します。 開発環境 VB2005 Visual Studio 2005 Standard Edition(SP1) Windows XP Pro SP2
- ベストアンサー
- Visual Basic
- 1回だけ変数の宣言や初期化をする方法は?
VisualStudio2010を使いC#で以下のコードを書いてます。 bool flag = true; //※ private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { Rectangle rect = new Rectangle(); //※ if (e.RowIndex < 0 || e.ColumnIndex < 0) return; if (e.ColumnIndex == 0) { DataGridViewCell Cell = dataGridView1[e.ColumnIndex, e.RowIndex]; if (flag) { rect = e.CellBounds; flag = false; } else { rect.Height += Cell.Size.Height; } } } CellPaintingのような繰り返される処理でflagやrect(※印)のような変数をCellPaintingのブロック内でかつ、一回だけ変数の宣言や初期化を行う方法はありますか? ちなみに、このコードは最初のセルの位置とその他のセル高さの合計をrectに設定しようとしてますが、これ自体にはあまり意味はありません。 何卒、よろしくお願いします。
- ベストアンサー
- その他(プログラミング・開発)
- エクセルBVAのフォーム式のKeyCode~の意味がわかりません
エクセルBVAです Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) If KeyCode = vbKeyDown Then KeyCode = 0 End If End Sub ヘルプをみると キーを押したとき、およびキーを離したときに続いて発生します。KeyDown イベントはキーを押すと発生し、KeyUp イベントはキーを離すと発生します。 キーってenterのことですかね? なら、 If KeyCode = vbKeyDown Then KeyCode = 0 End If で、KeyCodeを0ににするってどういうことでしょうか?
- ベストアンサー
- オフィス系ソフト
- Ctrl+vのイベントを解除
VB2010で、データグリッドビューのグリッドにペーストさせたくありません。 Ctrl+vのイベントを解除させる方法はないでしょうか? ご存知の方おしえてください。よろしくお願いします。 Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown If e.Control And e.KeyCode = Keys.V Then 'ココにイベント解除を入れたいです End If End Sub
- ベストアンサー
- Visual Basic
- TextBoxでEnterキーを押すと、にあるButtonを押したのと同じ効果を出す方法
VB初心者です。 まだ、はじめたばかりなので、上手く質問を説明できませんが、よろしくお願いします。 環境はVisualBasic .net 2003です。 フォームにテキストボックス(TextBox)とボタン(Button)があります。 テキストボックス内でEnterキーを押すと、ボタン(Button)を押したのと同様の効果をだしたいのですが。 VC++6.0だと、SendMessage(WM_COMMAND , ..... のようなやり方でできたはずですが・・・ --------------------------- テキストボックスのコード Private Sub txtTest_KeyDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTest.KeyDown If (e.KeyCode = Keys.Enter) Then '★★★ここでどういうコードを書いてよいかわかりません。 End If End Sub ---------------------------- ボタンを押したときのイベントハンドラ Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click ボタンを押したら、このイベントハンドラを呼び出したい。 もしくは、間接的に呼び出したい。 ------- どなたか、やり方をご存知の方がいらっしゃいましたら、よろしくお願いします。
- ベストアンサー
- Visual Basic
補足
ありがとうございます。 早速、コピーしてclassを2個作ってみましたが、やはり同じ現象で動きません。 私は、何か足りないのか?何かが間違っているのかよく分からないんです。 他にclassを作る以外にする事はありますか?