How to add the numbers stored in Label.Text - c#

I basically want to total the amount.
The amount gets stored in a label. I want to add labels. Basically I want to do an addition of labels but i can't because label is .Text which is String so when i add it I get a string of added label while i want the Numbers stored in the labels to get added. this is my code below.
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
temp4 = Int32.Parse(DropDownList3.Text);
temp5 = temp4 * 76;
Label7.Text = temp5.ToString();
}
On the click of a button the amount in Lablel7 should get added with another Label.
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Label16.Text = Label7.Text+Label6.Text;
}
So that the total amount can be found.
Am kinda new to programming and all itself and this is part of my project am sorry if this questions seems stupid

Label16.Text = (int.parse(Label7.Text)+int.parse(Label6.Text)).toString();
Use above code.
Convert your addition to string datatype.

Parse both label's Text property to integer and then do the addition.
Label16.Text = (int.Parse(Label7.Text) + int.Parse(Label6.Text)).ToString();
Its better if you can use int.TryParse which would save you from the exception if the text is not a number.
int number1;
int number2;
if(!int.TryParse(Label7.Text, out number1))
{
// invalid number in Label7
}
if(!int.TryParse(Label6.Text, out number2))
{
// invalid number in Label6
}
Label16.Text = (number1 + number2).ToString();

Label7.Text is type of string, you can add int, so you have to convert it. Afterr all you have convert back int to string
Label16.Text = (int.Parse(Label7.Text)+int.Parse(Label6.Text)).ToString();
Try to rename your controls and var. For example lblAmount tell you maore than Label6. Please read about Camel, Pascal convertion, It will help you in the future.

Two solutions :
1- You can parse each Text to convert into Int32, that you can add and then convert in text with ToString()
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Label16.Text = (Int32.Parse(Label17.Text) + Int32.Parse(Label6.Text)).ToString();
}
2- On each chanching, you can save values in private properties of type Int32, and work with them.

Related

Separate the three-digit numbers in Windows form(C # ) and return them to the original state

In Windows Form (C #), I enter my number in the text box and separate the three digits with the following code (for better readability of the number). For example, the:
2500000 => 2,500,000
But I have a problem!
I want to do math operations (addition, multiplication, etc.) on my numbers. And I need to return my number to the first state (2500000) !?
please guide me
This is my code:
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox1.Text = "0";
}
textBox1.Text = long.Parse(textBox1.Text.Replace(",", "")).ToString("n0");
textBox1.SelectionStart = textBox1.Text.Length;
}
Since the Text property is a string, you will need to parse it to a number in order to make math operations. You can do this safely by calling the TryParse method.
if (long.TryParse(textBox1.Text, NumberStyles.AllowThousands, CultureInfo.CurrentCulture, out var number))
{
// number is now of type long
number += number;
}
By the way, in your example you remove the commas by replacing them with an empty string, but then you put them back by calling .ToString("n0").

I want to put comma with decimal value in calculator by pressing equal in c#

I want to put comma with decimal value in calculator by pressing equal in c#.
Example : 1234.1234 will be 1,234.1234.
My code in below - but it is not working :
private void TextBoxTextChanged(object sender, EventArgs e)
{
string value = TextBox.Text.Replace(",", "");
long ul;
if (ulong.TryParse(value, out ul))
{
TextBoxCost.TextChanged -= TextBoxCostTextChanged;
TextBoxCost.Text = string.Format("{0:#,#0}", ul);
TextBoxCost.SelectionStart = TextBoxCost.Text.Length;
TextBoxCost.TextChanged += TextBoxCostTextChanged;
}
}
Can anybody help how can I solve it ?
Try this,
string a = (1234.1234).ToString("#,##0.0000");
I would recommend you change your number type to double and use double.TryParse:
string value = TextBox.Text.Replace(",", "");
double dbl;
if (double.TryParse(value, out dbl))
{
TextBoxCost.TextChanged -= TextBoxCostTextChanged;
TextBoxCost.Text = string.Format("{0:#,#0.0000}", dbl); // or {0:#,#0.####}
TextBoxCost.SelectionStart = TextBoxCost.Text.Length;
TextBoxCost.TextChanged += TextBoxCostTextChanged;
}
This is because ulong.TryParse will fail when it finds decimal points. You also used a long as your out parameter for ulong.TryParse, rather than a ulong, which is also not allowed.
Try this
Convert.ToDecimal(number).ToString("#,##0.00");

