Trying to move picture control on timer - c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Game2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void Move_Ship(int X_Coord ,int Y_Coord, string Positioning)
{
if(Positioning == "Rechts")
{
X_Coord += 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
if (Positioning == "Links")
{
X_Coord -= 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
if (Positioning == "Up")
{
Y_Coord -= 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
if (Positioning == "Down")
{
Y_Coord += 5;
this.pictureBox1.Location = new Point(X_Coord, Y_Coord);
};
}
private void timer1_Tick(object sender, EventArgs e)
{
if (Control.ModifierKeys == Keys.Up)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Up");
};
if (Control.ModifierKeys == Keys.Down)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Down");
};
if (Control.ModifierKeys == Keys.Left)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Links");
};
if (Control.ModifierKeys == Keys.Right)
{
Move_Ship(pictureBox1.Location.X, pictureBox1.Location.Y, "Rechts");
};
}
}
}
If I did this right my piture should be moving the but it doesn't do anything at all.
Did I do something wrong and if so please tell me?

One possible problem is usage of Control.ModifierKeys
Gets a value indicating which of the modifier keys (SHIFT, CTRL, and ALT) is in a pressed state.
If you want to use direction keys - listen for keyDown event and save direction there. Sample and details Control.KeyDown:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode == Keys.Down)
{
direction = "Down";
}

Related

Move picturebox with keyboard

I am fairly new to c# and I want to make a picture box move when I press the WASD keys, but the picture box refuses to move. The picture box is not docked, locked or anchored. This is my 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 Imagebox_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) x -= 1;
else if (e.KeyCode == Keys.S) x += 1;
pictureBox1.Location = new Point(x, y);
}
}
}
I have no idea what's going on! thanks for the help!
There are 2 issues with your code:
Set the form's KeyPreview property to true otherwise it the PictureBox will get the KeyDown event. This was preventing Form1_KeyDown from being called.
This code block has a subtle bug:
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) x -= 1;
else if (e.KeyCode == Keys.S) x += 1;
If you look closely, you're only modifying the x coordinate.
All together:
public Form1()
{
InitializeComponent();
// Set these 2 properties in the designer, not here.
this.KeyPreview = true;
this.KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) y -= 1;
else if (e.KeyCode == Keys.S) y += 1;
pictureBox1.Location = new Point(x, y);
}
You need to set the form's KyePreview property to true otherwise form will not take that key down event.
Secondly you are only changing x value and not y value.
The whole completed code is
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 Imagebox_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
if (e.KeyCode == Keys.D) x += 1;
else if (e.KeyCode == Keys.A) x -= 1;
else if (e.KeyCode == Keys.W) y -= 1;
else if (e.KeyCode == Keys.S) y += 1;
pictureBox1.Location = new Point(x, y);
}
}
}

Using data from arduino in winforms

