Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

How to get the enter key to go to next line instead of cell belowHelpful Member! 

foxygeek (Programmer) (OP)
20 Dec 04 11:31
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.
Helpful Member!  ThatRickGuy (Programmer)
20 Dec 04 12:15
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

----------------------
http://www.ringdev.com

foxygeek (Programmer) (OP)
20 Dec 04 12:31
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.
ThatRickGuy (Programmer)
20 Dec 04 13:21
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

----------------------
http://www.ringdev.com

foxygeek (Programmer) (OP)
22 Dec 04 11:57
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.
ThatRickGuy (Programmer)
22 Dec 04 12:25
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

----------------------
http://www.ringdev.com

foxygeek (Programmer) (OP)
22 Dec 04 13:03
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
ThatRickGuy (Programmer)
22 Dec 04 13:34
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

----------------------
http://www.ringdev.com

foxygeek (Programmer) (OP)
22 Dec 04 13:48
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.

ThatRickGuy (Programmer)
22 Dec 04 14:49
I you still have this line:

CODE

MyBase.ProcessKeyPreview(msg)

it may still be processing the return.

-Rick

----------------------
http://www.ringdev.com

foxygeek (Programmer) (OP)
22 Dec 04 16:36
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.
ThatRickGuy (Programmer)
22 Dec 04 18:08
heh, yeah, the "Thank... for this post" with the little purple star next to it. Glad ya got it working!

-Rick

----------------------
http://www.ringdev.com

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close