C# Tutorial - Move Picture box in windows form with keyboard and timer in visual studio

Поделиться
HTML-код
  • Опубликовано: 2 ноя 2024

Комментарии • 59

  • @alejandrobailon1761
    @alejandrobailon1761 4 года назад +3

    I spent around half an hour searching how to do this in english and spanish posts, videos and webpages and nothing helped. Your tutorial is exactly what I needed, thanks!

  • @lordofkeyboards2066
    @lordofkeyboards2066 3 года назад +5

    Thanks! I'm mostly learning C# with unity and I didn't know C# was this powerful! so why not learn this too.
    thank you for this course :)

  • @lorenzolonghi8697
    @lorenzolonghi8697 3 года назад +7

    I copied your code and the form but for some reasons it doesn't work.. idk why :-(

  • @shakz3969
    @shakz3969 3 года назад +2

    thanks this helped out a lot for my snake game, wondering why variables are assigned false in the keyIsUp method and true in the keyIsDown ?

    • @mooict
      @mooict  3 года назад

      We do this to move the character and also to stop the character from moving. We are using the keydown to move the character towards that direction and when the keys up it stops moving to that direction. The movement is happening inside of the time event. When either of the Boolean is true it will move the character towards that direction and when it's false and it won't update the movement.

    • @yazanjordan3103
      @yazanjordan3103 3 года назад

      because if u make them true in the keyisup the object will keep moving forever so this way we tell the computer whenever keyisup just stop the object from moving

  • @luky4013
    @luky4013 3 года назад +1

    Thanks, great tutorial

  • @madhater9900
    @madhater9900 3 года назад +1

    thanks, it's really helps me

  • @25jaws1
    @25jaws1 3 года назад +1

    Thank you!

  • @lienhard38
    @lienhard38 2 года назад

    THE BEST !

  • @yemensoft9566
    @yemensoft9566 Месяц назад

    Why You Change the Form KeyDown Event to Keyisdown not to use the default ?

  • @lukaasg3476
    @lukaasg3476 4 года назад +1

    Thanks!

  • @MaxCreed15
    @MaxCreed15 3 года назад

    Thank you this was great! is there a way I can rotate an imported image? I have added a birds-eye view car image and it would be great to know how to rotate the image for a particular key direction. Thanks in advance!

    • @shiekhrazia9806
      @shiekhrazia9806 2 года назад

      Yes you can get the option while adding the backcolor there you can get the import picture option

  • @juanilopezpatterson7330
    @juanilopezpatterson7330 4 года назад

    thank you, i love you

  • @kettee1-qv3ps
    @kettee1-qv3ps Год назад

    Awesome, thanks. I did what you showed and it works. But then I added a button and it dosent work anymore (picturebox doesent move), when I delete the button it works again. Could someone help? What I am trying to do is, to add button, and when you press the button it increases the speed of the picturebox (changes int speed variable)

    • @mooict
      @mooict  Год назад

      HI adding a button will take the focus away from the form, so it will not trigger the keyboard events. Thats why its not working. You can assign a different KEY in the key down and key up to increase/decrease the speed instead.

  • @Proxypp
    @Proxypp 5 месяцев назад +1

    but if i have a button thets dont work idk how to fix. Help me!

    • @mooict
      @mooict  5 месяцев назад

      Disable the button when not needed. When there’s a button the form will set focus to the button not the form.

  • @yaryerazo3077
    @yaryerazo3077 4 года назад +1

    Thanks, aaaa ♥

  • @TKCreative
    @TKCreative 2 года назад

    Hello moo. First of all, thank you for this video. I have a question. There is a picture of the car steering wheel. My goal is to be able to turn it left and right. I want to link the image to HScrollBar. Can I do that with the codes here? I tried many methods but it didn't work. I am not a professional c# user. I need your help. Please..

    • @mooict
      @mooict  2 года назад +1

      Unfortunately windows form doesn't support rotation of picture box. WPF template does. Also you may be able to work around in Windows form by using different rotation angle images load them up for each value from the scroll bar so it looks like the steering wheel is rotating. With WPF there is a rotation angle built into the images class that can help with your query. Good luck.

    • @TKCreative
      @TKCreative 2 года назад

      @@mooict
      oh. This is sad. thank you anyway.

    • @TKCreative
      @TKCreative 2 года назад +1

      Hello again mate. Simple way, I hooked image rotation to hscrollbar. The image was getting corrupted during rotation, but doubleBuffered = true; I fixed it by typing. It works great. I wanted to write it for those who need it. Thanks again..

  • @cainsykes7099
    @cainsykes7099 3 года назад

    Just beginning my journey with Coding, I am trying to Learn and memorise simple things like this , do u think it is a good idea to watch this tutorial a few times and then try re write this code out without any help from the video ?

    • @mooict
      @mooict  3 года назад

      Hi personally I found that making projects often as a beginner's just getting simple things working helps learn code faster. I haven't tried to memorise it before. My recommendation is do as many projects as you want follow any of the tutorials and then try to mix those up and add features of your own. It will break lots of time and each time you fix it you will understand it better. Coding is a skill like any other, more you do it the better you will get.

    • @cainsykes7099
      @cainsykes7099 3 года назад

      @@mooict thankyou

    • @cainsykes7099
      @cainsykes7099 3 года назад

      Do u have any easy projects to recommend For me to make ?

  • @nrain4049
    @nrain4049 3 года назад

    how to put the object back to its previous location using a button command? help pls :)

  • @tayyabali7583
    @tayyabali7583 3 года назад

    Can you tell a block to block movement for games like wumpos world

    • @mooict
      @mooict  3 года назад +1

      Yes you can. I did that with the Snakes and Ladders tutorial. See if that helps.

    • @tayyabali7583
      @tayyabali7583 3 года назад

      @@mooict thanks 😊

  • @thegamingmonster8061
    @thegamingmonster8061 3 года назад

    what are the controlls

  • @MistrzChomik
    @MistrzChomik Год назад +1

    if anyone need this:
    if (!moveL && !moveR && !moveU && !moveD)
    {
    moveTimer.Stop();
    }
    i figured out this formula, so that changing direction of moving was smoother, and prevent picture block from blocking when rapidly pressing move buttons on keyboard

  • @stefanobradovic2461
    @stefanobradovic2461 2 года назад

    i am trying to make a maze game, and i want to move my pb but it is in a child form and it's not working, when i try it in just a regular form it works, how do i fix it

    • @mooict
      @mooict  2 года назад

      You will have to add the timer and code to the child form for the picture box to move.

  • @krstffrtejada6025
    @krstffrtejada6025 3 года назад

    can I add ctrl button? ctrl + up = move up, like that.

    • @mooict
      @mooict  3 года назад

      May be this will help stackoverflow.com/questions/1265634/keydown-recognizing-multiple-keys

    • @krstffrtejada6025
      @krstffrtejada6025 3 года назад

      @@mooict thanks but i already fix e.Controls && e.KeyCode == e.Keys.Left

  • @dragonomar271
    @dragonomar271 2 года назад

    Yes it is working, but the problem is when i add a button then it won't work? any fix?

    • @mooict
      @mooict  2 года назад

      Buttons will override the focus from key down and key up. The focus will return back to the form when the button is disabled.

  • @polofnbr649
    @polofnbr649 3 года назад

    what are the controls ?

  • @naturemorte7919
    @naturemorte7919 Год назад +1

    Why I only got PreviewKeyDown

  • @tomasslavik9351
    @tomasslavik9351 3 года назад

    Nice code, but why when I add a button, the code no longer works? (that button doesn't do anything, I just put it there)

    • @mooict
      @mooict  3 года назад

      Button is an active object its looking for an event to trigger so windows form will prioritize that button on the form first, this is why the keydown and key up events won't trigger if a active button is on the form. If you disable the button and run the program it will start working again. Test it out.

    • @tomasslavik9351
      @tomasslavik9351 3 года назад +1

      ​@@mooict thnx

    • @magic874
      @magic874 2 года назад

      @@mooict Please add this in the description, moreover, how do we make it so windows forms does not prioritize this button? Thank you.

  • @quake11808
    @quake11808 3 года назад

    for me the picturebox would move but 10 seconds later it would stop working can anyone help

  • @ThiboVerhoye
    @ThiboVerhoye Год назад

    So this works but for some reason if i place down a button it doesn't work anymore. Anyone know why?

    • @mooict
      @mooict  Год назад

      Buttons take the focus away from the form. Disable the button once clicked and it will work again

  • @outcastcrew6
    @outcastcrew6 2 года назад

    my picturebox just falls infinitely.??

  • @theasoklyvan1976
    @theasoklyvan1976 2 года назад

    Can I get code??

  • @polofnbr649
    @polofnbr649 3 года назад

    i have done all the code correct 0 errors but the picture box wont move

    • @polofnbr649
      @polofnbr649 3 года назад

      here is the code: using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      namespace WindowsFormsApp6
      {
      public partial class Form1 : Form
      {
      bool moveRight, moveLeft, moveUp, moveDown;
      int speed = 12;
      public Form1()
      {
      InitializeComponent();
      }
      private void moveTimerEvent(object sender, EventArgs e)
      {
      if (moveLeft == true && pictureBox1.Left > 0)
      {
      pictureBox1.Left -= speed;
      }
      if (moveRight == true && pictureBox1.Left < 699)
      {
      pictureBox1.Left += speed;
      }
      if (moveUp == true && pictureBox1.Top > 0)
      {
      pictureBox1.Top -= speed;
      }
      if(moveDown == true && pictureBox1.Top < 466)
      {
      pictureBox1.Top += speed;
      }
      }
      private void keysidown(object sender, KeyEventArgs e)
      {
      if(e.KeyCode == Keys.Left)
      {
      moveLeft = true;
      }
      if(e.KeyCode == Keys.Right)
      {
      moveRight = true;
      }
      if(e.KeyCode == Keys.Down)
      {
      moveDown = true;
      }
      if(e.KeyCode == Keys.Up)
      {
      moveUp = true;
      }
      }
      private void keyisup(object sender, KeyEventArgs e)
      {
      if (e.KeyCode == Keys.Left)
      {
      moveLeft = false;
      }
      if (e.KeyCode == Keys.Right)
      {
      moveRight = false;
      }
      if (e.KeyCode == Keys.Down)
      {
      moveDown = false;
      }
      if (e.KeyCode == Keys.Up)
      {
      moveUp = false;

  • @alirezasamereh_general
    @alirezasamereh_general 2 года назад +1

    this looks like the snake movement in Snake Game

    • @mooict
      @mooict  2 года назад

      Yes it the earlier version for movements in Windows form, I did before doing the snakes game.

  • @Kronos.Saturn
    @Kronos.Saturn 2 года назад