Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2007

Tip: Looking for answers? Try searching our database.

DataGridView and Enter key

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mahesh Nimbalkar - 13 Jul 2007 22:02 GMT
I have a win form having DataGridView and one button which is accept button
for this form. Now when the focus is on form, enter key causes click event of
accept to fire. But when the focus is on DataGridView, enter key does not
fire click event of accept button.

I want click event to fire when focus is on DataGridView and user presses
enter key.

Steps to reproduce:

1) In WinForm application, add DataGridView and Button
2) Set added button as accept button and add click event for this button
3) Now run the application and click the DataGridView. The focus is on
DataGridView and pressing enter key, click event of button does not fire.
4) Run the application and set focus to form. Pressing enter key fires click
event of button.

Anyone knows how to solve this problem?

Thanks
Manish Bafna - 14 Jul 2007 04:26 GMT
Hi,
This behaviour is by design.The workaround for this is to override method
ProcessKeyPreview.Find below sample code which solves ur problem:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

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

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

           if (keyCode == Keys.Enter)
           {
               SomeMethod();
           }

           return true;
       }

       private void button1_Click(object sender, EventArgs e)
       {
           SomeMethod();
       }
       private void SomeMethod()
       {
           MessageBox.Show("Button1 Clicked");
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           DataTable dt = new DataTable();
           DataColumn col1 = new DataColumn();
           col1.DataType = typeof(System.String);
           col1.ColumnName = "Name";
           dt.Columns.Add(col1);

           DataRow row1 = dt.NewRow();
           row1["Name"] = "Manish Bafna";

           dt.Rows.Add(row1);

           dataGridView1.DataSource = dt;
       }

   }
}
Signature

Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

> I have a win form having DataGridView and one button which is accept button
> for this form. Now when the focus is on form, enter key causes click event of
[quoted text clipped - 16 lines]
>
> Thanks
oskar@musiker.nu - 23 Jul 2007 17:00 GMT
I have created a form (with a DataGridView) that overrides
ProcessKeyPreview as you show above. I have however encountered
another problem. The form with the DataGridView is shown when a button
is clicked on another form. If this button is clicked using the enter
key, the enter event is passed on to the form with the DataGridView
and the enter event is processed by this form too, causing an
undesireable result.

So, the question is: Can the code in ProcessKeyPreview in your example
be modified to handle this case, or should the form with the button
contain some code that handles this?
oskar@musiker.nu - 24 Jul 2007 07:32 GMT
On 23 Juli, 18:00, os...@musiker.nu wrote:
> I have created a form (with aDataGridView) that overrides
> ProcessKeyPreview as you show above. I have however encountered
[quoted text clipped - 7 lines]
> be modified to handle this case, or should the form with the button
> contain some code that handles this?

I solved this myself by using the following instead of overriding
ProcessKeyPreview:

private void dataGridView_KeyDown(object sender, KeyEventArgs args)
{
   if (args.KeyCode == Keys.Enter)
   {
       SomeMethod();
       args.Handled = true;
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.