I have created an aplication in C# windows application version 1.1 to display some records in a datagrid.
The datasource of the datagrid is datatable and not any database.I want tht when we edit any cell of a particular column
and press "Enter" it should show a Message Box let's say "the cell is edited."
I wrote this but its not working
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue.ToString()=="Enter")
{
// When the user presses both the 'Alt' key and 'F' key,
// KeyPreview is set to False, and a message appears.
// This message is only displayed when KeyPreview is set to True.
this.KeyPreview = false;
MessageBox.Show("KeyPreview is True, and this is from the FORM.");
}
}
Can anybody have any idea?

Dani AI

Generated

A couple of gotchas explain why bhavna_816’s form handler did not fire and why Tor Sverre saw trouble at the form level. In WinForms the Enter key is preprocessed for dialog behavior (default/accept button, focus navigation) before normal key events. If a form has an AcceptButton, pressing Enter will click that button unless you intercept it earlier in the message pipeline. The reliable, form-wide way is to override ProcessCmdKey, which runs during keyboard preprocessing and precedes default-button handling. That lets you show your message (or cancel the keystroke) even when a control has focus. Overview of keyboard input. If you still prefer event-based handling, set Form.KeyPreview = true as Reverend Jim hinted so the form can see key events before the focused control. Form.KeyPreview. Also remember: setting an AcceptButton makes Enter click that button by design. Accept button behavior.

Example: capture Enter at the form level and swallow it so it does not trigger AcceptButton.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Enter)
    {
        MessageBox.Show("Enter captured at form level.");
        return true; // consume Enter here
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

For .NET 1.1’s DataGrid, edits occur in a hosted TextBox inside the target DataGridTextBoxColumn. Hook that TextBox’s KeyDown for just the desired column, and call EndCurrentEdit so the change actually commits to the DataTable before you show your message.

var col = (DataGridTextBoxColumn)dataGrid1.TableStyles[0].GridColumnStyles["YourColumnName"];
col.TextBox.KeyDown += (s, e) =>
{
    if (e.KeyCode == Keys.Return)
    {
        var cm = (CurrencyManager)BindingContext[dataGrid1.DataSource, dataGrid1.DataMember];
        cm.EndCurrentEdit();
        MessageBox.Show("The cell is edited.");
        e.Handled = true; e.SuppressKeyPress = true;
    }
};

The hosted TextBox and column APIs are documented here: DataGridTextBox. Committing the edit via the binding manager is here: BindingManagerBase.EndCurrentEdit.

Recommended Answers

All 6 Replies

Hey, do you mean something like this?

private void [your data grid name](object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                MessageBox.Show("Enter pressed", "Attention");                
            }
        }

How does that work?

Hey, do you mean something like this?

private void [your data grid name](object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                MessageBox.Show("Enter pressed", "Attention");                
            }
        }

How does that work?

thanks for helping !
I got the solution!

I Have 2 text box and I am enter the first numbers and then press enter go to next textbox this event I want. . . . . . . . . . .

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

namespace WindowsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox1.KeyDown += new eventHandler(textBox1_KeyPress);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Keys.Enter) textBox2.Focus(); 

        }

        private void textBox1_Enter(object sender, EventArgs e)
        {


        }
    }
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            if (TextBox.Text.Length == 0)
            {
                TextBox.Focus();
                return;
            }
            else
                SendKeys.Send("{TAB}");
        }
    }

No problem getting it to work in a textbox or any other "fields" But it is a huge problem getting it to work at the Form-level

Have you set the KeyPreview form property to True?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.