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();
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);
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");
}
I've trying to build a simple tip calculator and get and EXACT value of what the tip should be. I've finished most of it but I'm struggling with finding the exact two numbers after the decimal place because it keeps rounding them off. Can anyone 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 project01LEA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Lyndsee Armstrong\nCSIS 1400\nProject #1");
}
private void textBox1_Leave(object sender, EventArgs e)
{
//define variables
const double POOR = 0.10;
const double AVERAGE = 0.15;
const double EXCELLENT = 0.20;
double mealAmount = Convert.ToDouble(textBox1.Text);
double doubledPoor = Convert.ToInt32(POOR * mealAmount);
double doubledAverage = Convert.ToInt32(AVERAGE * mealAmount);
double doubledExcellent = Convert.ToInt32(EXCELLENT * mealAmount);
string outStr = string.Format("{0:C2}", doubledPoor);
string outStr1 = string.Format("{0:C2}", doubledAverage);
string outStr2 = string.Format("{0:C2}", doubledExcellent);
textBox2.Text = outStr;
textBox3.Text = outStr1;
textBox4.Text = outStr2;
}
}
}
Your problem is Convert.ToInt32 - that changes your floating point value to an integer value, and integers don't hold fractional values. If you eliminate those lines, this should work exactly as you want (the string formatting looks correct).
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.