Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not detecting escape key C#

Tags:

c#

keydown

First of all greetings to everybody, for this is my first post.

i'm new in C# so it may be that the solution is obvious.

I have a datagridview with names and dates data. When double click on a date column a datetimepicker is shown for date to be picked. If Escape is hit, datetimepicker is hidden and the old data takes effect. When the code is run and if I double click on the date column in the last row, datetimepicker pops up, but escape in not being read. If the same is done in some row before the last, esc is working fine. If the same is done firstly in some other row and then in the last row, again the esc is working fine. The only problem is when datetimepicker is activated firstly in the last row. Also stand for Enter key

Used for key reading and datetimepicker handling

private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter) { CellAddValue(1, dateTimePicker1.Value); }
    if (e.KeyCode == Keys.Escape) { CellAddValue(1, dateWhenSelDate); }
}

public void CellAddValue(int o, DateTime date)
{
    string dateString = date.ToString(dateFormat);
    if (DateTime.Compare(date, dateTimePicker1.MinDate) == 0) { dateString = ""; }
    dataGridView1.Rows[selectedRow].Cells[selectedColumn].Value = dateString;
    if (o == 1)
    {
        dateTimePicker1.Hide();
        dataGridView1.Focus();
    }
}

Is there some settings or options that i must set to be able to work? Thanks

like image 972
Djansr Avatar asked Jan 26 '26 14:01

Djansr


1 Answers

Think this is a win-form application

Have a look at Form.KeyPreview

You need to set

From.KeyPreview = true;
like image 164
huMpty duMpty Avatar answered Jan 28 '26 05:01

huMpty duMpty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!