Resetting SendKeys message - c#

I created a program what will type a text for me automatically by using SendKeys command. When I press start button the text will be typed as it should, and when I press start button the text will stop from typing. The typing is done by using an interval timer which will decide when to start typing and have a brief space between typing 2 lines.
The problem is that when I start typing and type part of the message then press stop before program can type the entire message then start typing again, the message will continue to type from where it stopped. For example I want to type the message "123456789". I start typing then program types "1234" then press stop so program wont type no more. Then when I press start again program should start typing from 1, but instead my program types "56789".
How to reset the line when I stop, then start again? I tried to make the message as a "message" variable which is reset when I press stop button but it doesn't work.
This is how I set to type every interval tick:
private void Space(object sender, EventArgs e)
{
if (cbRandomLine.Checked || tickCount < lbMessage.Items.Count)
{
var index = cbRandomLine.Checked ? randomLine : tickCount;
var item = lbMessage.Items[index].ToString();
SendKeys.Send(item.Substring(currentChar++, 1));
if (currentChar == item.Length)
{
SendKeys.Send("{enter}");
tmrSpace.Enabled = false;
currentChar = 0;
}
}
tmrSpace.Interval = random.Next(10, 100);
}
private void Delay(object sender, EventArgs e)
{
if (delayCount == 0)
{
tmrDelay.Stop();
tmrInterval.Start();
lblDelay.Text = "Typing...";
}
else lblDelay.Text = "Typing in: " + delayCount;
delayCount--;
}
// METHODS
private void WhenStarted()
{
tickCount = 0;
delayCount = 2;
lbMessage.Enabled = false;
txtMessage.Enabled = false;
if (cbDelay.Checked)
{
lblDelay.Text = "Typing...";
tmrInterval.Enabled = true;
}
else
{
lblDelay.Text = "Typing in: 3";
tmrDelay.Enabled = true;
}
cbPause.Enabled = false;
cbDelay.Enabled = false;
cbRandomLine.Enabled = false;
btnStart.Enabled = false;
btnStop.Enabled = true;
btnStop.Focus();
}
private void WhenStopped()
{
lblDelay.Text = string.Empty;
whenStart = false;
tickCount = 0;
txtMessage.Text = string.Empty;
lbMessage.Enabled = true;
txtMessage.Enabled = true;
cbPause.Enabled = true;
cbDelay.Enabled = true;
cbRandomLine.Enabled = true;
btnStart.Enabled = true;
btnStop.Enabled = false;
btnStart.Focus();
tmrDelay.Enabled = false;
tmrInterval.Enabled = false;
tmrSpace.Enabled = false;
}
private void SetInterval()
{
if (nudPlusMinus.Value == 0)
{
tmrInterval.Interval = int.Parse(nudInterval.Value.ToString());
}
else
{
tmrInterval.Interval = random.Next(int.Parse(nudInterval.Value.ToString()) - int.Parse(nudPlusMinus.Value.ToString()), int.Parse(nudInterval.Value.ToString()) + int.Parse(nudPlusMinus.Value.ToString()));
}
}
private void ListBoxContentCheck()
{
if (lbMessage.Items.Count > 0)
{
btnStart.Enabled = true;
}
else
{
btnStart.Enabled = false;
}
}

You need to reset the currentChar variable.

Related

clearing pictureboxes from a list array

