Skip to main content

C# Application Moving An Image Using Arrow Keys of Keyboard


To run this application you have Visual Studio installed on your laptop/PC and have basic knowledge about C# language.

1.       Create new project.

2.       Open Windows Form  Application. Name it MovingTheImage or something else you want.


         3.       Double Click the form1 on Solution Explorer.


4.       Change the Background Color of the Form.
        i.                     Select the form by single click on it.
        ii.                   Right click  > Properies > BackColor > Black
        iii.                  Save (Ctrl+S).



5.       Go to toolbox Double Click on timer to add timer control.


6.       Now add Picture Box Control to the Form1.
        i.                     Add Image to the Picture Box.
       Properties>Image>Local Resource Or Project resource> Import>Ok.


7.       Double Click on Form1. Add Following Code.
          namespace MovingTheImage
          {
                 publicpartialclassForm1 : Form
         {    
          privateint x;
          privateint y;

                 public Form1()
                {
              InitializeComponent();
              x = 20;
              y = 20;


          }
                privatevoid Form1_Load(object sender, EventArgs e)
         {
          timer1.Start();
             pictureBox1.Location = newPoint(x, y);

        }
    }
}


8.       Run the Application By Pressing F5 or Ctrl+F5.


                   The Image Does not Move.

           9.       Now Stop running the application. Double click on timer1.
           Add  following Code to the timer event.
             privatevoid timer1_Tick(object sender, EventArgs e)
             {
              if (imgPosition == Positioning.Right)
               {
                x += 10;
                pictureBox1.Location = newPoint(x, y);
               }
              elseif (imgPosition == Positioning.Left)
               {
                x -= 10;
                pictureBox1.Location = newPoint(x, y);
              }
               elseif (imgPosition == Positioning.Up)
             {
                y -= 10;
                pictureBox1.Location = newPoint(x, y);
             }
              elseif (imgPosition == Positioning.Down)
             {
                y += 10;
                pictureBox1.Location = newPoint(x, y);
             }

             Invalidate();

        }


10.       Make following changes tothe code of seventh step:-

           publicpartialclassForm1 : Form
           {
           enumPositioning
            {
            Up,Down,Left,Right
            }

           privateint x;
           privateint y;
           privatePositioning imgPosition;

          public Form1()
           {
            InitializeComponent();
            x = 20;
            y = 20;
            imgPosition = Positioning.Right;

  }

  Keep rest as it is.
  Run Again.
  Your Image Strat Moving to the right.


     In this Step we Will Control the movement of the Image By the arrow keys.

11.       Open the properties window for Form1. Now Click on events button on top then
Find the keyDown Property Double click on it.




                   You will Reach Here.

    


12.       Add following code:-


   privatevoid Form1_KeyDown(object sender, KeyEventArgs e)
        {
if (e.KeyCode == Keys.Left)
               {
                imgPosition = Positioning.Left;

            }
elseif (e.KeyCode == Keys.Right)
            {
                imgPosition = Positioning.Right;

            }
elseif (e.KeyCode == Keys.Up)
            {
                imgPosition = Positioning.Up;
            }
elseif (e.KeyCode == Keys.Down)
            {
                imgPosition = Positioning.Down;
            }

}


         Here is the final Code For it.

            namespace MovingTheImage
            {
          publicpartialclassForm1 : Form
             {
          enumPositioning
          {
            Up,Down,Left,Right
          }

          privateint x;
          privateint y;
          privatePositioning imgPosition;

          public Form1()
           {
            InitializeComponent();
            x = 20;
            y = 20;
            imgPosition = Positioning.Right;

           }
            privatevoid Form1_Load(object sender, EventArgs e)
          {
            timer1.Start();
            pictureBox1.Location = newPoint(x, y);

           }
            privatevoid timer1_Tick(object sender, EventArgs e)
           {
            if (imgPosition == Positioning.Right)
            {
                x += 10;
                pictureBox1.Location = newPoint(x, y);
            }
            elseif (imgPosition == Positioning.Left)
            {
                x -= 10;
                pictureBox1.Location = newPoint(x, y);
            }
            elseif (imgPosition == Positioning.Up)
             {
                y -= 10;
                pictureBox1.Location = newPoint(x, y);
            }
          elseif (imgPosition == Positioning.Down)
            {
                y += 10;
                pictureBox1.Location = newPoint(x, y);
            }

            Invalidate();

            }


            privatevoid Form1_KeyDown(object sender, KeyEventArgs e)
            {
            if (e.KeyCode == Keys.Left)
            {
                imgPosition = Positioning.Left;

            }
            elseif (e.KeyCode == Keys.Right)
            {
                imgPosition = Positioning.Right;

            } 
            elseif (e.KeyCode == Keys.Up)
            {
                imgPosition = Positioning.Up;
            }
            elseif (e.KeyCode == Keys.Down)
            {
                imgPosition = Positioning.Down;
            }

            }
          }
         }




Now you can run your application and see the output, if you have any query related to application you can post here.








Comments

Popular posts from this blog

Home automation using Arduino Board

Today’s homes require sophistication control in its different gadgets which are basically electronic appliances. This has revolutionized the area of home automation with respect to an increased level of affordability and simplicity through the integration of home appliances with smart phone and tablet connectivity. Smart phones are already feature-perfect and can be made to communicate to any other devices in an adhoc network with a connectivity options like Bluetooth . With the advent of mobile phones, Mobile applications development has seen a major outbreak. Utilizing the opportunity of automating tasks for a smart home, mobile phone commonly found in normal household can be joined in a temporary network inside a home with the electronic equipments. Android, by Google Inc. provides the platform for the development of the mobile applications for the Android devices . Home automation system is a mobile application developed using Android targeting its vast market which will be benefi...

Beyond the Chat Box: How Agentic AI is Quietly Automating Your Customer's Next Purchase Journey

We have all been there. You land on an e-commerce storefront, a generic chat bubble pops up in the bottom right corner, and a robot named "ShopBot" asks if you need help finding shoes. You type in a specific problem—say, your last order arrived damaged and you need a replacement shipped to a temporary holiday address. The chatbot instantly gives you a link to the standard returns policy page. You are stuck in a dead-end conversational loop. For years, e-commerce has leaned on these traditional chatbots to deflect customer service tickets. But in 2026, a fundamental structural shift is happening. E-commerce is moving away from reactive, prompt-based chat interfaces and entering the era of Agentic Commerce (McKinsey, 2026). If you are still treating AI as a glorified answering machine, you aren't just falling behind—you are optimizing for an outdated version of the internet. Here is what separates traditional chatbots from agentic AI systems, and how the shift is changi...

Tips for Building Android Mobile and Tablet Apps

1. Check Popular Apps Before you actually start writing your own apps, you definitely want to resear ch the market if there are already exisiting apps providing the planned features. If there are, then take notes about what they provide and ask yourself if you are confident in doing better. 2. Model After Existing Sample Codes If you installed the Android SDK, there are several great sample projects inside sdk/extras/android/support/samples showcasing almost all the user interface examples and major functionalities, which you can use as templates to build and modify for your apps. 3. Avoid Undocumented APIs Some code worked at first but after a few Android SDK updates, it stopped working. 4. Layout for Device Orientation and Various Screen Sizes Since devices have embedded orientation sensors and they are usually defaulted to automatic mode, we need to account for that to provide a better user experience.