No syntax errors, but program does not produce any results - c#

I have to create a program for class. My applications needs to have value returning methods for OilLubeCharges(), FlushCharges(), MiscCharges(), OtherCharges(), TaxCharges(), TotalCharges().
It needs to have void methods for ClearOilLube(), ClearFlushes(), ClearMisc(), ClearOther(), ClearFees().
Currently my code has no syntax errors, however when I compile it it does not calculate anything. The calculate, clear, and exit buttons also do not work. Any assistance as to why these issues are occurring would be appreciated. Below is my code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CalcButton_Click(object sender, EventArgs e)
{
OilLubeCharges();
FlushCharges();
MiscCharges();
OtherCharges();
TaxCharges();
TotalCharges();
}
private void ClearButton_Click(object sender, EventArgs e)
{
oilCheckBox.Checked = false;
lubeCheckBox.Checked = false;
radFlushBox.Checked = false;
tranFlushBox.Checked = false;
insCheckBox.Checked = false;
mufCheckBox.Checked = false;
tireCheckBox.Checked = false;
partsTextBox.Text = "";
laborTextBox.Text = "";
serLabTotalTextBox.Text = "";
partsTotalTextBox.Text = "";
taxPartsTextBox.Text = "";
totalFeesTextBox.Text = "";
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private int OilLubeCharges()
{
int total = 0;
if (oilCheckBox.Checked)
{
total += 26;
serLabTotalTextBox.Text = total.ToString("c");
}
if (lubeCheckBox.Checked)
{
total += 18;
serLabTotalTextBox.Text = total.ToString("c");
return total;
}
else
{
return total;
}
}
private int FlushCharges()
{
int total = 0;
if (radFlushBox.Checked)
{
total += 30;
serLabTotalTextBox.Text = total.ToString("c");
}
if (tranFlushBox.Checked)
{
total += 80;
serLabTotalTextBox.Text = total.ToString("c");
return total;
}
else
{
return total;
}
}
private int MiscCharges()
{
int total = 0;
if (insCheckBox.Checked)
{
total += 15;
serLabTotalTextBox.Text = total.ToString("c");
}
if (mufCheckBox.Checked)
{
total += 100;
serLabTotalTextBox.Text = total.ToString("c");
}
if (tireCheckBox.Checked)
{
total += 20;
serLabTotalTextBox.Text = total.ToString("c");
return total;
}
else
{
return total;
}
}
private int OtherCharges()
{
int total = 0;
int parts = 0;
int labor = 0;
if (int.TryParse(partsTextBox.Text, out parts))
{
partsTextBox.Text = parts.ToString("c");
total = parts;
}
if (int.TryParse(laborTextBox.Text, out labor))
{
laborTextBox.Text = labor.ToString("c");
return total;
}
else
{
return total;
}
}
private decimal TaxCharges()
{
decimal parts = 0;
decimal partsTax;
partsTax = parts * .06m;
taxPartsTextBox.Text = partsTax.ToString("c");
return partsTax;
}
private decimal TotalCharges()
{
decimal total = 0;
total = OilLubeCharges() + FlushCharges() + MiscCharges() + TaxCharges() + OtherCharges();
totalFeesTextBox.Text = total.ToString("c");
return total;
}
}

As stated in the comments above, most likely your events are not hooked up to your buttons. You can do this several ways; typically it's done in the designer.
To know for sure if this is the cause, try this:
public Form1()
{
InitializeComponent();
CalcButton.Click += CalcButton_Click;
}
Note that this assumes your button is named "CalcButton". You can see whether that's true in the designer as well.
If that works, you need to do the same with the rest of your buttons either the same way or by selecting the method in the designer for the button.

Related

Rewriting my code as a Method, using C# Visual Studio