im trying to clear the picturebox that are created from a list in my game once it has ended, i have cleared the list but the picturebox still display i have tried using the below code in a timer that is enable upon the game ending
foreach (Invader ship in invaders)
{
ship.isDisposed = true;
ship.ship.Dispose();
}
this doesnt do anyhting tho so does anyone have any ideas how i could do this?
public partial class Form1 : Form
{
private List<Invader> invaders = new List<Invader>();
private List<Laser> lasers = new List<Laser>();
int invaderNumber = 0;
int score = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// moves the spacefighter up when clicked
if (e.KeyCode.Equals(Keys.W))
{
if (SpaceFighter.Top > 0)
{
SpaceFighter.Top = SpaceFighter.Top - 30;
}
}
// moves the spacefighter left when clicked
if (e.KeyCode.Equals(Keys.A))
{
if (SpaceFighter.Left > 0)
{
SpaceFighter.Left = SpaceFighter.Left - 10;
}
}
// moves the spacefighter right when clicked
if (e.KeyCode.Equals(Keys.D))
{
if (SpaceFighter.Right < this.Width)
{
SpaceFighter.Left = SpaceFighter.Left + 10;
}
}
// moves the spacefighter down when clicked
if (e.KeyCode.Equals(Keys.S))
{
if (SpaceFighter.Bottom < this.Height - 10)
{
SpaceFighter.Top = SpaceFighter.Top + 10;
}
}
// fires lasers when clicked
if (e.KeyCode.Equals(Keys.Space))
{
System.Media.SoundPlayer LaserSound = new System.Media.SoundPlayer(Properties.Resources.LaserSound);
LaserSound.Play();
this.lasers.Add(new Laser(this, SpaceFighter));
}
}
private void timer1_Tick(object sender, EventArgs e)
{
// introduces 10 enemies once the game starts
if (invaderNumber > 9 )
{
timer1.Enabled = false;
timer2.Enabled = true;
}
else
{
invaders.Add(new Invader(this));
invaderNumber++;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
// detects if the enemy ship interacts with the spacefighter and ends the game if this happens
invaders.RemoveAll(ship => ship.isDisposed);
foreach(Invader ship in invaders)
{
ship.MoveInvader(this);
if (SpaceFighter.Bounds.IntersectsWith(ship.ship.Bounds))
{
// plays sound for exploding ship
System.Media.SoundPlayer SpaceshipSound = new System.Media.SoundPlayer(Properties.Resources.SpaceshipSound);
SpaceshipSound.Play();
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = true;
invaders.Clear();
listBox1.Items.Add(lblScore.Text); // adds score to listbox
MessageBox.Show("You Lose!");
return;
}
}
// detects if an enemy ship his hit by a laser
lasers.RemoveAll(laser => laser.isDisposed);
foreach (Laser laser in lasers)
{
laser.MoveLaser(this);
foreach (Invader ship in invaders)
{
if (laser.laser.Bounds.IntersectsWith(ship.ship.Bounds))
{
laser.isDisposed = true;
laser.laser.Dispose();
ship.isDisposed = true;
ship.ship.Dispose(); // makes the ship dissappear once its shot
System.Media.SoundPlayer ShipSound = new System.Media.SoundPlayer(Properties.Resources.EnemySound); // sound for the enemy ship being destroyed
ShipSound.Play();
score = score + 2; //adds 2 points to players score if enemy is hit
lblScore.Text = score.ToString(); //updates the score label
invaderNumber = invaderNumber - 1;
}
}
}
foreach (Invader ship in invaders)
{
if (ship.ship.Top > 485)
{
ship.isDisposed = true;
ship.ship.Dispose();
invaderNumber = invaderNumber - 1;
}
}
}
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Enabled = true; // activates timer 1
timer3.Enabled = true; // activates timer 3
btnStart.Visible = false; // hidesthe start button
lblScore.Text = "0"; // updates score label to 0 for start of game
lblName.Text = txtName.Text; // updates the name label to user nput
txtName.Visible = false; // hides the textbox
lblEnterName.Visible = false; // hides the enter name label
SpaceFighter.Visible = true; // makes the spacefighter visible
}
// code for the countdown clock
int m = 2;
int s = 60;
private void timer3_Tick(object sender, EventArgs e)
{
if(s > 0)
{
s = s - 1;
lblTimer.Text = "0" + m.ToString() + ":" + s.ToString();
}
if(s == 0)
{
s = 59;
m = m - 1;
lblTimer.Text = "0" + m.ToString() + ":" + s.ToString();
}
if(s < 10)
{
s = s - 1;
lblTimer.Text = "0" + m.ToString() + ":" + "0" + s.ToString();
}
if (m < 0)
{
listBox1.Items.Add(lblScore.Text + " " + lblName.Text); // adds score to list box
timer4.Enabled = true;
invaders.Clear();
}
if (m >= 0)
{
timer1.Enabled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
SpaceFighter.Visible = false; // hides the space fighter until the player starts the game
listBox1.Visible = false; // keepsscore table hidden
lblScoreTable.Visible = false; // score table lables kept hidden
lblNameTable.Visible = false;
btnMenu.Visible = false;
}
private void Timer4_Tick(object sender, EventArgs e)
{
lblTimer.Text = "00:00"; // sets game timer to 00:00
timer3.Enabled = false; // disbales timer 3
listBox1.Visible = true; // makes score card visible
listBox1.Sorted = true;
lblNameTable.Visible = true; // displays score table labels
lblScoreTable.Visible = true;
btnMenu.Visible = true;
foreach (Invader ship in invaders)
{
ship.isDisposed = true;
ship.ship.Dispose();
}
}
private void BtnMenu_Click(object sender, EventArgs e)
{
// resets game to its original state in order to play another game
m = 2;
s = 60;
lblTimer.Text = "03:00";
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
timer4.Enabled = false;
listBox1.Visible = false;
lblNameTable.Visible = false;
lblScoreTable.Visible = false;
lblEnterName.Visible = true;
txtName.Visible = true;
SpaceFighter.Visible = false;
btnMenu.Visible = false;
btnStart.Visible = true;
score = 0;
lblScore.Text = "Score";
lblName.Text = "Name";
txtName.Clear();
invaderNumber = 0;
SpaceFighter.Top = 380;
SpaceFighter.Left = 400;
}
}
this would do:
foreach (var control in this.Controls)
{
var pb = control as PictureBox;
if (pb != null)
{
if (pb.Name != "SpaceFighter")
{
pb.Dispose();
this.Controls.Remove(pb);
}
}
}
for the listbox sorting:
using System.Collections;
ArrayList Sorting = new ArrayList();
foreach (var o in listBox1.Items)
{
Sorting.Add(o);
}
Sorting.Sort();
Sorting.Reverse();
listBox1.Items.Clear();
foreach (var o in Sorting)
{
listBox1.Items.Add(o);
}

