C #: Windows Forms: Getting keystrokes in a panel / picture?

I am creating a level editor for a game using window forms. The form has several drop-down menus, text fields, etc., where the user can enter information.

I want to make commands like CTRL + Vor CTRL + Aavailable for work in the game world itself, and not for word processing. The world of the game is presented PictureBoxcontained in Panel.

This event handler never starts:

private System.Windows.Forms.Panel canvas;
// ...
this.canvas = new System.Windows.Forms.Panel();
// ...
this.canvas.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.canvas_PreviewKeyDown);

What is the preferred way to do this? Can I Panelget keyboard input? I would like to allow the user to use the copy / paste / select-all commands when working with text input, but not when placing objects in the game world.

+3
2

MSDN Control.CanSelect:

Windows Forms false CanSelect. .

  • GroupBox
  • PictureBox
  • ProgressBar
  • LinkLabel ( )

, , , , SetStyle "". TabStop true, .

public class SelectablePanel : Panel
{
    public SelectablePanel()
    {
        this.SetStyle(ControlStyles.Selectable, true);
        this.TabStop = true;
    }
}

. PreviewKeyDown, .

+1

Source: https://habr.com/ru/post/1742554/


All Articles