Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:42:35+00:00 2026-05-16T17:42:35+00:00

I am continuing to program some kind of keyboard navigation in my simple graphic

  • 0

I am continuing to program some kind of keyboard navigation in my simple graphic program (using C#). And I ran into trouble once again.

alt text

My problem is that I want to process the keyboard input to move a layer around. Moving the layer with the mouse already works quite well, yet the control doesn’t get the focus (neither KeyUp/KeyDown/KeyPress nor GotFocus/LostFocus is triggered for this control).
Since my class derives from Panel (and overwrites a couple of events), I’ve also overwritten the events mentioned above, but I can’t succeed in getting those events triggered.

I think I could manage to implement keyboard response either using something like Keyboard.GetState() or ProcessCmdWnd or something. However: I still have to be able to tell when the control got the focus.

Is there an more or less elegant way to add this ability to a user control (which is based on Panel)?

I’ve checked many threads in here and I might use this approach for keyboard input. The focus problem however still remains.

Thank you very much for information in advance!

Igor.

p.s.: I am programming in C# .NET v3.5, using VS2008. It’s a Windows.Forms application, not WPF.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:42:36+00:00Added an answer on May 16, 2026 at 5:42 pm

    The Panel class was designed as container, it avoids taking the focus so a child control will always get it. You’ll need some surgery to fix that. I threw in the code to get cursor key strokes in the KeyDown event as well:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    class SelectablePanel : Panel {
        public SelectablePanel() {
            this.SetStyle(ControlStyles.Selectable, true);
            this.TabStop = true;
        }
        protected override void OnMouseDown(MouseEventArgs e) {
            this.Focus();
            base.OnMouseDown(e);
        }
        protected override bool IsInputKey(Keys keyData) {
            if (keyData == Keys.Up || keyData == Keys.Down) return true;
            if (keyData == Keys.Left || keyData == Keys.Right) return true;
            return base.IsInputKey(keyData);
        }
        protected override void OnEnter(EventArgs e) {
            this.Invalidate();
            base.OnEnter(e);
        }
        protected override void OnLeave(EventArgs e) {
            this.Invalidate();
            base.OnLeave(e);
        }
        protected override void OnPaint(PaintEventArgs pe) {
            base.OnPaint(pe);
            if (this.Focused) {
                var rc = this.ClientRectangle;
                rc.Inflate(-2, -2);
                ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert an old 'C' program containing some static methods into Obj-c
I'm writing a program in ocaml containing some loops for, my problem is that
Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
I am writing program witch runs msi installations silent on background. Using MsiSetExternalUI and
i need to write a program in which main() would read a file(containing some
I'm pre-compiling a C program containing Pro*C code with Oracle 10.2 and AIX 5.2
I'm trying to do this: cmd.exe /C C:\Program Files\Somewhere\SomeProgram.exe > C:\temp\Folder Containing Spaces\SomeProgram.out However,
Continuing from my previous question , is there a comprehensive document that lists all
Continuing my investigation of expressing F# ideas in C#, I wanted a pipe forward
Due to continuing crash problems, I'm about to uninstall and reinstall my copy of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.