Wpf Button IsEnabled not working

i have a method that enabling and disabling button in here.My if-else block should do when enter a number to lbDivide the 'öde', '0' and '00' buttons should be active but only activing öde button.How do i solve this ?
öde = make payment
Kişi Sayısı = How many person?
private void Bol_Click(object sender, RoutedEventArgs e)
{
lbDivide.Text = "0";
btnBol.Opacity = 0.5;
btnBol.IsEnabled = false;
lbPayment.Visibility = Visibility.Hidden;
if (lbDivide.Text == "0")
{
btnQr.Opacity = 0.5;
btnQr.IsEnabled = false;
zero.Opacity = 0.2;
zero.IsEnabled = false;
double_zero.IsEnabled = false;
double_zero.Opacity = 0.2;
}
else
{
btnQr.Opacity = 1;
btnQr.IsEnabled = true;
zero.Opacity = 1;
double_zero.Opacity = 1;
zero.IsEnabled = true;
double_zero.IsEnabled = true;
}
I think I know where the error is.
private void Bol_Click(object sender, RoutedEventArgs e)
{
lbDivide.Text = "0"; /// in this line of code you're basically setting lbDivide.text to be 0 every time the button is clicked, so the else condition will never be met.
btnBol.Opacity = 0.5;
btnBol.IsEnabled = false; /// you're basically disabling the button after the first click.
lbPayment.Visibility = Visibility.Hidden;
if (lbDivide.Text == "0")
{
btnQr.Opacity = 0.5;
btnQr.IsEnabled = false;
zero.Opacity = 0.2;
zero.IsEnabled = false;
double_zero.IsEnabled = false;
double_zero.Opacity = 0.2;
}
else
{
btnQr.Opacity = 1;
btnQr.IsEnabled = true;
zero.Opacity = 1;
double_zero.Opacity = 1;
zero.IsEnabled = true;
double_zero.IsEnabled = true;
}
}
change if(lbDivide.Text == "0") by if(lbDivide.Text.Equals("0"))

How would i create an array of images and then load each image into successive indexes

As you can see I've created an array of images, but I'm not sure how to load each image into successive indexes.
Someone told me to do this for my game but I'm not sure how or if it will fix my problem
I've made a game where the character walks left then up then down then right and a few timers that load the animation and handle the movement but when I draw using this code
*e.Graphics.DrawImage(Properties.Resources.Corn_Cobs, 70, 70, 40, 40);*
My character gets really laggy/slow but when I don't draw the image it works smoothly and the character speeds up like normal.
Here is the code:
namespace Rice_Boy_Tester_2
{
public partial class Form1 : Form
{
bool iggy = false;
bool left2 = false;
bool right2 = false;
bool Up2 = false;
bool Down2 = false;
bool Check2 = false;
bool left = false;
bool right = false;
bool Up = false;
bool Down = false;
bool Check = false;
Image[] Animations;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Animations = new Image[6];
Animations[0] = Image.FromFile("Rice-Boy-Walking-Left-Bouncing.gif");
Animations[1] = Image.FromFile("Rice-Boy-Walking-Up-Bouncing.gif");
Animations[2] = Image.FromFile("Rice-Boy-Walking-Right-Bouncing.gif");
Animations[3] = Image.FromFile("Rice-Boy-Walking-Down.gif");
Animations[4] = Properties.Resources.Rice_Boy_Standing_Left;
Animations[5] = Properties.Resources.Corn_Cobs;
}
private void Refresh_Tick(object sender, EventArgs e)
{
this.Refresh();
}
private void PriceBoyWalk_Tick(object sender, EventArgs e)
{
if (left)//Goes Left
{
Player.Left -= 1;
}
if (Player.Left < 170 & Check == false)//checks how far away player is from form
{
left = false;
Up = true;
}
if (Up & Player.Left < 170) //Goes Up
{
Player.Top -= 1;
Check = true;
}
if (Player.Top < 100 & Check)
{
Up = false;
Down = true;
}
if (right)//Goes Right
{
Player.Left += 1;
}
if (Down)//Goes Down
{
Player.Top += 1;
}
if (Player.Top + 150 > this.ClientSize.Height)// When RiceBoy goes down and hits bottom right = true
{
Check = false;
Down = false;
right = true;
}
if (Player.Left + 150 > this.ClientSize.Width)//Stops At Starting Point
{
right = false;
}
}
private void B1_Click(object sender, EventArgs e)
{
this.Paint += new PaintEventHandler(form1_Pad1_Corn);
RiceBoyWalkGif.Enabled = true;
left = true;
left2 = true;
RiceBoyWalk.Enabled = true;}
}
private void form1_Pad1_Corn(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawImage(Properties.Resources.Corn_Cobs, 70, 70, 400, 400);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (left2)
{
Player.Image = Animations[0];
left2 = false;
}
if (Player.Left < 170 & Check2 == false)//checks how far away player is from form
{
left2 = false;
Up2 = true;
}
if (Up2 & Player.Left < 170) //Goes Up
{
this.Player.Size = new System.Drawing.Size(36, 76); // Changes size of the picture box to maintain quaility
Player.Image = Animations[1];//Animates RiceBoyWalkingUp
Check2 = true;
Up2 = false;
}
if (Player.Top < 105 & Check2)//Player.Top < 101 must be +1 greater than the RiceBoyWalkTimer
{
Up2 = false;
Down2 = true;
}
if (right2)
{
this.Player.Size = new System.Drawing.Size(53, 77); // Changes size of the picture box to maintain quaility
Player.Image = Animations[2];//Animates RiceBoyWalkingRight
right2 = false;
}
if (Down2)//Goes Down
{
Player.Image = Animations[3];//Animates RiceBoyWalkingDown
Down2 = false;
}
if (Player.Top + 150 > this.ClientSize.Height)// When RiceBoy goes down and hits bottom riceboy walks right
{
iggy = true;// shows that riceboy is approching the starting point
Check2 = false;
Down2 = false;
right2 = true;
}
if (Player.Left + 150 > this.ClientSize.Width & iggy)//Stops At Starting Point
{
right2 = false;
Player.Image = Animations[4];// Rice boy standing left
}

String value not changing

Hi, I have code that reads and writes to a COM port. When the program reads from the COM port it searches for a string value and puts it in a variable. After it does this, it again listens to the COM port. I need to write to the COM port and read some new data, but I'm not seeing the value has changing to a new value.
Here is my code:
private void timer1_Tick(object sender, EventArgs e)
{
sq = "777";
if (CommunicationManager.myQ.Count != 0)
{
sq = CommunicationManager.myQ.Dequeue().ToString();
textBox1.Text = sq + textBox1.Text;
buffer = Regex.Match(textBox1.Text, #"\
((.+?)\,15,").Groups[1].Value;
}
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= numtst; i++)
{
listView1.Items[i].BackColor = Color.White;
fl[i] = false;
}
nt = 0;
flon = false;
flag[0] = false;
comm.WriteData("AT\r\n");
wait(700);
if (buffer.lenght == 16)
{
flag[0] = true
}
if (flag[0] == true)
{
flon = true;
CommunicationManager.myQ.Clear();
break;
}
}
if (flon == true)
{
listView1.Items[nt].BackColor = Color.LightGreen;
fl[nt] = true;
}
else
{
listView1.Items[nt].BackColor = Color.Red;
if (flag[76] == true)
{
button1.Enabled = true;
button1.BackColor = Color.Red;
button1.Text = "Test ERROR";
return;
}
}
comm.WriteData("ATT\r\n");
wait(3700);
comm.WriteData("AT4\r\n");
nt = 1;
flon = false;
flag[1] = false;
if (buffer == text4.text)
{
flag[1] = true
}
wait(700);
if (flag[1] == true)
{
flon = true;
CommunicationManager.myQ.Clear();
break;
}
if (flag[76] == true)
{
button1.Enabled = true;
return;
}
}
if (flon == true)
{
listView1.Items[nt].BackColor = Color.LightGreen;
fl[nt] = true;
}
else
{
listView1.Items[nt].BackColor = Color.Red;
if (flag[76] == true)
{
button1.Enabled = true;
button1.BackColor = Color.Red;
button1.Text = "Test ERROR";
return;
}
}
in the second part if (buffer == text4.text) i see only first value of the buffer variable.
i checked in the terminal and all commands working good.

C# Dynamically assign and update form during a method execution

How can I dynamically, during method's loops still working, assign values and make them update on the form? What happens in my program is, that when launched it hangs until it finishes (finds the solution to the sudoku) and then populates the Textboxes.
Here is the main method:
bool SolveSudoku()
{
if (!FindUnassignedLocation()) return true;
for (int num = 1; num <= 9; num++)
{
if (NoConflicts(emptyRow, emptyCol, num))
{
Grid[emptyRow, emptyCol].Text = num.ToString();
Grid[emptyRow, emptyCol].BackColor = Color.White;
Grid[emptyRow, emptyCol].ForeColor = Color.Black;
if (SolveSudoku()) return true;
Grid[emptyRow, emptyCol].Text = "";
}
}
bool checkIfFirstBackTrack = true;
do {
if (checkIfFirstBackTrack)
{
backtrackCounter++;
checkIfFirstBackTrack = false;
}
if (emptyCol == 0 && emptyRow > 0)
{
emptyCol = 8;
emptyRow--;
}
else if (emptyCol > 0)
{
emptyCol--;
}
}
while(Preset[emptyRow, emptyCol] != "");
return false;
}
}
One option would be a background worker See more information on setting up a background worker here.:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
//// your code here
}
}
}

Categories