I am currently having trouble understanding Methods and how they work in C#. I currently have code written for a car cost calculator program I created, I want to rearrange or break my code down using methods. I am unsure how or where to begin doing so as it pertains to my program. Here is my code, clarification would be helpful! Thank you!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//constants for the Zone entered by user
const decimal ZoneCostN = 27;
const decimal ZoneCostS = 36;
const decimal ZoneCostE = 45;
const decimal ZoneCostW = 54;
private void CalcButton_Click(object sender, EventArgs e)
{
//set the variables
decimal PackWeight = 0;
decimal CostZone = 0;
decimal CostWeight = 0;
decimal ShippingTot = 0;
decimal Net = 0;
const decimal PerPound = 18;
//parses the entry into the textboxes
decimal.TryParse(WeightText.Text, out PackWeight); ;
//algorithm for variables
CostWeight = PackWeight * PerPound;
Zonelbl.Text = "";
CostZone = 0;
//if else statement to get the zone cost
{
if (NorthButton.Checked)
{
CostZone = ZoneCostN;
}
else if (SouthButton.Checked)
{
CostZone = ZoneCostS;
}
else if (EastButton.Checked)
{
CostZone = ZoneCostE;
}
else if (WestButton.Checked)
{
CostZone = ZoneCostW;
}
else
{
MessageBox.Show("Select a zone!");
}
}
//algorithm to get total and net
ShippingTot = CostZone + CostWeight;
Net = ShippingTot / CostWeight;
//if condition for CAPPED label
if (ShippingTot >= 100)
{
CAPPEDlbl.Visible = true;
}
else
{
CAPPEDlbl.Visible = false;
}
//output for all the data
Zonelbl.Text = CostZone.ToString("c");
Weightlbl.Text = CostWeight.ToString("c");
Totallbl.Text = ShippingTot.ToString("c");
Netlbl.Text = Net.ToString("c");
}
private void ClearButton_Click(object sender, EventArgs e)
{
//clears the form
Zonelbl.Text = "";
Weightlbl.Text = "";
Totallbl.Text = "";
Netlbl.Text = "";
WeightText.Text = "";
CAPPEDlbl.Visible = false;
WeightText.Focus();
}
}
Usually, we create methods when we need to reuse a code. In your case, you should see which part of your code will be reused in the future. If it is a simple form you may don't need to change anything but imagine you want to use your clear functionality somewhere else, create a method and call it everywhere you need
void clear()
{
Zonelbl.Text = "";
Weightlbl.Text = "";
Totallbl.Text = "";
Netlbl.Text = "";
WeightText.Text = "";
CAPPEDlbl.Visible = false;
WeightText.Focus();
}
private void ClearButton_Click(object sender, EventArgs e)
{
clear();
}
Now you can reuse clear() and in case you needed to change it you only need to change the method. It's the concept and you can apply it wherever you need.

Why are the values still adding up after i clear the output label?

I'm not sure what is going on. I thought I had it set up to clear the output label at the end. Everytime I clear it, the program still remembers the previous number and adds to it. I honestly have no idea what is going on.
Also on a side note, how do I put set up radiobuttons to be used in the this method?
First coding class so i'm still kind of a beginner.
private double oilandlube()
{
if (checkBox1.Checked == true)
{
Oil_change = 26;
}
if (checkBox2.Checked == true)
{
Lube_job = 18;
}
return Oil_change + Lube_job;
}
private void Oiltype ()
{
if (radioButton1.Checked)
{
Regular = 0;
}
if (radioButton2.Checked)
{
Mixed = 10;
}
else
{
Mixed = 0;
}
if (radioButton3.Checked)
{
Full_Synthetic = 18;
}
else
{
Full_Synthetic = 0;
}
}
private void carwash()
{
if (radioButton4.Checked)
{
none = 0;
}
if (radioButton5.Checked)
{
complimentary = 0;
}
if (radioButton6.Checked)
{
Full_service = 6;
}
else
{
Full_service = 0;
}
if (radioButton7.Checked)
{
Premium = 9;
}
else
{
Premium = 0;
}
}
private double flushes()
{
if (checkBox3.Checked == true)
{
Radiator_flush = 30;
}
if (checkBox4.Checked == true)
{
Transmission_flush = 80;
}
return Radiator_flush + Transmission_flush;
}
private double misc()
{
if (checkBox5.Checked == true)
{
inspection = 15;
}
if (checkBox6.Checked == true)
{
replace_muffler = 100;
}
if (checkBox7.Checked == true)
{
tire_rotation = 20;
}
return inspection + replace_muffler;
}
private double partsandlabor()
{
double.TryParse(textBox1.Text, out parts);
double.TryParse(textBox2.Text, out labor);
return parts + labor;
}
private double tax()
{
return partsandlabor() * taxes;
}
private void summary()
{
service = oilandlube() + flushes() + misc();
total_parts = partsandlabor();
double total_tax = tax();
double grand_total = service + total_parts + total_tax;
label7.Text = service.ToString("c");
label8.Text = total_parts.ToString("c");
label9.Text = total_tax.ToString("c");
label10.Text = grand_total.ToString("c");
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
oilandlube();
carwash();
flushes();
misc();
partsandlabor();
summary();
}
private void ClearOilandlube()
{
checkBox1.Checked = false;
checkBox2.Checked = false;
}
private void ClearMisc()
{
checkBox5.Checked = false;
checkBox6.Checked = false;
checkBox7.Checked = false;
}
private void ClearFlushes()
{
checkBox3.Checked = false;
checkBox4.Checked = false;
}
private void ClearSummary()
{
label7.Text = "";
label8.Text = "";
label9.Text = "";
label10.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
ClearOilandlube();
ClearMisc();
ClearFlushes();
ClearSummary();
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
radioButton5.Checked = false;
radioButton6.Checked = false;
radioButton7.Checked = false;
textBox1.Text = "0";
textBox2.Text = "0";
}
}
}
When you clear the contents of your controls, you should also clear the values of the backing variables, so they don't retain their previous values. You should be able to just set them back to zero in your Clear methods.
For example, oil and lube might look like:
private void ClearOilandlube()
{
checkBox1.Checked = false;
checkBox2.Checked = false;
Oil_change = 0;
Lube_job = 0;
Mixed = 0;
Regular = 0;
Full_Synthetic = 0;
}
It looks like you're holding state of some your variables globally so you can access them elsewhere.
Mixed = 10;
You'll have to reset that to some default value as well.