using System;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
namespace ek_zıplama
{
public partial class Form1 : Form
{
public enum Directions
{
right,
left,
up,
}
private Directions car_direction;
public SerialPort myPort;
int G = 15;
int force;
bool jump;
public string DATA;
public Form1()
{
InitializeComponent();
myPort = new SerialPort();
myPort.BaudRate = 9600;
myPort.PortName = "COM6";
myPort.Open();
jump = false;
}
private void Form1_Load(object sender, EventArgs e)
{
moves();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (jump)
{
car.Top -= force;
force -= 1;
}
//using block to stay in same position when car is stopped
if (car.Left + car.Width - 1 > block.Left && car.Left + car.Width + 5 < block.Left + block.Width + car.Width
&& car.Top + car.Height >= block.Top && car.Top < block.Top)
{
car.Top = ekran.Height - block.Height - car.Height;
force = 0;
jump = false;
}
}
private void moves()
{
if (label2.Text == "10111" && car_direction != Directions.right)
{
car.Location = new Point(car.Location.X + 130, car.Location.Y);
car_direction = Directions.right;
}
if (label2.Text == "01111" && car_direction != Directions.left)
{
car.Location = new Point(car.Location.X - 130, car.Location.Y);
car_direction = Directions.left;
}
if (!jump && label2.Text == "11011")
{
jump = true;
force = G;
}
}
private void timer3_Tick(object sender, EventArgs e)
{
DATA = myPort.ReadExisting();
label2.Text = DATA;
}
}
}
What I botch about that in move function.
I tried to create a function like
*if DATA is 01111 then my car turns left
*if DATA is 10111 then my car turns right
*if DATA is 11011 then my car jumps
Before I changed,car was controlled with keyboards.Everything was same but "move"function.And it was working.What were instead od "move" function is:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right&& car_direction != Directions.right)
{
car.Location = new Point(car.Location.X + 130, car.Location.Y);
car_direction = Directions.right;
}
if (e.KeyCode == Keys.Left && car_direction != Directions.left)
{
car.Location = new Point(car.Location.X - 130, car.Location.Y);
car_direction = Directions.left;
}
if (!jump && e.KeyCode == Keys.Up)
{
jump = true;
force = G;
}
}
Hope you get me :)
I'm quite sure it's not going to work if you only call it once in your Form_Load event. Since you maybe want your car to move each time the data in the label has changed, you should just make a reference to the function you created. That means, that you should just put moves(); in private void timer3_Tick(object sender, EventArgs e):
private void timer3_Tick(object sender, EventArgs e)
{
DATA = myPort.ReadExisting();
label2.Text = DATA;
moves();
}
In this way, each time the timer ticks, your car is going to move according to the new data in the label.
When I was doing something with Arduino I used something like this:
private void timer_Tick(object sender, EventArgs e)
{
if (serialPort != null && serialPort.IsOpen && serialPort.BytesToRead > 0)
{
data_as_string += serialPort.ReadLine();
}
}
I'm not sure how DATA is sent, but you can send single lines from Arduino using Serial.write("your line here");. Receiving and parsing data from here is very simple, as you could just read lines 01111, 10111, etc. and use any logic to manipulate the car. I hope this helps.

Windows.Forms.Timer not working properly

I am posting a sample code to decode a problem with a larger code.
I have a 2 seconds timer - System.Windows.Forms.Timer
Every 2 seconds i increment a global int by one, and show its value using MessageBox
If the int reaches 4, i turn off the main flag.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool Play_On = false;
int i = 0;
public Form1()
{
InitializeComponent();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Play")
{
button1.Text = "Puase";
Play_On = true;
}
else
{
button1.Text = "Play";
Play_On = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
Timer MinResTimer = new Timer();
{
MinResTimer.Tick += new EventHandler(MinResTimer_Elapsed);
MinResTimer.Interval = 2000;
MinResTimer.Enabled = true;
MinResTimer.Start();
}
}
public void MinResTimer_Elapsed(object sender, EventArgs e)
{
if (Play_On == true)
{
MessageBox.Show("timed"+ i.ToString());
i++;
}
if (i == 4)
{
Play_On = false;
button1.Text = "Play";
}
}
}
}
The problem is that the flag isnt being turned off at all.
The output I am getting is Timed0 - In a messagebox, many times.
I think I have some problem with the messagebox - not sure
Could someone help me find out whats going on here?
Move your increment of i++ to before you call MessageBox
Then you will see that you really want to protect the event from still firing while the message box is displayed.
so your new code would move the I++ to before the MessageBox and then you would disable and enable the timer before and after the message box is called.
When the messagebox show,then you didn't click the 'OK' button to close the messagebox, i++ can not do.So i always equals 0 before you click the 'OK' button on messagebox.
void MinResTimer_Elapsed(object sender, EventArgs e)
{
if (Play_On == true)
{
i++;
MessageBox.Show("timed" + i.ToString());
}
if (i == 4)
{
Play_On = false;
MinResTimer.Enabled = false;
button1.Text = "Pause";
}
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Play")
{
button1.Text = "Puase";
MinResTimer.Enabled = true;
i = 0;
Play_On = true;
}
else
{
button1.Text = "Play";
MinResTimer.Enabled = false;
Play_On = false;
}
}
EDIT #1:
bool Play_On = false;
int i = 0;
Timer MinResTimer;
private void Form1_Load(object sender, EventArgs e)
{
MinResTimer = new Timer();
{
MinResTimer.Tick += new EventHandler(MinResTimer_Elapsed);
MinResTimer.Interval = 2000;
MinResTimer.Enabled = true;
MinResTimer.Start();
}
}
void MinResTimer_Elapsed(object sender, EventArgs e)
{
if (Play_On == true && i <= 4)
{
i++;
MessageBox.Show("timed" + i.ToString());
}
if (i == 4)
{
Play_On = false;
button1.Text = "Pause";
}
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "Play")
{
button1.Text = "Puase";
Play_On = true;
}
else
{
button1.Text = "Play";
Play_On = false;
}
}

