I'm just trying to create a program that presses Numpad 0 in the program, every few set seconds. This is what I have so far.
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
int enabled = 0;
public Form1()
{
InitializeComponent();
}
private void Start_Click(object sender, EventArgs e)
{
if (enabled == 0)
{
Start.Text = "Stop";
Timer.Enabled = true;
enabled = 1;
}
else
{
Start.Text = "Start";
Timer.Enabled = false;
enabled = 0;
label2.Text = "0";
}
}
private void Timer_Tick(object sender, EventArgs e)
{
SendKeys.Send("{NumpadIns}");
label2.Text = Convert.ToString(Convert.ToInt32(label2.Text) + 1);
}
}
}
I have tried this so far
{NumpadIns}
{NumIns}
{Num0}
{INS}
Nothing seems to work, the program I'm using with it is bound to Num Zero so it has to be Num Zero and not 0 on the top row, or Insert. Thanks (Yes I have googled but for some reason this is really hard to find).
From my experience, you should you some wrappers like Input Simulator. It is easy to use and have many pre-defined enums so you do not need to pass String argument.
Related
I am trying to make an If Statement, which when button1 is clicked will be showed in label1, when textbox1 is 25 or above, "Customer can receive £5 off purchase" and will show when 50 or above, "Customer can receive £10 off purchase"
My Code is as followed: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 If_Statement
{
public partial class Form1 : Form
{
int numbera = 25;
int numberb = 50;
public Form1()
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text <= numbera)
{
label1.Text = ("Customer can receive £5 off purchase");
}
if (textBox1.Text <= numberb)
{
label1.Text = ("Customer can receive £10 off purchase");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
Wondering where I am going wrong and if I can have an explanation of why and how to fix it.
Thanks in advance.
You need to convert the value entered to a number before comparing it:
private void button1_Click(object sender, EventArgs e)
{
var number = Double.Parse(textBox1.Text);
if (number <= numbera)
{
label1.Text = ("Customer can receive £5 off purchase");
}
else if (number <= numberb)
{
label1.Text = ("Customer can receive £10 off purchase");
}
}
You should notice that code will break whenever the user enters something like "abc", as that can't be parsed as number, so you'll need to work with a more safer way to validate the user input like Double.TryParse.
I am making a very small RPG game in C# to practice some skill (and to have fun!). I have gotten pretty far with the images, buttons and such.
My issue is that I am being thrown an error when trying to convert my label strings into integers to be compared for my attackingPhase() method.
Here is my code and a screenshot of the error.
I believe my code is correct but I can not figure out as to why the error is being thrown.
Thank you for all help.
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 RPG
{
public partial class Form2 : Form
{
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
pictureBox1.Image = mainForm.MyPictureBoxEnemy.Image;
pictureBox2.Image = mainForm.MyPictureBoxHero.Image;
lbl_Health_Value_Enemy.Text = "100";
lbl_Health_Value_Hero.Text = "100";
}
public void attackingPhase()
{
Random rnd = new Random();
int enemy_damage = rnd.Next(1, 25);
int hero_damage = rnd.Next(2, 15);
var enemyHealth = Convert.ToInt32(lbl_Health_Value_Enemy);
var heroHealth = Convert.ToInt32(lbl_Health_Value_Hero);
if((enemyHealth & heroHealth) > 0)
{
enemyHealth = enemyHealth - enemy_damage;
heroHealth = heroHealth - hero_damage;
} else
{
MessageBox.Show("DEAD");
}
lbl_Health_Value_Enemy.Text = enemyHealth.ToString();
lbl_Health_Value_Hero.Text = heroHealth.ToString();
}
private void btnAttack_Click(object sender, EventArgs e)
{
attackingPhase();
}
}
}
You need to convert the Text property of a label,
var enemyHealth = Convert.ToInt32(lbl_Health_value_Enemy.Text);
var heroHealth = Convert.ToInt32(lbl_Health_Value_Hero.Text);
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 P2_7_24_2016_ED_app
{
public partial class Form1 : Form
{
int win;
int loss;
int winCounter;
int lossCounter;
public Form1()
{
InitializeComponent();
}
private void buttonWin_Click(object sender, EventArgs e)
{
this.textBoxWin.Text = "";
++win;
++winCounter;
this.textBoxWin.Text = win.ToString();
}
private void buttonLoss_Click(object sender, EventArgs e)
{
this.textBoxLoss.Text = "";
++loss;
++lossCounter;
this.textBoxLoss.Text = loss.ToString();
}
private void buttonRate_Click(object sender, EventArgs e)
{
textBox1.Text = "";
double result = winCounter / (winCounter + lossCounter);
textBox1.Text = result.ToString();
}
}
}
This is the code in windows form application, its short so i'm not going to explain everything.
Problem is that in the end textBox1.Text = result.ToString();
is showing "0", but it should be some numbers. When the win winCounter is like 1, and lossCounter is 1 it should insert 1/(1+1) = 0.5 but it says "0". I tried everything i knew so please give me a tip.
This is integer arithmetic:
(winCounter/(winCounter+lossCounter))
The result is not going to be what you expect.
Cast either winCounter or (winCounter+lossCounter) to a floating point type (float, double or decimal depending on the speed/accuracy you need) before doing the division:
(winCounter/(decimal)(winCounter+lossCounter))
or
((double)winCounter/(winCounter+lossCounter))
and you'll get the result you expect.
As described in the comments, you will need to cast your result. This should work:
textBox1.Text = (winCounter/(double)(winCounter+lossCounter)).ToString();
I am trying to create a program that shuts down the computer after the entered amount of time (hours, minutes and seconds). Everything works except it doesn't update richTextBox1 at all. Please could someone help with this issue. Thanks in advance.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace ShutdownPC
{
public partial class Form1 : Form
{
public int inputHours = 0;
public int inputMinutes = 0;
public int inputSeconds = 0;
public int totalSeconds = 0;
public int totalMilliseconds = 0;
public int ticks = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
button1.Enabled = false;
// Hours
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
inputHours = int.Parse(textBox1.Text);
}
// Minutes
if (!string.IsNullOrWhiteSpace(textBox2.Text))
{
inputMinutes = int.Parse(textBox2.Text);
}
// Seconds
if (!string.IsNullOrWhiteSpace(textBox3.Text))
{
inputSeconds = int.Parse(textBox3.Text);
}
// Updating richTextBox1 every second with remaining time left
totalSeconds = (inputHours * 3600) + (inputMinutes * 60) + inputSeconds;
timer1.Start();
while (ticks < totalSeconds)
{
TimeSpan time = TimeSpan.FromSeconds(totalSeconds);
string timeOutput = time.ToString(#"hh\:mm\:ss");
richTextBox1.AppendText(String.Format(timeOutput));
Thread.Sleep(1000);
richTextBox1.Clear();
}
// Shutting down computer
totalMilliseconds = ((inputHours * 3600) + (inputMinutes * 60) + inputSeconds) * 1000;
Thread.Sleep(totalMilliseconds);
richTextBox1.AppendText("end");
//Process.Start("shutdown", "/s /t 0");
}
private void timer1_Tick(object sender, EventArgs e)
{
ticks++;
}
}
}
As SLaks told, if you block the UI thread the box will never be updated, but in .net you have await/async, just convert your method to private async void button1_Click and instead of Thread.Sleep use await Task.Delay.
Also, you set the box content with
TimeSpan time = TimeSpan.FromSeconds(totalSeconds);
richTextBox1.AppendText(String.Format(timeOutput));
This should be
TimeSpan time = TimeSpan.FromSeconds(totalSeconds - ticks);
richTextBox1.AppendText(String.Format(timeOutput));
If you want a count-down.
This SHOULD work because, despite being declared as the last private double, the modularization of C# should allow the first CalcTotalChargs be recognized as well. This is preventing me from running the program successfully
Here is the code I have so far:
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 Adam_Zeidan_HW7CH6_6_Hospital_Stay
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void calculateButton_Click(object sender, EventArgs e)
{
*label6.Text = "You will be paying: " + **CalcTotalChargs()**.ToString("c");*
}
private int CalcStayCharges()
{
return (350 * int.Parse(textBox1.Text)); // Calculating the amount of days by $350
}
private double CalcMiscCharges()
{
return double.Parse(textBox2.Text) + double.Parse(textBox3.Text) +
double.Parse(textBox5.Text) + double.Parse(textBox5.Text); // Adding together the other values entered within the textboxes to add to the eventual total charge
}
private double CalcTotalCharges()
{
return CalcMiscCharges() + CalcStayCharges(); // Adding the number value of the sum of the previous calculation to the sum of the 350 * Number of days staying
}
}
}
Your function spelt incorrectly, as such it couldn't complete.
CalcTotalChargs().ToString("c") should be CalcTotalCharges().ToString("c")
Use the code below and the issue should be resolved.
private void calculateButton_Click(object sender, EventArgs e)
{
label6.Text = "You will be paying: " + CalcTotalCharges().ToString("c");
}