I have the best part of 2 days trying to figure out how to "times 2 dropdownlists" with each other and show it in a label...
like in math when u mutiple 2 numbers..
plzz help me...
all suggestions are welcome
Protected void Button1_Click(object sender, EventArgs e)
{
int amount1 = int.parse(DropDownList1.selectedValue.ToString());
int amount2 = int.parse(DropDownList1.selectedValue.ToString());
Label.Text = amount1 * amount2;
}
i get an error:
Cannot implicitly convert type 'Int' to 'string
Just use:
Label.Text = (amount1 * amount2).ToString();
as you cannot assign integer result to string Label.Text property.
You can also do it this way
int val = amount1 * amount2;
Label.Text = val.ToString();
Label.Text expects String and you were trying to assign integer to it.
Related
In my program I am using a database containing a Time (string previousTimeVASN).
I have a timer to clock total seconds. i would like to display the sum of previousTimeVASN + the total seconds.
I have error showing:
"System.FormatException: 'Input string was not in a correct format.'" at line: double test1 = Convert.ToDouble(previousTimeVASN);
any suggestion are much appreicated.
private void sNbtn_Click(object sender, RoutedEventArgs e)
{
TabControl.SelectedIndex = 1;
dtVASN.Tick += new EventHandler(dtVASN_Tick);
dtVASN.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
void dtVASN_Tick(object sender, EventArgs e)
{
if (swVASN.IsRunning)
{
TimeSpan tsVASN = swVASN.Elapsed;
double test = tsVASN.TotalSeconds;
double test1 = Convert.ToDouble(previousTimeVASN);
txtVASN.Text = (test + test1).ToString();
}
}
The method Convert.ToDouble will throw FormatException if value is not a number in a valid format. You are getting the same here means either the value in previousTimeVASN is not a number or it is not in the expected format. Here is an alternate option for you to check whether the conversion is possible or not, that is, Double.TryParse Method
Converts the string representation of a number in a specified style
and culture-specific format to its double-precision floating-point
number equivalent. A return value indicates whether the conversion
succeeded or failed.
So the code can be revamped as like the following:
if (swVASN.IsRunning)
{
TimeSpan tsVASN = swVASN.Elapsed;
double test = tsVASN.TotalSeconds;
double test1;
Double.TryParse(previousTimeVASN, out test1);
txtVASN.Text = (test + test1).ToString();
}
if you want to alert the user that the second number is not valid then you can use the return value of parse like the following:
if(Double.TryParse(previousTimeVASN, out test1))
txtVASN.Text = (test + test1).ToString();
else
txtVASN.Text = "previous Time VASN is not valid:'
I have knowledge in PHP and I want to learn C # language but I do not even do simple addition.
I want to get the value of a ComboBox, convert this value to int and be able to add another value
Despite the conversion done, I have an error : Can not convert type "int" to "string.
My code :
private void btnValidate_click(object sender, RoutedEventArgs e)
{
int number = Test();
}
int Test()
{
string day = DayBirth.Text;
int number;
bool isNumeric = int.TryParse(day, out number);
if (isNumeric == false)
{
Resultat1.Text = "This is not a number";
}
else
{
Resultat1.Text = number + 10;
}
return number;
}
Thank you
The issue is that Resultat1.Text is expecting a string, not an int. You can do
Resultat1.Text = (number+10).ToString();
and it should work.
what you need to do is Converting your number to string after addition
Resultat1.Text = (number + 10).ToString;
Text property accept string value not integer so after addition you have to convert it as string
Resultat1.Text = (number + 10).ToString();
I am making a program for calculating the cost of 3d printing stuff. i came up on this question.
private void button1_Click(object sender, EventArgs e)
{
double getal1 = Int32.Parse(textBox1.Text);
double getal2 = Int32.Parse(textBox2.Text);
double volume = 0.002405281875;
double volume1 = 0;
int dichtheidpla = Int32.Parse("1,24");
if (comboBox1.Text == "PLA")
{
//volume berekenen
volume1 = volume * getal1;
V.Text = volume1.ToString("N2");
//gewicht berekenen
int volumeberekend = Convert.ToInt32(volume1);
gewicht1.Text = volumeberekend * dichtheidpla;
}
else if (comboBox1.Text == "ABS")
{
}
}
}
when i want to calculate gewicht1 the error shows me: Cannot implicitly convert type 'int' to 'string'. there is a red line under 'volumeberekend * dichtheidpla'.
gewicht1.Text is an string type, and the result of the expression will be an int. So you just have to convert that result into string.
gewicht1.Text = (volumeberekend * dichtheidpla).ToString();
It's as it says, it can't implicitly convert int to string without you deciding how you want that to happen.
Try this explicit conversion instead:
int volumeberekend = Convert.ToInt32(volume1);
int total = volumeberekend * dichtheidpla
gewicht1.Text = total.ToString();
I have little problem, and I need help..
So here is my problem I have created win form in c# and used numericupdown element to insert my numbers, but I cant calculate percent. Here is the code below:
private void button8_Click(object sender, EventArgs e)
{
int x, y, sum;
x = Convert.ToInt16(numericUpDown7.Value);
y = Convert.ToInt16(numericUpDown8.Value);
sum = x * 3.4528 + 21%;
textBox5.Text = Convert.ToString(sum);
}
What I need to do is to insert x and press button to calculate this formula
example: x * 3.4528 + 21 % = ???
Maby someone has options to help me.
Thanks for all of you, who will help me!
Try this
sum = (x * 3.4528) * 1.21;
private void button1_Click(object sender, EventArgs e)
{
double eng, urdu, math, cs, tot, per;
eng = Convert.ToDouble(txtenglish.Text);
urdu = Convert.ToDouble(txturdu.Text);
math = Convert.ToDouble(txtmath.Text);
cs = Convert.ToDouble(txtcs.Text);
tot = eng + urdu + math + cs;
lbltotal.Text = Convert.ToString(tot);
per = (tot / 400) * 100;
lblpercent.Text = Convert.ToString(per);
}
First off you need to use decimal, float, or double instead of int (you can find many references online about each to help you determine which would be best for you). Otherwise it will just truncate the answer and drop anything after the decimal point. Second you need to use the formula that everyone else has mentioned sum = x * 3.4528 * 1.21.
hi in this code im trying to convert a double from a label into the variable but im coming up with the exception
"Unable to cast object of type 'System.Windows.Forms.Label' to type
'System.IConvertible'."
on the "convert.toDouble(lblPricekey);" area and I'm not sure why.
private void btnAddtoTotal_Click(object sender, EventArgs e)
{
double numPadTotal;
numPadTotal = Convert.ToDouble(lblPricekey.Text);
double finalTotal = total + numPadTotal;
txtTotal.Text = finalTotal.ToString();
}
You are converting Label to double. You should convert Label.Text.
numPadTotal = Convert.ToDouble(lblPricekey.Text);
lblPricekey is a type of label. You can't convert it to double. Use Text property for converting. It represents string. Look at from MSDN.
Gets or sets the text content of the Label control.
Try like this;
numPadTotal = Convert.ToDouble(lblPricekey.Text);
Try:
private void btnAddtoTotal_Click(object sender, EventArgs e)
{
double numPadTotal = Convert.ToDouble(lblPricekey.Text);
double finalTotal = total + numPadTotal;
txtTotal.Text = finalTotal.ToString();
}
The best way to this is use the TryParse Method
double numPadTotal;
var IsDouble = double.TryParse(lblPricekey.Text, out numPadTotal);
if (IsDouble)
{
double finalTotal = total + numPadTotal;
txtTotal.Text = finalTotal.ToString();
}