C# KeyEventArgs giving out wrong outputs when caps is on

I'm using global keyboard hooks as a keyboard listener when the window is out of focus. I'm half way through some code and I realise that for whatever reason, putting caps lock on changes the output of KeyEventArgs. Using shift/no shift outputs ordinary results but if I were to put caps lock on, A will become U, B will become V, C will become W and so on. Here's a look at the code that does this (will add global keyboard hooks if necessary):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;
namespace WindowsFormsApplication1
{
public partial class Form6 : Form
{
globalKeyboardHook gkh = new globalKeyboardHook();
int sign_Indicator = 0;
double variable1;
double variable2;
int addBit = 0;
int subBit = 0;
int multBit = 0;
int divBit = 0;
int modBit = 0;
Boolean fl = false;
String s, x;
public Form6()
{
InitializeComponent();
}
private void Form6_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.A);
gkh.HookedKeys.Add(Keys.B);
gkh.HookedKeys.Add(Keys.C);
gkh.HookedKeys.Add(Keys.D);
gkh.HookedKeys.Add(Keys.E);
gkh.HookedKeys.Add(Keys.F);
gkh.HookedKeys.Add(Keys.G);
gkh.HookedKeys.Add(Keys.H);
gkh.HookedKeys.Add(Keys.I);
gkh.HookedKeys.Add(Keys.J);
gkh.HookedKeys.Add(Keys.K);
gkh.HookedKeys.Add(Keys.L);
gkh.HookedKeys.Add(Keys.M);
gkh.HookedKeys.Add(Keys.N);
gkh.HookedKeys.Add(Keys.O);
gkh.HookedKeys.Add(Keys.P);
gkh.HookedKeys.Add(Keys.Q);
gkh.HookedKeys.Add(Keys.R);
gkh.HookedKeys.Add(Keys.S);
gkh.HookedKeys.Add(Keys.T);
gkh.HookedKeys.Add(Keys.U);
gkh.HookedKeys.Add(Keys.V);
gkh.HookedKeys.Add(Keys.W);
gkh.HookedKeys.Add(Keys.X);
gkh.HookedKeys.Add(Keys.Y);
gkh.HookedKeys.Add(Keys.Z);
gkh.HookedKeys.Add(Keys.D1);
gkh.HookedKeys.Add(Keys.D2);
gkh.HookedKeys.Add(Keys.D3);
gkh.HookedKeys.Add(Keys.D4);
gkh.HookedKeys.Add(Keys.D5);
gkh.HookedKeys.Add(Keys.D6);
gkh.HookedKeys.Add(Keys.D7);
gkh.HookedKeys.Add(Keys.D8);
gkh.HookedKeys.Add(Keys.D9);
gkh.HookedKeys.Add(Keys.D0);
gkh.HookedKeys.Add(Keys.Return);
gkh.HookedKeys.Add(Keys.Shift);
gkh.HookedKeys.Add(Keys.CapsLock);
gkh.HookedKeys.Add(Keys.Back);
gkh.HookedKeys.Add(Keys.Escape);
gkh.HookedKeys.Add(Keys.Space);
gkh.HookedKeys.Add(Keys.OemPeriod);
gkh.HookedKeys.Add(Keys.Oemcomma);
gkh.HookedKeys.Add(Keys.OemMinus);
gkh.HookedKeys.Add(Keys.Oemplus);
gkh.HookedKeys.Add(Keys.OemPipe);
gkh.HookedKeys.Add(Keys.OemBackslash);
gkh.HookedKeys.Add(Keys.OemOpenBrackets);
gkh.HookedKeys.Add(Keys.OemCloseBrackets);
gkh.HookedKeys.Add(Keys.OemQuotes);
gkh.HookedKeys.Add(Keys.OemSemicolon);
gkh.HookedKeys.Add(Keys.Oemtilde);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
}
public static string location = "C:\\Users\\Hayden\\Desktop\\log.txt";
void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Shift)
{
if (e.KeyCode == Keys.OemPipe)
{
System.IO.File.AppendAllText(#location, "|");
}
else
{
System.IO.File.AppendAllText(#location, e.KeyCode.ToString());
}
}
else
{
if (e.KeyCode == Keys.Return)
{
System.IO.File.AppendAllText(#location, "[RETURN]");
}
else if (e.KeyCode == Keys.Back)
{
System.IO.File.AppendAllText(#location, "[BACKSPACE]");
}
else if (e.KeyCode == Keys.Space)
{
System.IO.File.AppendAllText(#location, " ");
}
else if (e.KeyCode == Keys.OemPeriod)
{
System.IO.File.AppendAllText(#location, ".");
}
else if (e.KeyCode == Keys.Oemcomma)
{
System.IO.File.AppendAllText(#location, ",");
}
else if (e.KeyCode == Keys.OemPipe)
{
System.IO.File.AppendAllText(#location, "\\");
}
else if (e.KeyCode == Keys.CapsLock)
{
System.IO.File.AppendAllText(#location, "");
}
else if (Control.IsKeyLocked(Keys.CapsLock))
{
System.IO.File.AppendAllText(#location, e.KeyCode.ToString());
}
else
{
System.IO.File.AppendAllText(#location, e.KeyCode.ToString().ToLower());
}
}
}
How can I fix this? I have no idea why it's changing the letter completely just because I put caps lock on. Shift works fine though.
The globalKeyboardHook class you link to has a AddModifiers(Keys key) method with this line:
if ((GetKeyState(VK_CAPITAL) & 0x0001) != 0) key = key | Keys.CapsLock;
This code checks if Cap Lock is on and if so it ORs the key variable which is later returned by the method.
The problem is that Caps Lock is not a modifier key and shouldn't be manipulated this way. This is the reason you're seeing the strange key values you are when Caps Lock in on.
Incidentally, only Control, Shift and Alt are modifier keys and AddModifiers() handles these.
If you remove this line I believe you'll be fine.

Caller ID Check if Caller has ended Call

I have a program that gets the incoming number, date and time. I want to check however if the person who is ringing me has put the phone down, how can I do this?
Below is the code which I currently have:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace CallerID
{
public partial class CallerID : Form
{
int timesTicked = 0;
Point defaultLocation = new Point();
Point newLocation = new Point();
public CallerID()
{
InitializeComponent();
port.Open();
SetModem(); // SetModem(); originally went after WatchModem();
WatchModem();
//SetModem();
telephoneTimer.Interval = 16;
telephoneTimer.Tick += new EventHandler(telephoneTimer_Tick);
defaultLocation = pictureBox1.Location;
newLocation = pictureBox1.Location;
}
void telephoneTimer_Tick(object sender, EventArgs e)
{
if (timesTicked <= 2)
newLocation.X++;
if (timesTicked >= 4)
newLocation.X--;
if (timesTicked == 6)
{
timesTicked = 0;
pictureBox1.Location = defaultLocation;
newLocation = defaultLocation;
}
pictureBox1.Location = newLocation;
timesTicked++;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
WatchModem();
}
private SerialPort port = new SerialPort("COM3");
string CallName;
string CallNumber;
string ReadData;
private void SetModem()
{
port.WriteLine("AT+VCID=1\n");
//port.WriteLine("AT+VCID=1");
port.RtsEnable = true;
}
private void WatchModem()
{
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
public delegate void SetCallerIdText();
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ReadData = port.ReadExisting();
//Add code to split up/decode the incoming data
//if (lblCallerIDTitle.InvokeRequired)
if (ReadData.Contains("NMBR"))
{
lblData.Invoke(new SetCallerIdText(() => lblData.Text = ReadData));
}
//else
// lblCallerIDTitle.Text = ReadData;
}
private void lblData_TextChanged(object sender, EventArgs e)
{
telephoneTimer.Start();
button1.Visible = true;
}
}
}
Please ignore the Timer Code as that is just for animation.
Have you tried the PinChanged event? Normally Carrier Detect will go low when the remote end disconnects.

Categories