How to enable the keyboard to control picturebox.
1. Select form and view attributes: Set keypreview to true;
2. Select form and view the event (lightning symbol next to the property): double-click the keydown event to edit the code.
Private void form1_keydown (Object sender, keyeventargs E)
{
Switch (E. keycode)
{
Case keys. Left: {picturebox1.left-= 2;} break;
Case keys. Right: {picturebox1.left + = 2;} break;
Case keys. Up: {picturebox1.top-= 2;} break;
Case keys. Down: {picturebox1.top + = 2;} break;
}
}
Private void form1_paint (Object sender, painteventargs E)
{
Int A = 10, B = 10, c, d, I, j;
C = picturebox1.location. X;
D = picturebox1.location. Y;
Graphics G = E. graphics; // create a canvas. The canvas here is provided by form.
Pen P = new pen (color. Blue, 2); // defines a blue, width-based brush.
G. drawline (P, A, B, C, D); // draw a straight line on the drawing board. The starting coordinate is (10, 10), and the ending coordinate is the coordinate in the upper left corner of picturebox.
}