freezing form after clicking button

I have a problem regarding to payment button.
I don't encounter an error before building, but after building and clicking the payment button it hangs. I think its because of lblTotalPrice.text, but I don't know how to fix it.
public partial class Form1 : Form
{
int totalCost;
public double holDer;
public Form1()
{
InitializeComponent();
this.cbo1.Items.AddRange(new object[] { "Lechon Kawali - 200", "Bicol Express - 300"
,"Adobo - 350" });
}
private void btnAdd_Click(object sender, EventArgs e)
{
lb1.Items.Add(cbo1.SelectedItem);
lb1.SelectedIndex = lb1.SelectedIndex ;
int z = 0;
if (cbo1.SelectedIndex == 0)
{
z = z + 1;
}
if (cbo1.SelectedIndex == 1)
{
z = z + 2;
}
if (cbo1.SelectedIndex == 2)
{
z = z + 3;
}
switch(z)
{
case 1:
totalCost = totalCost + 200;
break;
case 2:
totalCost = totalCost + 300;
break;
case 3:
totalCost = totalCost + 350;
break;
}
lblSubTotalCost.Text = ("Php " + totalCost.ToString());
}
private void btnDelete_Click(object sender, EventArgs e)
{
int deleteCost = 0;
int itemCost = 0;
foreach (int selectedIndex in lb1.SelectedIndices)
{
itemCost = int.Parse(lb1.Items[selectedIndex].ToString().Split('-')[1]);
deleteCost += itemCost; lb1.Items.RemoveAt(selectedIndex);
}
totalCost = totalCost - deleteCost;
lblSubTotalCost.Text = ("Php " + totalCost.ToString());
lb1.Items.Remove(lb1.SelectedItem);
if (lb1.Items.Count > 0)
{
lb1.SelectedIndex = 0;
}
else
MessageBox.Show("No orders");
}
private void lblVAT_TextChanged(object sender, EventArgs e)
{
Add();
}
private void lblSubTotalCost_TextChanged(object sender, EventArgs e)
{
multiply();
Add();
}
public void multiply()
{
int a;
double b = 0.12;
bool Valid = int.TryParse(totalCost.ToString(), out a);
if (Valid)
lblVAT.Text = (a * b).ToString();
else
lblVAT.Text = "No VAT entered";
}
private void lbTotalPrice_TextChanged(object sender, EventArgs e)
{
Add();
}
public void Add()
{
int a;
int b;
bool AValid = int.TryParse(totalCost.ToString(), out a);
bool BValid = int.TryParse(lblVAT.Text, out b);
if (AValid && BValid)
{
lblTotalPrice.Text = ("Php " + (a + b).ToString());
}
}
private void btnPayment_Click(object sender, EventArgs e)
{
holDer = double.Parse(tbPayment.Text) - double.Parse(lblTotalPrice.Text);
MessageBox.Show("Change: " + holDer.ToString());
}
}
As you stated in your comments, your problem it is not that your application hangs, but the problem is that you are getting a Input string was not in a correct format Exception.
That it seems is from this block:
private void btnPayment_Click(object sender, EventArgs e)
{
holDer = double.Parse(tbPayment.Text) - double.Parse(lblTotalPrice.Text);
MessageBox.Show("Change: " + holDer.ToString());
}
As it is a not very complex code, your problem seems to be when you are casting your textboxes to Double. Be sure that you are using the correct separator for double values and that you not have any strange characters in tbPayment
Try using TryParse method:
double res;
if(double.TryParse(tbPayment.Text, out res))
{
holDer = res - double.Parse(lblTotalPrice.Text);
MessageBox.Show("Change: " + holDer.ToString());
}
else
{
MessageBox.Show("Input a correct format");
}
I suspect it's because the lblTotalPrice_TextChanged event-handler calls Add() which then changes the lblTotalPrice.Text property, causing the event to fire again, ad -infinitum?

