Datagrid and Arrow keys, how to make the arrow move around?

J

Joe

Ok Simple problem.

I have a MS datagrid I made my very own multiline
columnstyle for my datagrid. Right away I had problems.
Yea it was multiline (using a regular textbox that shows
up on focus of the custom columnstyle), the problem was
the fact that if you hit enter it would go to the next row.
Now I would expect tab to be used for moving around, but
it appears someone at MS doesn't like tab and likes using
enter and the arrow keys to move to different Cells.

Ok So I need a way to be able to hit enter and it go to
another line in the textbox. I figured out how to do this,
I built a seperate project with a textbox and different
buttons and I'm sending messages to the wndproc of the
textbox. I got all the operations to work in my sample
probject. Though when I went to add them into my datagrid
I did the following. Note: it might not look good here.

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Dim KC As Keys = CType((msg.WParam.ToInt32 And
Keys.KeyCode), Keys)
If (msg.Msg = WM_KEYDOWN Or msg.Msg = WM_KEYUP)
Then
Dim DisableKeys As Boolean
Try
DisableKeys = CType
(Me.GetActiveTableStyle.GridColumnStyles
(Me.CurrentCell.ColumnNumber),
DataGridExt.DataGridExtensions.DataGridMultiLineTextboxColu
mnStyle).IsKeysDisabled
Catch
Return False
End Try
If DisableKeys = True Then
If KC = Keys.Enter Or KC = Keys.Up Or KC =
Keys.Down Then
Dim M As New
System.Windows.Forms.Message
M.HWnd = CType
(Me.GetActiveTableStyle.GridColumnStyles
(Me.CurrentCell.ColumnNumber),
DataGridExt.DataGridExtensions.DataGridMultiLineTextboxColu
mnStyle).Box.Handle
Select Case KC
Case Keys.Enter
M.LParam = IntPtr.Zero
M.Msg = Me.WM_CHAR
M.WParam = IntPtr.op_Explicit
(Keys.Enter)
Case Keys.Up
M.LParam = IntPtr.Zero
M.Msg = Me.WM_KEYDOWN
M.WParam = IntPtr.op_Explicit
(Me.VK_UP)
Case Keys.Down
M.LParam = IntPtr.Zero
M.Msg = Me.WM_KEYDOWN
M.WParam = IntPtr.op_Explicit
(Me.VK_DOWN)
End Select

CType
(Me.GetActiveTableStyle.GridColumnStyles
(Me.CurrentCell.ColumnNumber),
DataGridExt.DataGridExtensions.DataGridMultiLineTextboxColu
mnStyle).Box.SendMessage(M)

Return True
End If
End If
If KC = Keys.Left Or KC = Keys.Right Then
Select Case KC
Case Keys.Left
If CType
(Me.GetActiveTableStyle.GridColumnStyles
(Me.CurrentCell.ColumnNumber),
DataGridExt.DataGridExtensions.DataGridMultiLineTextboxColu
mnStyle).Box.SelectionStart > 0 Then
CType
(Me.GetActiveTableStyle.GridColumnStyles
(Me.CurrentCell.ColumnNumber),
DataGridExt.DataGridExtensions.DataGridMultiLineTextboxColu
mnStyle).Box.SelectionStart -= 1
End If
Case Keys.Right
CType
(Me.GetActiveTableStyle.GridColumnStyles
(Me.CurrentCell.ColumnNumber),
DataGridExt.DataGridExtensions.DataGridMultiLineTextboxColu
mnStyle).Box.SelectionStart += 1
End Select
Return True
End If
End If
Return False
End Function



Ok So you would think everything works huh? well the enter
works and the left and right work fine, but the up and
down do not work inside the textbox, they still cause row
changes (navagation of the rows in the datagrid). What do
you guys think I'm doing wrong?

Now from what I learned doing alot of googleing is that if
the textbox has a parent control it will route the
messages to the parent (which is why I had to do the
override here), now the enter works fine. Though I found
that arrow keys are handled differently. So basiclly I
need to find an answer to my problem which is to keep the
darn arrow keys working within the textbox only.

Waiting for those great answers.

Joe
 
D

Dmitriy Lapshin [C# / .NET MVP]

Joe,

You can also try a similar approach by overriding the grid's
ProcessDialogKey and ProcessKeyPreview methods. The latter seems to be
called before a keystroke is dispatched to a hosted textbox or another
control used to edit the cell content.
 
J

Joe Fuentes

Tried that, it didn't have any affect. In keypreview it wouldn't do
anything with the up or down and the enter made 3 to 4 lines instead of
one. and the other one did nothing.

I remember tyring those before and others. I read somewhere that
controls always refer back to thier parents on the arrow keys. So that
would explain why when I send it an up arrow it's going back to the grid
again. I need to stop the bouncing.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top