Search code examples
c#focusdynamic-controls

Dynamic Video Player Will Not Release Focus


Hi Everyone i Know By Default Labels and Picture Box cannot Have Focus, i Design a Method on the Form Key down to Move these Controls About with the arrow buttons

When i Make a Dynamic Video Player Control, i Can No longer move these controls nor does the form appear to gain focus anymore

I Have Tried This.Focus after making the video player, is there anything anyone can think of that maybe could keep focus on the form permanent

If Example Code is Need i will copy, but the code itself appears to be fine the video player just keeps the focus

Form 1 Keydown

        if (SenderS == "Label")
        {
            // Label b = sender as Label;
            Label b = (Label)Controls[ControlID];
            int x = b.Location.X;
            int y = b.Location.Y;

            if (e.KeyCode == Keys.Right) x += 1;
            else if (e.KeyCode == Keys.Left) x -= 1;
            else if (e.KeyCode == Keys.Up) y -= 1;
            else if (e.KeyCode == Keys.Down) y += 1;

            b.Location = new Point(x, y);
        }

Label Mouse Down

        SenderS = "Label";
        Label b=sender as Label;

        ControlID = b.Name;

This Code Works fine but when a video is added to the form i can no longer use it i assume because the form no longer accepts the keydown event


Solution

  • Presented as general purpose,You first need to override the IsInputKey method:

            private void YourControl_KeyDown(object sender, KeyEventArgs e)
            {
                //your logic here
            }
    
            protected override bool IsInputKey(System.Windows.Forms.Keys keyData)
            {
                switch (keyData)
                {
                    case Keys.Right:
                    case Keys.Left:
                    case Keys.Up:
                    case Keys.Down:
                        return true;
                }
                return base.IsInputKey(keyData);
            }
    

    Or override the ProcessCmdKey method.