Question Unit Length Converter Calculation

slb2009

New member
Joined
Apr 5, 2009
Messages
4
Programming Experience
1-3
Hello Everyone,

I am doing a project "Unit Length Converter" using VB.NET.

I have two dropdownlist, 10 lines of textboxes (20 textboxes each side) and one Calculate button.

Please see the graphical sample of my project I am doing right now:

http://sdrv.ms/XMElf7

I selected the units of length on the both side of the dropdownlists, for example at left side dropdownlist1 I selected the unit (From:) "Inches" and on the right side of the dropdownlist2, I selected the unit (To:) centimeter. Later, I typed the numbers in each textbox(es) on the left side, and the textbox(es) on the right side are the results for the unit centimeter conversion.

How to add values entered into each textbox1, textbox3, textbox5... to be display the results in the textbox2, textbox4, textbox6, so on... based on the calculation converter, using VB.NET code?

See my code below, I understand is not the right code:

Dim calculation As Double

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
FillDropDownList1()
FillDropDownList2()
End If
End Sub

Private Sub FillDropDownList1()

DropDownList1.Items.Add("Choose one...")
DropDownList1.Items.Add("Inches")
DropDownList1.Items.Add("Feet")
DropDownList1.Items.Add("Meters")
End Sub

Private Sub FillDropDownList2()

DropDownList2.Items.Add("Choose one...")
DropDownList2.Items.Add("Inches")
DropDownList2.Items.Add("Feet")
DropDownList2.Items.Add("Meters")
End Sub

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

Dim Num As Double
Dim val1 As Double = TextBox1.Text
Dim val2 As Double = TextBox2.Text

If Double.TryParse(TextBox1.Text, Num) = False Then
Label3.Text = "Please enter a numeric value"
Exit Sub
End If

If DropDownList1.SelectedItem.Text <> "" Then

Select Case DropDownList1.SelectedItem.Text
Case "inches"
Num = CDbl(TextBox1.Text)
End Select

If DropDownList2.SelectedItem.Text <> "" Then
Select Case DropDownList2.SelectedItem.Text
Case "centimeter"
val1 = TextBox2.Text * 0.254
End Select
End If
End If

Protected Sub TextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox1.TextChanged
End Sub

Protected Sub TextBox2_TextChanged(sender As Object, e As System.EventArgs) Handles TextBox2.TextChanged
End Sub

End class

I have been doing doing this for a few days unsuccefully. You help is very much appreciative. Don't forget to view my graphical project.

Thanks
 
There is no need to initialize val 2 with the textbox2.text.. if you are multiplying the data from textbox 1 with a static multiplier then textbox2' will have no data in it at the point where you try to reference it.

Also, what is the point in declaring and initializing val1 at the top of the sub when you don't refer back to it as a data source in the calculations below except to change it again with data from a textbox (2) that still does not exist yet?


Break your problem down.. you need to take the value in the "unit from" textbox.. convert it.. then display the new number in the "unit to" textbox..

so val2 = (val1 * multiplier) and your textbox2 needs to display val2.
 
Thank you 22-degrees for your quick response.

I tried with this following line of code on calculate button:

Protected Sub btncalculate_Click(sender As Object, e As System.EventArgs) Handles btncalculate.Click

Dim Num As Double
Dim val1 As Double = TextBox1.Text
Dim val2 As Double = TextBox2.Text
If Double.TryParse(TextBox1.Text, Num) = False Then
Label3.Text = "Please enter a numeric value"
Exit Sub
End If

Try

If DropDownList1.SelectedItem.Text <> "" Then
Select Case DropDownList1.SelectedItem.Text

Case "inches"
val2 = (val1 * 0.254)


End Select

End If


Catch ex As Exception

End Try

End class

I have tried but doesn't display the results in textbox2 to. What am I missing?
 
Is this your first vb.net project?

My apologies if this sounds rude.. but do you see any line of code that assigns data to textbox2.text? If you want your data to appear somewhere then you are going to have to use code to make it happen. The only mention of textbox2 in your sub is once again a line of code that takes the value from textbox2 (blank at this point in time) and stores it to a variable which will be changed again before it is used..

Once your calculation is complete, you have your val2 variable holding the converted number for you. Now all you have to do is tell textbox2 that you want it to display the var2 value.

Also, 1 inch = 2.54 centimeters, not 0.254 centimeters
 
This is my first project for the Unit Length Converter.
Please could you share me an example of lines of code that give results on the right Unit To as seen on the graphical example link. Thanks for the correction of 1 inch = 2.54 centimeters.
 
If you understand the code you are using, then you already know how to send the results to the textbox. You just need to see the logic. If "val1 = textbox1.text" is how you get the content of the textbox into the variable named val1, then, the reverse of this is how you get the content of val1 to display in textbox1.

To change the text in any textbox from either nothing to something or something to something else you simply write,

textboxName.Text = ((whatever you want it to be))

Where "textboxName" is whatever the name of the textbox in question is.

In your case, your code to order the content of val2 into the textbox named textbox2 is:

TextBox2.Text = val2.ToString

As you say this is your first project, I will add that the logic to remember is that when you write a line of code such as this, whatever the content of val2 is at the time the code is read and executed, that will be the result of what is shown in the textbox.

Therefore you need to make sure that the conversion calculation has been created and stored in val2 before the contents of val2 are to be displayed in textbox2. Otherwise val2 in this case will have no data in it and textbox2 will only show "0".

I'd also recommend that you start from scratch with this and futurte projects until you start to see the logic more clearly.. Don't use code found online. Break everything down into small pieces and solve those little problems. Once you can do this, a lot of the bigger problems solve themselves.

Good Luck
 

Latest posts

Back
Top