I need to calculate the Lb1SumF plus Lb2SumF equal Lb3SumF.
I ran it, and somehow the label3 does not display the expected result.
Here is a screenshot from result.
Here is my code.
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
Lb3_TextChanged might never be invoked as you are not changing the text of the label. I would suggest to change it to a private method and not an event handler. Here is what the code could be like:
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
// Call to update sum
UpdateSum();
}
catch
{
Lb2SumF.Text = "0";
}
}
// private void Lb3_TextChanged(object sender, EventArgs e)
private void UpdateSum()
{
int sum = 0;
if(!string.IsNullOrEmpty(Lb1SumF.Text) && !string.IsNullOrEmpty(Lb2SumF.Text))
{
sum = Convert.ToInt32(Lb1SumF.Text) + Convert.ToInt32(Lb2SumF.Text);
}
Lb3.Text = Convert.ToString(sum);
}
Replace you code with this it will work, you are using * operator where you have to use + operator, I have commented that lines in your code and replaced it for better understanding.
Happy Coding
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
private void Lb1SumF_Click(object sender, EventArgs e)
{
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TX1.TabIndex=0;
}
private void label4_Click(object sender, EventArgs e)
{
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb1PriceF.Text) * Convert.ToInt32(TX1.Text); // You were doing wrong here , you were multiplying these values
sumF = Convert.ToInt32(Lb1PriceF.Text) + Convert.ToInt32(TX1.Text);
Lb1SumF.Text = Convert.ToString(sumF); //Label1 sum
}
catch
{
Lb1SumF.Text = "0";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sumF;
//sumF = Convert.ToInt32(Lb2PriceF.Text) * Convert.ToInt32(TX2.Text); //you are doing it wrong here , you are multiplying
sumF = Convert.ToInt32(Lb2PriceF.Text) + Convert.ToInt32(TX2.Text);
Lb2SumF.Text = Convert.ToString(sumF); //Label2 sum
}
catch
{
Lb2SumF.Text = "0";
}
}
private void Lb3_TextChanged(object sender, EventArgs e)
{
int i = Convert.ToInt32(Lb1SumF.Text);
int j = Convert.ToInt32(Lb2SumF.Text);
Lb3.Text = Convert.ToString(i+j); // Label3 sum
}
private void Lb3SumF_Click(object sender, EventArgs e)
{
}
}
}
Related
I am a newbie and I am honestly struggling a lot with this. I am trying to calculate all the entries from the texbox to the listbox and then divide by 7 and display the average in an output label. I am having trouble displaying the case average. Here is the code:
public partial class formAverageWeeklyCases : Form
{
int SEVEN = 7;
public formAverageWeeklyCases()
{
InitializeComponent();
}
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void buttonEnter_Click(object sender, EventArgs e)
{
if (textBoxCaseEntry.Text.Trim().Length >= 1)
{
listBoxDailyCases.Items.Add(textBoxCaseEntry.Text.Trim());
textBoxCaseEntry.Text = string.Empty;
textBoxCaseEntry.Focus();
}
else
{
MessageBox.Show("The number entered does not appear to be valid");
textBoxCaseEntry.Text = string.Empty;
textBoxCaseEntry.Focus();
}
}
private void buttonReset_Click(object sender, EventArgs e)
{
textBoxCaseEntry.Text = string.Empty;
listBoxDailyCases.Items.Clear();
labelOutputDailyAverage.Text = string.Empty;
}
private void textBoxAverageWeeklyCases_TextChanged(object sender, EventArgs e)
{
if (listBoxDailyCases.SelectedItems.Count <= SEVEN)
{
double average = listBoxDailyCases.SelectedItems.Count / SEVEN;
labelOutputDailyAverage.Text.Show(+average);
}
else
{
MessageBox.Show("Please enter a only 7 case count numbers");
}
}
}
If I understand you correctly, you want to collect 7 numbers from the user and then calculate the average? If yes, I suggest you declare a list of ints (or doubles if you need doubles), then call TryParse to validate the user input and then calculate the average when needed:
public partial class formAverageWeeklyCases : Form
{
const int SEVEN = 7;
private List<int> numbers = new List<int>(SEVEN); // NEW
public formAverageWeeklyCases()
{
InitializeComponent();
}
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void buttonEnter_Click(object sender, EventArgs e)
{
if (int.TryParse(textBoxCaseEntry.Text.Trim(), out int parsed)) // NEW
{
numbers.Add(parsed); // NEW
textBoxCaseEntry.Text = string.Empty;
textBoxCaseEntry.Focus();
}
else
{
MessageBox.Show("The number entered does not appear to be valid");
textBoxCaseEntry.Text = string.Empty;
textBoxCaseEntry.Focus();
}
}
private void buttonReset_Click(object sender, EventArgs e)
{
textBoxCaseEntry.Text = string.Empty;
listBoxDailyCases.Items.Clear();
labelOutputDailyAverage.Text = string.Empty;
}
private void textBoxAverageWeeklyCases_TextChanged(object sender, EventArgs e)
{
if (listBoxDailyCases.SelectedItems.Count <= SEVEN)
{
double average = numbers.Sum() / SEVEN; // NEW
labelOutputDailyAverage.Text = average.ToString();
}
else
{
MessageBox.Show("Please enter a only 7 case count numbers");
}
}
}
I'm writing a code where we need to find all the ones in a character and display them as a total. However, the code updates the total for each character pressed, rather than adding all previous ones counts.
Help would be appreciated.
namespace ica5_eventdriven
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
listBox1.Enabled = true;
lblKeyCode.Text = e.KeyChar.ToString();
listBox1.Items.Add(Convert.ToString(Convert.ToByte(e.KeyChar), 2));
List<byte> byteList = new List<byte>();
byteList.Add((byte)e.KeyChar);
byte binaryVal = Convert.ToByte(e.KeyChar);
int ones = 0;
int zeros = 0;
foreach (byte val in byteList)
{
for (int i = 0; i < 8; i++)
{
if ((binaryVal & 1) == 1)
{
ones++;
lblOnesCount.Text = ones.ToString();
}
else
zeros++;
binaryVal >>= 1;
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
I'm trying to make it so that my form does not appear blank. It was working before, It wouldn't let me save my file at one point so I renamed it to form3temp and now the form shows up when I run it, but not when I look at it in design view. Form 5 also does the same thing. The rest of my 7 forms are fine.
How it appears inside design
https://i.gyazo.com/06994988ff2f307d5042c0bafec1c43e.png
How it appears when I run the application
https://gyazo.com/97c1838e55f5f595b09211f2ee742c9f
namespace LawnCarePrototype
{
public partial class Form31 : Form
{
public static decimal yardCost = 0.0M;
decimal yardSizeMultiplier = 0.0M;
public Form31()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
yardSizeMultiplier = 0.0M;
yardCost = 0.0M;
if (radioButton1.Checked)
{
yardSizeMultiplier = 0.8M;
}
if (radioButton2.Checked)
{
yardSizeMultiplier = 1.0M;
}
if (radioButton3.Checked)
{
yardSizeMultiplier = 1.2M;
}
if (checkBox1.Checked)
{
yardCost =+ 15.00M * yardSizeMultiplier;
//2 EDGE
Global.edgeExp = 15.00M * yardSizeMultiplier;
}
if (checkBox2.Checked)
{
yardCost =+30.00M * yardSizeMultiplier;
Global.bushExp = 30.00M * yardSizeMultiplier;
//3 BUSH
}
if (checkBox3.Checked)
{
yardCost =+ 25.00M * yardSizeMultiplier;
Global.leafExp = 25.00M * yardSizeMultiplier;
//4 LEAF
}
if (checkBox4.Checked)
{
yardCost =+ 35.00M * yardSizeMultiplier;
//1 LAWN
Global.MowExp = 35.00M * yardSizeMultiplier;
}
if (yardCost == 0.00M)
{
MessageBox.Show("Please Input Your Selection");
}
else
{
Form4 form4obj = new Form4();
form4obj.Show();
this.Close();
}
Global.subTotal = yardCost;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2obj = new Form2();
form2obj.Show();
this.Close();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
I expect the form design to appear because I don't see any errors.
Not sure what could be causing this in my code.
Side Note: Sorry for the poor code format!
I want to remove an item from a list...
I am obviously missing something...I have tried just about every variation including EXCEPT, REMOVE, etc...
When debugging, I step through each ling, but when it gets to btnRemove_Click, it steps through removing but does not remove anything...it acts as if I never sent a command to remove anything???
Help!
public partial class frmUpdate : Form
{
private Student student = new Student();
private string _scores;
public frmUpdate()
{
InitializeComponent();
}
public string GetUpdatedScores(Student s)
{
txtName.Text = s.Name;
_scores = s.Scores;
FillStudentGrades();
this.ShowDialog();
return _scores;
}
private void FillStudentGrades()
{
lstScores.Items.Clear();
string[] grades = splitGrades(_scores);
foreach (string s in grades)
{
lstScores.Items.Add(s.ToString());
}
}
private void lstScores_SelectedIndexChanged(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnAdd_Click(object sender, EventArgs e)
{
frmAddScore addScore = new frmAddScore();
_scores += " " + addScore.AddScore();
FillStudentGrades();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
int i = lstScores.SelectedIndex;
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (lstScores.SelectedIndex < 0)
{
MessageBox.Show("You Must Select A Grade.");
btnUpdate.Focus();
}
else
{
int i = lstScores.SelectedIndex;
string[] grades = splitGrades(_scores);
string message = "Are you sure you want to remove " + grades[i].ToString() + "?";
DialogResult button = MessageBox.Show(message, "Confirm Remove",
MessageBoxButtons.YesNo);
if (button == DialogResult.Yes)
{
int count = 0;
foreach (char c in grades[i])
{
if (char.IsDigit(c))
{
count++;
}
}
int a = _scores.IndexOf(grades[i].ToString());
_scores = _scores.Remove(a, (count + 1));
FillStudentGrades();
btnOk.Focus();
}
else
{
btnOk.Focus();
}
}
}
private void btnClearAll_Click(object sender, EventArgs e)
{
}
private void btnOk_Click(object sender, EventArgs e)
{
student.Name = txtName.Text;
student.Scores = _scores;
this.Close();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
public string[] splitGrades(string s)
{
string[] grades = s.Split(' ');
return grades;
}
}
In C#, strings are immutable. _scores.Remove(i); doesn't change _scores. Instead it returns a new string object that you can assign to a variable, for example, back to _scores like this:
_scores = _scores.Remove(i);
you need to use RemoveAt methos - as you are trying to remove index and not the value
I'm new to c#, and after looking through the other threads on this subject I'm still at a loss of how to fix the bug:C.
I'm trying to make a simple calculator and here is the code for that:
//Global Variables
string sign;
double val1;
double val2;
int trackkeypoint = 0;
public void Calculator()
{
InitializeComponent();
}
private void cmd0_Click(object sender, EventArgs e)
{
txtbox.Text = txtbox.Text + cmd0.Text;
}
private void cmd1_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd1.Text;
}
private void cmd2_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd2.Text;
}
private void cmd3_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd3.Text;
}
private void cmd4_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd4.Text;
}
private void cmd5_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd5.Text;
}
private void cmd6_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd6.Text;
}
private void cmd7_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd7.Text;
}
private void cmd8_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd8.Text;
}
private void cmd9_Click(object sender, EventArgs e)
{
txtbox.Text=txtbox.Text+cmd9.Text;
}
private void cmdequal_Click(object sender, EventArgs e)
{
val2 = double.Parse(txtbox.Text);
double result;
if(sign=="+")
{
result = val1 + val2;
txtbox.Text = result.ToString();
}
else if(sign=="-")
{
result = val1 - val2;
txtbox.Text = result.ToString();
}
else if(sign=="X")
{
result = val1 * val2;
txtbox.Text = result.ToString();
}
else if(sign=="/")
{
result = val1 / val2;
txtbox.Text = result.ToString();
}
}
private void cmdclear_Click(object sender, EventArgs e)
{
//Clears text
txtbox.Text = "";
val1 = 0;
val2 = 0;
sign = "";
}
private void cmdplus_Click(object sender, EventArgs e)
{
sign = "+";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdsubtract_Click(object sender, EventArgs e)
{
sign = "-";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdmultiply_Click(object sender, EventArgs e)
{
sign = "X";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmddivide_Click(object sender, EventArgs e)
{
sign = "/";
val1 = double.Parse(txtbox.Text);
txtbox.Text = "";
}
private void cmdsqrt_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Sqrt(v).ToString();
}
private void cmdsquare_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Pow(v,2).ToString();
}
private void cmdsin_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Sin(v).ToString();
}
private void cmdcos_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Cos(v).ToString();
}
private void cmdtan_Click(object sender, EventArgs e)
{
double v;
v = double.Parse(txtbox.Text);
txtbox.Text = Math.Tan(v).ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtbox_TextChanged(object sender, EventArgs e)
{
}
private void txtbox_KeyPress(object sender, KeyPressEventArgs e)
{
int keycode;
keycode = e.KeyChar;
//accept only number from key 0 to key 9, key back, and key dot
if (keycode >= 48 && keycode <= 57 || keycode==8 || keycode==32 || keycode==46)
{
//key dot allowed only one time
if (keycode == 46) ++trackkeypoint;
if (trackkeypoint > 1) { e.Handled = true; --trackkeypoint; }
}
else e.Handled = true;
}
private void txtbox_KeyDown(object sender, KeyEventArgs e)
{
}
}
}
and I get this error:
I tried changing that to CWindowsGUI and that didn't work, or removing the offending bits, or a host of other layman fixes. It also shows up in the designer window:
The namespace is the same on the CWindowsGUI.Designer.cs as the actual code thing
Looking at the class you provided I think you renamed your Form1 to Calculator
So try:
Application.Run(new Calculator());
Edit:
CWindowsGUI.cs
public partial class Calculator : Form
{
public Calculator() // Not public void Calculator()
{
InitializeComponent();
}
CWindowsGUI.Designer.cs
public partial class Calculator
First you need to fix some errors
private void Form1_Load(object sender, EventArgs e)
{
}
you do not have any form named as Form1 it seems that when you created application it created a form for you named as Form1 you just renamed it from solution explorer.
delete this load event.
public void Calculator()
{
InitializeComponent();
}
is it a constructor of your form CWindowsGUI
constructor name should be same as form name correct it.
Do You wants to run CWindowsGUI form first ???
Application.Run();
method defines that which form you wants to set as startup that's all
then try this :
Application.Run(new CWindowsGUI());
instead of
Application.Run(new Calculator());
my suggestion would be that delete this form(CWindowsGUI) and create a new form then code on that page also donot forget to change the form in
Application.Run(new CWindowsGUI());