I thought this would be a fun exercise to show you more about the .NET framework and how to write reusable code. This code solves your same problem (assuming you rename all the label controls from Label1 to Label10 insead of Label11 to Label20.)
First I will show you the main code. This is the very same code that reads each texbox, calculates the material, and stores the result in a label.
Dim materials() As Double = { _
0.0008, 0.0138, 0.0552, 0.1656, 0.4968, 0.9936, 1.4904, 2.2356, 3.3534, 5.0301}
Dim i As Integer
Dim total As Double = 0
For i = 1 To 10
Dim value As String = GetTextByName("TextBox" & i)
value = value.Trim
Dim result As Double = 0
If IsNumeric(value) Then
result = CDbl(value) * materials(i - 1)
total += result
End If
SetTextByName("Label" & i, result.ToString)
Next
For one, the code utilizes the VB "For Next" loop. This basically repeates one set of code for each calculation, rather than writting the same code 10 times. Inside the loop, you will see two custom functions (GetTextByName and SetTextByName), this demonstrates how to modularize your code. It demonstrates code reuse as well. The two functions are defined below:
Public Function GetTextByName(ByVal name As String) As String
Dim flags As BindingFlags = _
BindingFlags.NonPublic Or _
BindingFlags.GetProperty Or _
BindingFlags.Instance
Dim frmType As Type = Me.GetType
Dim tbProp As PropertyInfo = frmType.GetProperty(name, flags)
If Not tbProp Is Nothing Then
Dim value As Object = tbProp.GetValue(Me, Nothing)
If value.GetType Is GetType(TextBox) Then
Return CType(value, TextBox).Text
ElseIf value.GetType Is GetType(Label) Then
Return CType(value, Label).Text
End If
End If
Return ""
End Function
Public Sub SetTextByName(ByVal name As String, ByVal value As String)
Dim flags As BindingFlags = _
BindingFlags.NonPublic Or _
BindingFlags.GetProperty Or _
BindingFlags.Instance
Dim frmType As Type = Me.GetType
Dim tbProp As PropertyInfo = frmType.GetProperty(name, flags)
If Not tbProp Is Nothing Then
If tbProp.PropertyType Is GetType(Label) Then
With CType(tbProp.GetValue(Me, Nothing), Label)
.Text = value
End With
ElseIf tbProp.PropertyType Is GetType(TextBox) Then
With CType(tbProp.GetValue(Me, Nothing), TextBox)
.Text = value
End With
End If
End If
End Sub
Both functions assume you have this line of code at the very top of the code page:
Imports System.Reflection
Basically the functions look for a textbox or label control on the current form by name and then dynamically grab or set the controls text property. As it stands now, both these functions must be defined in the Form class for which they are used. You could further refine these functions to be reused on any Form. However, that will be up to you. To get a better understanding, read up on Reflection in the .NET framework.
Also, you may argue that more code was involved to write the same solution, but now you have two reusable functions that serve many purposes now. Some will argue that it's overkill and sometimes I might agree. However, this is for educational purposes for one and for two, the execution of code in .NET is so fast, you would never notice.
By fully understanding the code above, you will have made great leaps.