I have a picturebox that I want to relocate when a key is pressed.
I'm making a game of pong. With the code below I want to make the paddle of player 1, which is a picturebox that should move up and down while a key is pressed. The following code doesn't seem to work:
public partial class Form1 : Form
{
bool upPressed, downPressed;
public Form1()
{
InitializeComponent();
}
private void PongTimer_Tick(object sender, EventArgs e)
{
if(upPressed){
paddlePlayer1.Location = new Point(paddlePlayer1.Location.X, paddlePlayer1.Location.Y + 5);
}
if (downPressed)
{
MessageBox.Show("Numlock");
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = true;
}
if (e.KeyCode == Keys.NumLock)
{
downPressed = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Up){
upPressed = false;
}
}
}
I used the Numlock to see if the problem was in the picture, this isn't the case. I've put the KeyPreview propery to true but this didnt solve it either. The timer does work properly.
I hope somebody here can tell me what I'm doing wrong.
This is a code example to move a PictureBox:
Drag/drop a PictureBox on a form (in this case Form1).
Change the Background color of the PictureBox to e.g. red. (This way it is visible during runtime).
Implement the KeyDown event of Form1 (in this case Form1_KeyDown).
Run the application.
Start pressing an arrow key and the PictureBox will move.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.Right) { x += 1; }
else if (e.KeyCode == Keys.Left) { x -= 1; }
else if (e.KeyCode == Keys.Up) { y -= 1; }
else if (e.KeyCode == Keys.Down) { y += 1; }
pictureBox1.Location = new Point(x, y);
}
Related
Currently, this code runs fine, and moves the picturebox, but it always moves it once, waits about a second, and then continues to move it. How do I make the movement keep going instead of stopping for a second?
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
x = pictureBox1.Location.X;
y = pictureBox1.Location.Y;
if (pictureBox1.Location.X > 0)
{
pictureBox1.Location = new Point(x - 10, y);
}
}
}
One way to achieve this outcome is to use a Timer and enable it on KeyDown event and disable it on KeyUp event. When MoveTimer.Tick occurs (in this case every 25 ms or so), handle the event by moving "once" to the left until the key is released.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
MoveTimer.Tick += (sender, e) =>
{
pictureBox1.Location = new Point(
pictureBox1.Location.X - 10,
pictureBox1.Location.Y);
};
}
Timer MoveTimer = new Timer { Interval = 25 };
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if(e.KeyData == Keys.A)
{
MoveTimer.Enabled = true;
}
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
MoveTimer.Enabled = false; // No need to check which key
}
}
In my mini app in C# I have a button that I want to enable and disable it by right click on it. i.e. when button is enabled by right click it turn to disable and when it is disable, right click on it change it's status to enable. Disabling of enabled button is easy and straightforward but enabling it by right click on it, is not possible; because the button is disabled and not event is sent to emaciate code.
How Can I do?
The mouse events of a disabled control are passed down to its Parent.
You can catch them there and test if the cursor is on the button.
Example:
if (button1.Bounds.Contains(e.Location)) button1.Enabled = true;
If you have several buttons you need to test them all..:
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
foreach (Control ctl in panel1.Controls)
{
if (ctl is Button && ctl.Bounds.Contains(e.Location))
ctl.Enabled = true;
}
}
If only the right mouse button is supposed to enable, add a test for it, maybe like so:
if (e.Button.HasFlag(MouseButtons.Right) &&
ctl is Button && ctl.Bounds.Contains(e.Location))
Right-clicking to enable and disable a button is not standard or recommended button behavior, so the API completely leaves out an easy implementation for it. Once the button is disabled, it no longer captures mouse events, but the form does.
public partial class Form1 : Form
{
private Boolean _button1IsRightClicked;
public Form1()
{
InitializeComponent();
_button1IsRightClicked = false;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi");
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
{
_button1IsRightClicked = true;
}
else
{
_button1IsRightClicked = false;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
{
_button1IsRightClicked = false;
enableDisableButton1(button1.Enabled);
}
else
{
_button1IsRightClicked = false;
}
}
private void enableDisableButton1(Boolean isEnabled)
{
if (isEnabled)
{
button1.Enabled = false;
isEnabled = false;
}else
{
button1.Enabled = true;
isEnabled = true;
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
_button1IsRightClicked = true;
}
else
{
_button1IsRightClicked = false;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && _button1IsRightClicked == true)
{
_button1IsRightClicked = false;
enableDisableButton1(button1.Enabled);
}
else
{
_button1IsRightClicked = false;
}
}
}
I have this code, which does not want to work! The point is to make a key viewer which shows the pressed cursor keys!
However, none of the keyUpPress method works.
bool debug = true;
void keyUpPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (debug == true)
{
MessageBox.Show("CURSOR UP", "Cursor!");
}
pictureBox1.Image = Properties.Resources.key_up_pressed;
pictureBox1.Refresh();
}
keyUpPress is an event that triggers after lifting a key (after holding it down).
You can't bind a eventHandler to a PictureBox, but you can bind it to the form (if it's a forms application). Example code (with Form1 as the name of the form):
private void Form1_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (debug == true)
{
MessageBox.Show("CURSOR UP", "Cursor!");
}
pictureBox1.Image = Properties.Resources.key_up_pressed;
pictureBox1.Refresh();
}
}
I have this Code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Left)
{
left = true;
right = false;
down = false;
}
}
private void timerMoving_Tick(object sender, EventArgs e)
{
if(Ghost.Bounds.IntersectsWith(panel3.Bounds))
{
timerMoving.Stop();
}
if(left)
{
Ghost.Left -= 5;
timerMoving.Start();
}
}
In the above code as my Ghost(which is moving down) hits the panel3 boundary it stops.
I want my timer to start again as i press Left,Right or up key and only stop when it's moving down,hitting panel3. I tried doing this:
if(left)
{
Ghost.Left -= 5;
timerMoving.Start();
}
but nothing happens, Why?
The simple (though not the optimal) way to achieve this goal pertinent to your task description is by adding the line shown in the following code snippet:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Left)
{
// add this line
if(!timerMoving.Enabled) timerMoving.Start();
left = true;
right = false;
down = false;
up = false;
}
}
and removing this line as shown below:
if (left)
{
Ghost.Left -= 5;
//timerMoving.Start(); - comment off
}
Hope this may help.
So I have KeyDown event that is not working correctly. The pacman appears on the map but does not respond to any key presses. There are no errors.
Here it is (Gameboard is name of the form, mapPictureBox the name of the picture box):
private void Gameboard_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
pacman.yPosition += 10;
}
else if (e.KeyCode == Keys.Up)
{
pacman.yPosition -= 10;
}
else if (e.KeyCode == Keys.Left)
{
pacman.xPosition -= 10;
}
else if (e.KeyCode == Keys.Right)
{
pacman.xPosition += 10;
}
mapPictureBox.Invalidate();
}
I have an instance of the MovingPacman class declared at the top(where I call images:
MovingPacman pacman = new MovingPacman();
Any ideas?
Set the KeyPreview property of your form to True. By default, edit controls consume key events, this property enable the form to handle events before controls do.
Check this