Does a picturebox not support keyboard events?

两盒软妹~` 提交于 2020-12-26 09:05:47

问题


I am currently using Visual Studio, and I don't know if this is a glitch or not, but when I go into the form properties, and show the events, there are two events called KeyDown and KeyUp. Now when I do the same for a PictureBox, it has way less events and no KeyDown and KeyUp events. Does the PictureBox support less events then other things? Is this a glitch?

Screenshot of Form1 properties:

Screenshot of PictureBox1 properties:


回答1:


Its not a glitch. Its the way it is. You don't type in PictureBox. If you need to do some task through keys, route it through form only




回答2:


As others here have stated, the most appropriate method for capturing keyboard event in this situation is to intercept key events at the Form level, as the PictureBox control is incapable of receiving focus and lacks exposed key events.

To accomplish this, first set the KeyPreview property of the form to true within the designer, or alternatively within the form's constructor:

this.KeyPreview = true;

Then, subscribe to the KeyUp event:

this.KeyUp += MainForm_KeyUp;

Finally, use an event handler similar to as follows to intercept and process key events:

private void MainForm_KeyUp(object sender, KeyEventArgs e)
{
    switch (e.KeyCode)
    {
        case Keys.A:
            // Perform some action...
            break;
        case Keys.B:
            // Perform some action...
            break;
        case Keys.End:
            // Perform some action...
            break;

        // etc...

    }
}


If you intend to "consume" the key from within the event handler, you may set the Handled property of the KeyEventArgs object as follows:

e.Handled = true;



回答3:


The PictureBox is used to display images and therefore has no need for keyboard input. It does not take the focus and does not interact with the keyboard.




回答4:


You can use a simple trick to handle keyboard events with PictureBox.

Add an handler for MouseEnter and MouseLeave events so to set focus to the PictureBox when the cursor is over. Doing so it will catch keyboard events.

...
_myPictureBox.MouseEnter += new EventHandler(myPictureBox_MouseEnter);
_myPictureBox.MouseLeave += new EventHandler(myPictureBox_MouseLeave);
_myPictureBox.KeyDown += new KeyEventHandler(myPictureBox_event_KeyDown);
...

private void myPictureBox_MouseEnter(object sender, EventArgs e)
{
	Focus();
}

private void myPictureBox_MouseLeave(object sender, EventArgs e)
{
	FindForm().ActiveControl = null;
}

private void myPictureBox_KeyDown(object sender, KeyEventArgs e)
{
	if (e.KeyCode == Keys.Delete)
		MessageBox.Show("Bye");                
}



回答5:


Event handlers for KeyDown, KeyPress and KeyUp can be added to a PictureBox. For example, in Visual Basic, for a PictureBox called MyPicBox, add a handler for MyPicBox.MouseEnter and put the line MyPicBox.Select() in the handler. The picture box will now respond to keyboard events. For example, suppose the picture box is showing a visual display of a scientific image. Key combos like Shift-Ctrl-H or Shift-Ctrl-V can be intercepted by a PictureBox.KeyDown handler to invoke some cursor position dependent action such as displaying a cut through the underlying array:

 Private Sub MyPicBox_KeyDown(sender As Object, e As KeyEventArgs) Handles MyPicBox.KeyDown
    If e.Control AndAlso e.Shift Then
        Select Case e.KeyCode
            Case Keys.V
                MyCut = Cut.VERTICAL
                Exit Select
            Case Keys.H
                MyCut = Cut.HORIZONTAL
                Exit Select
            Case Keys.N
                MyCut = Cut.NONE
                Exit Select                
        End Select
    End If
End Sub 

The keyboard events are listed at https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.picturebox



来源:https://stackoverflow.com/questions/34759517/does-a-picturebox-not-support-keyboard-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!