Stopping the calculations because of predicted error

I am unsure how to ask this question as I can't quite well translate it.
I am currently working on my own Windows Form Application that will calculate the dimensions of a given package in inches (written all together in string format) and calculate the dimensions in centimeters, milimeters or even meters and at this point I was wondering what if someone entered wrong dimensions and given measures can not be parsed.
Something like Environment.Exit(), but without closing the application just stopping calculations and writing a message that an error has occured.
If there is a question like this answered, please do link it because I haven't been able to find it.
namespace PretvaranjeDimenzija
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public double mjera = 1;
public bool pogreska = false;
public string mjeraKratica = " cm";
public string dimenzijeIspis = "";
private void buttonIzracunaj_Click(object sender, EventArgs e)
{
string dim = textBoxDimenzije.Text;
if (dim.Contains('.'))
{
dim = dim.Replace('.', ',');
}
if (dim.Length != 0)
{
if (dim.IndexOf('x') != -1)
{
string[] multiDim = dim.Split('x');
double[] multiDimCentimetara = new double[multiDim.Length];
bool[] uspjeh = new bool[multiDim.Length];
for (int i = 0; i < multiDim.Length; i++)
{
uspjeh[i] = double.TryParse(multiDim[i], out multiDimCentimetara[i]);
if (uspjeh[i] == false)
{
pogreska = true;
goto kraj;
}
}
kraj:
if (pogreska == true)
{
MessageBox.Show("Doslo je do pogreske!");
pogreska = false;
}
else
{
double[] dimenzije = new double[multiDim.Length];
for (int i = 0; i < dimenzije.Length; i++)
{
dimenzije[i] = multiDimCentimetara[i] * 2.54 * mjera;
if (i == dimenzije.Length - 1)
{
dimenzijeIspis += dimenzije[i].ToString() + mjeraKratica;
}
else
{
dimenzijeIspis += dimenzije[i].ToString() + "x";
}
}
textBoxIspisDimenzija.Text = dimenzijeIspis;
dimenzijeIspis = "";
}
}
else
{
double dimCentimetara;
if(double.TryParse(dim, out dimCentimetara))
{
double dimenzija = dimCentimetara * 2.54 * mjera;
dimenzijeIspis = dimenzija.ToString() + mjeraKratica;
textBoxIspisDimenzija.Text = dimenzijeIspis;
dimenzijeIspis = "";
}
else
{
MessageBox.Show("Doslo je do pogreske!");
return;
}
}
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
mjera = 0.01;
mjeraKratica = " m";
if (radioButton2.Checked == true)
{
radioButton2.Checked = false;
radioButton1.Checked = true;
}
if (radioButton3.Checked == true)
{
radioButton3.Checked = false;
radioButton1.Checked = true;
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
mjera = 1;
mjeraKratica = " cm";
if (radioButton1.Checked == true)
{
radioButton1.Checked = false;
radioButton2.Checked = true;
}
if (radioButton3.Checked == true)
{
radioButton3.Checked = false;
radioButton2.Checked = true;
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
mjera = 10;
mjeraKratica = " mm";
if (radioButton2.Checked == true)
{
radioButton2.Checked = false;
radioButton3.Checked = true;
}
if (radioButton1.Checked == true)
{
radioButton1.Checked = false;
radioButton3.Checked = true;
}
}
}
It should be pretty simple, depending on your requirements. For example, you could just use a basic if block in your method.
void CalculateStuff()
{
// Get input. Do stuff.
if (IsInvalid)
{
MessageBox.Show("You did a bad thing.");
return; // exit the method.
}
// now that we know the input is good, do other stuff.
}
Substitute IsInvalid with whatever check condition you want that will return true if the input is not valid.

Multiplication in C# WPF

I'm trying to make a program that generates 2 random numbers, then, after the user inputs the multiplication of those numbers, it will tell the user in a MessageBox whether or not the user was right or wrong. Sort of like an educational simulator.
I'm having problems with the end part. What I'm saying is; If the answer is the same as number1 and number2 multiplied, then it says; "Rétt!" and if not; "Rangt..."
Any ideas?
private void bt_end_Click(object sender, RoutedEventArgs e)
{
int number1 = 0;
int number2 = 0;
int answer = 0;
tbnumber1.Text = number1.ToString();
tbnumber2.Text = number2.ToString();
if (svar == (number1 * number2))
{
MessageBox.Show("Rétt!");
}
else
{
MessageBox.Show("Rangt...");
}
}
}
}
The full code.
public MainWindow()
{
InitializeComponent();
}
int tala1 = 0;
int tala2 = 0;
int svar = 0;
private void btstart_Click(object sender, RoutedEventArgs e)
{
tbtala1.Text = "";
tbtala2.Text = "";
tbsvar.Text = "";
Random random = new Random();
tala1 = random.Next(1, 11);
tala2 = random.Next(1, 11);
tbtala1.Text = tala1.ToString();
tbtala2.Text = tala2.ToString();
}
private void btend_Click(object sender, RoutedEventArgs e)
{
tbtala1.Text = tala1.ToString();
tbtala2.Text = tala2.ToString();
tbsvar.Text = svar.ToString();
if (svar == (tala1 * tala2).ToString())
{
MessageBox.Show("Rétt!");
}
else
{
MessageBox.Show("Rangt... :(");
}
}
}
}
SOLVED. Thank you to everyone who did/tried to help.
Final Version:
Final Version:
public MainWindow()
{
InitializeComponent();
}
int tala1 = 0;
int tala2 = 0;
int svar = 0;
private void btstart_Click(object sender, RoutedEventArgs e)
{
tbtala1.Text = "";
tbtala2.Text = "";
tbsvar.Text = "";
Random random = new Random();
tala1 = random.Next(1, 11);
tala2 = random.Next(1, 11);
tbtala1.Text = tala1.ToString();
tbtala2.Text = tala2.ToString();
}
private void btend_Click(object sender, RoutedEventArgs e)
{
tala1 = Convert.ToInt32(tbtala1.Text);
tala2 = Convert.ToInt32(tbtala2.Text);
svar = Convert.ToInt32(tbsvar.Text);
if (svar == (tala1 * tala2))
{
MessageBox.Show("Rétt!");
}
else
{
MessageBox.Show("Rangt... :(");
}
To do this in a safe manner, you should not only Parse the text inputs to numbers, but you should also ensure that the text does in fact contain numbers first. If your numbers are supposed to be integers then you could use this:
private void bt_end_Click(object sender, RoutedEventArgs e)
{
int number1 = 0;
int number2 = 0;
if (int.TryParse(tbnumber1.Text, out number1) ||
int.TryParse(tbnumber12.Text, out number2)) MessageBox.Show("Rangt...");
MessageBox.Show(svar == number1 * number2 ? "Rétt!" : "Rangt...");
}
You're putting the numbers in the textbox values. you have to convert textbox values to numbers.
private void btend_Click(object sender, RoutedEventArgs e)
{
try{
tala1 = Convert.ToInt32(tbtala1.Text);
tala2 = Convert.ToInt32(tbtala2.Text);
svar = Convert.ToInt32(tbsvar.Text);
if (svar == (tala1 * tala2))
{
MessageBox.Show("Rétt!");
}
else
{
MessageBox.Show("Rangt... :(");
}
}catch{
MessageBox.Show("Invalid input");
}
}
You never define svar. You probably meant something like this, assuming svar was set earlier and is the correct product of the random multiplication:
private void bt_end_Click(object sender, RoutedEventArgs e)
{
int number1 = 0;
int number2 = 0;
number1 = int.Parse(tbnumber1.Text);
number2 = int.Parse(tbnumber2.Text);
if (svar == (number1 * number2))
{
MessageBox.Show("Rétt!");
}
else
{
MessageBox.Show("Rangt...");
}
}
1.I observed that you have not initialized variable 'svar' anywhere.
Initialise your svar variable with proper value.
2.Replace This :
if (svar == (number1 * number2).ToString())
With Following :
if (svar == (number1 * number2))

Categories

Resources