Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am very new to c# , and i am trying to do a mathematical calculation using tow or more fields in my win form application , and i am trying to show the result in a text box when the link table is clicked , although his programme is not showing any error in visual studio , i am not getting the updated value in my textbox , (that is no changes happening when i click the link label) , kindly go through my below programme and guide me where i am wrong:

What I have tried:

private void TSCalc_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if(materialTypeComboBox.Text == "ABC" && unitComboBox.Text == "XYZ")
                {
                    tSTextBox.Text = decimal.Round((decimal)Math.Sqrt(float.Parse(frontWtTextBox.Text) / float.Parse(backWtTextBox.Text) / 2 * float.Parse(cFComboBox.Text)), 2).ToString("0.00");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Saving Failed:" + ex.Message.ToString(), "Save",
                     MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Posted
Updated 11-Dec-20 23:35pm

use 'TryParse to validate your sources are Type 'float:
float f1 = 0.0;

if (! Single.TryParse(frontWtTextBox.Text, out f1))
{
     tSTextBox.Text =  $"invalid input in frontWtTextBox.Text: {frontWtTextBox.Text}";

     // return ? throw error ?
}   
Note: written from memory, not tested.

Put breakpoints in your code and single-step through it (F11): observe what happens.

If the end-user is entering floats with another delimiter, like a comma, you may need to provide a culture specific IFormatProvider: [^]
 
Share this answer
 
Comments
Member 14898617 12-Dec-20 5:41am    
Thank you for the response
C#
tSTextBox.Text = decimal.Round((decimal)Math.Sqrt(float.Parse(frontWtTextBox.Text) / float.Parse(backWtTextBox.Text) / 2 * float.Parse(cFComboBox.Text)), 2).ToString("0.00");

Statements like this are really bad practice (especially for a beginner). If anything is wrong in there then the whole thing fails but it is difficult to find out which part is the problem. Break it down into its constituent parts so you can use your debugger to check each item as the code progresses. Something like:
C#
float frontValue;
// always use TryParse so you can check if the text is valid
if (!float.TryParse(frontWtTextBox.Text, out frontValue)
{
    // the text string is not a valid floating point number
    // so what should you do here?
}
float backValue;
if (!float.TryParse(backWtTextBox.Text, out frontValue)
{
    // same problem here
}
// and the same with any other input values.
// now perform your calculations - one step at a time
// and finally convert your result to a string and post it to the result box.
 
Share this answer
 
Comments
Member 14898617 12-Dec-20 5:40am    
Thank you so much ..now i got the idea

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900