me.acceptbutton and datagrid

GornHorse

Well-known member
Joined
May 27, 2003
Messages
105
Location
Australia
Hi There,

Quick query, I hope...

I have a windows form with a datagrid on it.

I have set the me.acceptbutton = btnSave so that when the <enter> button is pressed, it prompts the user to save. This works great when any of the controls OTHER than the datagrid have control, such as labels & textboxes.

When the datagrid has the focus, and the enter button is pressed, nothing happens. The problem with this is, that the user is going to press the <enter> button when the datagrid has the focus, as this is the major data entry portion of the screen.

What im wondering is if anyone can supply some handy vb.net hints that will enable the user to press <enter> when the datagrid has the focus and be prompted to save?

Your assistance with this will be much appreciated.

Regards,
Michelle
 
I have encountered such a requirement and my solution was to override the ProcessKeyPreview method:
[VB]
Public Class CDataGrid
Inherits Windows.Forms.DataGrid
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101

Protected Overrides Function ProcessKeyPreview(ByRef m As
Windows.Forms.Message) As Boolean
Dim keycode As Keys = CType(m.WParam.ToInt32, Keys)
And Keys.KeyCode
If (m.Msg = WM_KEYDOWN OrElse m.Msg = WM_KEYUP)
AndAlso keycode = Keys.Enter Then
-If Enter was pressed, bubble up message to containing form.
Return Me.FindForm().PreProcessMessage(m)
Else
Return True
End If
End Function
End Class
[/VB]
 
Hi Jabe,

Thanks for the code, but naivity has set in. What do i actually do with it? I have tried just placing the function within my current class, and it seems to keep calling itself, and wont let me type anything into the textboxes i have on the form.

If i put it into its own class like you have got, what exactly do i do with it? Do i call it? If so, when do i call it? Is CDataGrid the name of the datagrid that you have on your form?

Please provide these details, as still relatively new to VB.NET.


Regards,
Michelle
 
Hi There.....
Time to open this up again!

It was working great, but it has just completely stopped functioning! I dont get it. The only code that i have added since is a reference to setting the currentcell of the datagrid, but i have commented that new code out, and it still wont work. I have put a debug breakpoint on the overriding function in the dgBatchPayments class, and it doesnt even call it now. Im not quite sure what has happened to make this not work??? Also, if i comment out the entire class, when i press <enter> within the datagrid, it doesnt even do the default anymore, which is go to the next row.

Completely stumped!

Please help ASAP.

Regards,
Michelle
 
Alright,

I think ive figured it out. The override works when the <enter> button is pressed when the actual datagrid has the focus, but NOT when a cell within the datagrid has the focus.

So theoretically, should i be able to do something similar for the datatable style?? Or perhaps the columns?

Any ideas?

Im going to work with this theory, but let me know if you perhaps have an answer for this query as well.

Thanks,
Michelle
 
Hi There

GOT IT!!!!

Ok, i set the following property to all of my datagridtextboxcolumn style thingys:

me.colstyle1.textbox.acceptreturn = False

By setting this, it defaults to the pages accept button. The other great thing about it, is i can remove the Override function, and it still works sweet!

Alright,

That ends this session!

Cheerio,
Michelle
 
Back
Top