C # Move a keyboard control Control

Source: Internet
Author: User

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.

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.