Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the enter key to go to next line instead of cell below 1

Status
Not open for further replies.

foxygeek

Programmer
Oct 21, 2004
15
US
Hello i am working with a multiple datagrids and i have also created and autocomplete that works great with textboxes. It works great with datagridtextboxes as well but my problem is when they hit enter to go to a new line or accept the autocomplete word or phrase, it will take them to the next cell over. How can i make this not happen.I only want the tab button to move throught the datagrid, not the enter button or the arrows. Please help.
 
you can try capturing it on the KeyPress event. I beleive [control]-enter will force a new line and not change cells. You may be able to capture the enter key and set the control key to report as being down. I can't remember if that stuff is all in KeyPressed or KeyDown, one or the other.

-Rick

----------------------
 
I know how to caputre keys pressed but what do you mean by "set the control key to report as being down". Are you referring to SendKey. I created a class that extends the datagrid control and i override the ProccessCmdKey function, this is how i use my autocomplete function. But i just don't know what to do do with the enter key.
 
My bust, I was thinking the .control property could be set. instead, you could do something like this:

Code:
Private Sub C1FlexGrid1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles C1FlexGrid1.KeyDown
  If e.KeyCode = Keys.Enter Then
    CType(sender, C1.Win.C1FlexGrid.C1FlexGrid).Text &= ControlChars.Cr
    e.Handled = true
  End If
End Sub

it's for a C1 flex grid, but you should be able to do something similar with a data grid.

-Rick

----------------------
 
Ok i'm not really sure how to use that, but i found an FAQ on another website and had the same question. But i think the code was in C++ or C# so i tried to translate it into VB .net but the enter button is still moving to the next cell. HERE is the FAQ:

5.34 How can I prevent the Enter key from moving to the next cell when the user is actively editing the cell and presses Enter?

Override the method ProcessKeyPreview in your DataGrid.

protected override bool ProcessKeyPreview(ref System.Windows.Forms.Message m)

{

Keys keyCode = (Keys)(int)m.WParam & Keys.KeyCode;

if((m.Msg == WM_KEYDOWN || m.Msg == WM_KEYUP)

&& keyCode == Keys.Enter )

return false;

return true;

}



This is what i did:


Protected Overrides Function ProcessKeyPreview(ByRef msg As Message) As Boolean
MyBase.ProcessKeyPreview(msg)
Const WM_SYSKEYDOWN As Int32 = &H104
Const WM_KEYDOWN As Int32 = &H100
Dim tmpInt As Int32
tmpInt = msg.WParam.ToInt32
Dim keycode As Keys
keycode = tmpInt
If keycode = Keys.Enter Then
Return False
End If
Return True

Can anybody please help me.
 
The Code I posted above will catch the Enter key so it won't be handled, and then adds a carage return to the cell you are in. isn't that what you were looking for?

-Rick

----------------------
 
The only thing with Datagrids is that when keyup, keydown, and keypress are used it doesn't not fired when the enter key is press so by looking on forums and such i found you need to created a class that inherits the datagrid and overrided the function ProcesscmdKey. i have done this but nothing seems to work so far that i have tired to make the enter key not go to the next cell. i have tired what you said in a sense that i use the SendKey.Key("^{Enter}") but that didn't work. I tired variation of things but nothing seems to work. So found that other FAQ with the same problem and tired that, it gets fired when i presss the enter key but it still goes to the next cell. There has to be a way i just can't seem to find it. Datagrids in vb .net are alot different that Flexgrids. I don't think FlexGrids are in VB .NET
 
That was a C1 flex grid, from the Resource Kit. There seriously kick the llama's hiney. If the code you posted builds off of the standard datagrid so that the EnterKey is captured by the KeyPress (up/down) events, then you should be able to use the code I posted above along with it.

But after looking over your code, I think you might want to adjust it slightly (Assuming the first set of code in c is accurate)

Code:
Public Class clsNewDataGrid
  Inherits Windows.Forms.DataGrid

  Protected Overrides Function ProcessKeyPreview(ByRef msg As Message) As Boolean
    Const WM_KEYDOWN As Int32 = &H100
    Const WM_KEYUP As Int32 = &H101

    Dim keycode As Keys = CType(msg.WParam.ToInt32, Keys) & Keys.KeyCode
    If (msg.Msg = WM_KEYDOWN Or msg.Msg = WM_KEYUP) And keycode = Keys.Enter Then
      Return False
    End If
    Return True
  End Function
End Class

But I was unable to recreate the problem you were talking about as far as having the datagrid automatically going to the next row on enter. But if you use the class above to create a datagrid object then the enter key should not register.

-Rick

----------------------
 
When you say you weren't able to recreate the problem what did you mean. Did you mean that when you hit the enter key on a regular datagrid while editing the cell you didn't move to the next cell. On thing that i found out with that the code i got from the other site is you don't need to do the :
Dim keycode as keys = CType(msg.WParam.ToInt32, Keys) & Keys.Keycode

becuase that set the keycode to some huge number instead of 13 which is the enter key. that is why in my code i did this:

tmpInt = msg.WParam.ToInt32
Dim keycode As Keys
keycode = tmpInt

which gave the right 13 when the enter key was pressed and as i was steping through the code it would always return False when hte enter key was press but still move the the cell below.

 
Was that my problem this whole time. As soon as i took that line out it work fine, just like i wanted it to. Thank you very much for all your help. You are wonderful. Is there any type of point system in this forum so i can give you some points for being so prompt in all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top