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!
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.
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
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!
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)
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.
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..
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.
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..
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 ?
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.
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
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
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.
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!
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 :)
I copied your code and the form but for some reasons it doesn't work.. idk why :-(
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 ?
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.
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
Thanks, great tutorial
thanks, it's really helps me
Thank you!
THE BEST !
Why You Change the Form KeyDown Event to Keyisdown not to use the default ?
Thanks!
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!
Yes you can get the option while adding the backcolor there you can get the import picture option
thank you, i love you
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)
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.
but if i have a button thets dont work idk how to fix. Help me!
Disable the button when not needed. When there’s a button the form will set focus to the button not the form.
Thanks, aaaa ♥
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..
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.
@@mooict
oh. This is sad. thank you anyway.
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..
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 ?
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.
@@mooict thankyou
Do u have any easy projects to recommend For me to make ?
how to put the object back to its previous location using a button command? help pls :)
Can you tell a block to block movement for games like wumpos world
Yes you can. I did that with the Snakes and Ladders tutorial. See if that helps.
@@mooict thanks 😊
what are the controlls
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
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
You will have to add the timer and code to the child form for the picture box to move.
can I add ctrl button? ctrl + up = move up, like that.
May be this will help stackoverflow.com/questions/1265634/keydown-recognizing-multiple-keys
@@mooict thanks but i already fix e.Controls && e.KeyCode == e.Keys.Left
Yes it is working, but the problem is when i add a button then it won't work? any fix?
Buttons will override the focus from key down and key up. The focus will return back to the form when the button is disabled.
what are the controls ?
Why I only got PreviewKeyDown
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)
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.
@@mooict thnx
@@mooict Please add this in the description, moreover, how do we make it so windows forms does not prioritize this button? Thank you.
for me the picturebox would move but 10 seconds later it would stop working can anyone help
So this works but for some reason if i place down a button it doesn't work anymore. Anyone know why?
Buttons take the focus away from the form. Disable the button once clicked and it will work again
my picturebox just falls infinitely.??
Can I get code??
i have done all the code correct 0 errors but the picture box wont move
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;
this looks like the snake movement in Snake Game
Yes it the earlier version for movements in Windows form, I did before doing the snakes game.