Karmello Karmello - 3 months ago 20
C# Question

How can i totally disable tabbing on DataGridView but keep ability to select rows?

How can I totally disable tabbing on DataGridView so it won't go through cells at all?

I use DataGridView as music playlist in my application and I don't need that annoying windows default selection frame around cells. I want be able to select rows normally. I managed to hide selection border on buttons with SetStyle(ControlStyles.Selectable, false) but this does not disable tabbing on DataGridView.

Answer

OK. I've managed to do that. This ARTICLE helped me a lot. I used form's OnActivated and OnDeactivated events to disable and enable TAB key. Here you have sample code:

protected override void OnActivated(EventArgs e) {

    ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
    objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
    ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);

    base.OnActivated(e);
}

protected override void OnDeactivate(EventArgs e) {

    UnhookWindowsHookEx(ptrHook);
    objKeyboardProcess = null;
    ptrHook = IntPtr.Zero;

    base.OnDeactivate(e);
}

There were a couple of problems that came up while i was trying to make it work but that's different story. Happy coding! :)