Input string was not in a correct format - Slot Mchine App

I get the error: 'Input string was not in a correct format'..... I am running an if else calculation and then populating a label with the result the variable is declared as decimal then ToString('C') to the label...
List<string> moneyWon = new List<string>();
private void calculateAmountWon()
{
moneyWon.Add(amountWonlabel.Text);
decimal won = moneyWon.Sum(str => Convert.ToInt32(str));
moneyWonLabel.Text = won.ToString("C");
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
The only thing that will throw that error is the Convert.ToInt32(str) call. One of the items in the moneyWon list is not a valid int value.
You should probably also declare moneyWon as a List<int> and store all the values as int's instead of string's. It doesn't make sense to store everything as a string and then convert it to int when you need it.
Based on what you're outputting I'm assuming that your strings are formatted as currency with a currency symbol and two decimal places. If that's the case you can change your parsing to:
decimal won = moneyWon.Sum(str => decimal.Parse(str, NumberStyles.Currency));
You're still vulnerable to invalid formats but if the values are all set programatically then they should be predictable.
Another option is to use a list of numeric types and parse up front:
List<decimal> moneyWon = new List<decimal>();
private void calculateAmountWon()
{
moneyWon.Add(decimal.Parse(amountWonlabel.Text, NumberStyles.Currency));
decimal won = moneyWon.Sum();
moneyWonLabel.Text = won.ToString("C");
}

Displaying a character count while the user is typing?

I'm trying to build a program with that displays the number of characters and words while a user is typing into the text box. I thought I knew what I was doing but ran into this error:
'Cannot implicitly convert type 'string' to
'Systems.Windows.Forms.Label'
This is what I have so far. The last line of code contains the error:
private void userTextBox_TextChanged(object sender, EventArgs e)
{
string userInput = userTextBox.Text;
char charCount;
charCount = userInput[0];
charCountOutput = charCount.ToString();
}
1) You need to set the property on the Label to set the text
charCountOutput.Text = ...
2) The length of a string can be accessed through the Length property
charCountOutput.Text = userInput.Length.ToString();
charCountOutput.Text = charCount.ToString();
Assuming charCountOutput is the label
Your code is trying to assign the Label object the value of a string, which is a type mismatch (obviously).
You're assigning to a textfield, changing the text of the field.
charCountOutput.Text = charCount.ToString();
int countChar = userTextBox.Text.ToString().Length;
Here's a late addition - you probably already have seen this, but here's a really fast approach. Assumes charCountOutput is label on your form:
private void userTextBox_TextChanged(object sender, EventArgs e)
{
var userInput = userTextBox.Text;
charCountOutput.Text = userInput.Length.ToString();
}

how to change timespan variable to a integer type?

I'm trying to convert timespan variable into an integer variable using 'parse'. I get an error that says:
Format exception was unhandled: Input string was not in correct format
This is the code is have :
private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
{
TimeSpan t = dateTimePicker4.Value.ToLocalTime() - dateTimePicker3.Value.ToLocalTime();
int x = int.Parse(t.ToString());
y = x;
}
My target is to display this the change in time for two timepickers, dynamically in a text box, i.e, the difference in minutes between them should be displayed in a textbox automatically.
the difference in minutes between them should be displayed in a
textbox automatically.
Instead of parsing use TimeSpan.TotalMinutes property.
t.TotalMinutes;
The property is of double type, if you just need to integer part then you can do:
int x = (int) t.totalMinutes;
private void dateTimePicker4_ValueChanged(object sender, EventArgs e)
{
TimeSpan t = dateTimePicker4.Value.ToLocalTime() - dateTimePicker3.Value.ToLocalTime();
int x = int.Parse(t.Minutes.ToString());
y = x;
}
Have you tried changing it to int x = int.Parse(t.Minutes.ToString());?
From : http://msdn.microsoft.com/en-us/library/system.timespan.aspx

Categories

Resources