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

  Рет қаралды 39,065

Moo ICT

Moo ICT

Күн бұрын

In this tutorial, we will cover how to move a simple picture box across the screen using key down, key up and timer events. We will do all of this inside of Windows Form App using Visual Studio and C#. We are not using any games engine for this tutorial. This will be a building block to where we want to progress into more fun and advanced games development in the future tutorials. If you want to learn more about programming in windows form and c# check out www.mooict.com... for more written tutorials on games development and foundation programming skills.
You will see demonstrations on how to use a simple windows form component and interact with it dynamically using the key down event, key up event and using a timer with its own event. We will also stop the box from going out of the boundary. We are using 4 different Boolean for this tutorial, we will show you how to use them effectively inside of the program. Happy Learning.

Пікірлер: 58
@lordofkeyboards2066
@lordofkeyboards2066 3 жыл бұрын
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 :)
@alejandrobailon1761
@alejandrobailon1761 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!
@lorenzolonghi8697
@lorenzolonghi8697 3 жыл бұрын
I copied your code and the form but for some reasons it doesn't work.. idk why :-(
@Proxypp
@Proxypp 4 ай бұрын
but if i have a button thets dont work idk how to fix. Help me!
@mooict
@mooict 4 ай бұрын
Disable the button when not needed. When there’s a button the form will set focus to the button not the form.
@25jaws1
@25jaws1 3 жыл бұрын
Thank you!
@luky4013
@luky4013 2 жыл бұрын
Thanks, great tutorial
@madhater9900
@madhater9900 3 жыл бұрын
thanks, it's really helps me
@shakz3969
@shakz3969 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 2 жыл бұрын
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 2 жыл бұрын
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
@lukaasg3476
@lukaasg3476 4 жыл бұрын
Thanks!
@lienhard38
@lienhard38 2 жыл бұрын
THE BEST !
@naturemorte7919
@naturemorte7919 Жыл бұрын
Why I only got PreviewKeyDown
@juanilopezpatterson7330
@juanilopezpatterson7330 3 жыл бұрын
thank you, i love you
@yaryerazo3077
@yaryerazo3077 4 жыл бұрын
Thanks, aaaa ♥
@nrain4049
@nrain4049 3 жыл бұрын
how to put the object back to its previous location using a button command? help pls :)
@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.
@thegamingmonster8061
@thegamingmonster8061 3 жыл бұрын
what are the controlls
@MistrzChomik
@MistrzChomik Жыл бұрын
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
@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
@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.
@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 ?
@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 ?
@quake11808
@quake11808 3 жыл бұрын
for me the picturebox would move but 10 seconds later it would stop working can anyone help
@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 жыл бұрын
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 жыл бұрын
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..
@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
@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 жыл бұрын
​@@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.
@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
@outcastcrew6
@outcastcrew6 2 жыл бұрын
my picturebox just falls infinitely.??
@tayyabali7583
@tayyabali7583 3 жыл бұрын
Can you tell a block to block movement for games like wumpos world
@mooict
@mooict 3 жыл бұрын
Yes you can. I did that with the Snakes and Ladders tutorial. See if that helps.
@tayyabali7583
@tayyabali7583 3 жыл бұрын
@@mooict thanks 😊
@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;
@theasoklyvan1976
@theasoklyvan1976 2 жыл бұрын
Can I get code??
@alirezasamereh549
@alirezasamereh549 2 жыл бұрын
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 жыл бұрын
How to make a simple breakout game in windows form with C#
40:01
Шок. Никокадо Авокадо похудел на 110 кг
00:44
Nastya and balloon challenge
00:23
Nastya
Рет қаралды 58 МЛН
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 3,4 МЛН
Intro to Windows Forms (WinForms) in .NET 6
1:35:50
IAmTimCorey
Рет қаралды 225 М.
NEVER install these programs on your PC... EVER!!!
19:26
JayzTwoCents
Рет қаралды 3,4 МЛН
Learning C# In A Week... Otherwise I Fail University
9:04
Training an unbeatable AI in Trackmania
20:41
Yosh
Рет қаралды 13 МЛН
Hacking Websites with SQL Injection - Computerphile
8:59
Computerphile
Рет қаралды 2,4 МЛН
Шок. Никокадо Авокадо похудел на 110 кг
00:44