Question

I'm trying to develop a results box that pops when I start to type in cell in the datagridview, something similar to what Chrome does when you type in the address bar.

I've done most of it, the main problem I'm having is trying to capture and handle arrow key presses (up and down). I've caste the cell control to a textbox so I can grab key presses and pass them to the result box so it know what results to display.

tx.PreviewKeyDown += new PreviewKeyDownEventHandler(tx_PreviewKeyDown);
tx.KeyDown += new KeyEventHandler(tx_KeyDown);
tx.KeyPress += new KeyPressEventHandler(tx_KeyPress);

that works all fine, the problem is, when I press the arrow key down, the tx_PreviewKeyDown captures it, then it disappears until its caught in the datagridview_KeyUp event, by then, its too late and the datagridview has shifted the active cell downwards.

I can handle the event in the tx_PreviewKeyDown and pass that down to move the highlighted value in the result box fine, but what I need to do is prevent it from shifting the active cell downwards into the new row.

I need to be able to cancel the keypress, but the PreviewKeyDown doesn't have a e.Handle flag, either that I need to find what KeyDown event is fired after the tx_PreviewKeyDown but before datagridview_KeyUp event. Something handles that arrow key, but what?!

Was it helpful?

Solution

You can always extend the DataGridView and use the WndProc method:

class exDataGridView : DataGridView
{
    private const int WM_KEYDOWN = 0x100;
    private const int WM_KEYUP = 0x101;
    private const int KEYUP = 38;
    private const int KEYDOWN = 40;

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_KEYDOWN:
                if (m.WParam == (IntPtr)KEYDOWN)
                {
                    // Do key down stuff...
                    return;
                }
                else if (m.WParam == (IntPtr)KEYUP)
                {
                    // Do key up stuff...
                    return;
                }
                break;
        }

        base.WndProc(ref m);
    }
}

OTHER TIPS

Try to use the ff. events

CellBeginEdit - this event will fire when you start typing in the cell, CellEndEdit - when you lost focus in the cell, DataError - if error occur during data input

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top