Question ComboBox Construction of If statement needed.

Beginner

Well-known member
Joined
Mar 12, 2008
Messages
114
Programming Experience
Beginner
Hi

There are two missing things in the project. Im already done devloping the first missing thing.

Now Basically heres what i want to do:

49799547vg8.jpg


1-Select the Material Name from the Combo Box
2-(Build an If statement; for instanse if the mild steel is selected and the Feed tooth is 0.3 then my value(KC) would be 1820.
3-When the Calculate Button Is hit this is what i want to happen, The Gearsafety.value = (declared variable) where the declared variable would be a mathematical Equation. Example

Dim Calculate as Double
Calculate = Tool Diamater * Depth of Cut * vf * KC ; where the KC is 1820.






26956386ns5.jpg



Now continuing the previous code:

If calcualte < GearMaxpower.value then
Gearsafety.value= "safe"
ele
Gearsafety.value "unsafe"


Now my Question is how would i build an if statement for the combo box for all the materials.
I think its :

If item1.value = true & GearFeedPerTooth=0.1 Then KC = 2200 else if item1.value = true & GearFeedPerTooth=0.2 Then KC = 1950...................................

Then Item2.value = true.....................................

Then Item 3.........



May you kindly tell me the exact code regarding the selection of the combo box items ? and an easier procedure if available ?


Also If I set the control ’ s Sorted property to True , the ComboBox lists its choices in sorted order , will that affect the coding and result in error values ?




Heres my attempt:

Private​
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, y, z As Double

x = (ToolDiameter * (Math.PI) * GearSpindleSpeed.Value) / 1000
GearVc.Text = x
y = GearFeedPerTooth.Value * GearTeethCutter.Value * GearSpindleSpeed.Value
GearVf.Text = y
z = (y * GearDepthCutZ.Value * ToolDiameter) / 1000
GearMrr.Text = z
Dim KC, Calculate As Double

If GearMaterialSelection.SelectedItem = "Mild steel" & GearFeedPerTooth.Value = 0.1 Then

KC = 2200
ElseIf GearMaterialSelection.SelectedItem = "Mild steel" & GearFeedPerTooth.Value = 0.2 Then

KC = 1950
ElseIf GearMaterialSelection.SelectedItem = "Mild steel" & GearFeedPerTooth.Value = 0.3 Then

KC = 1820
ElseIf GearMaterialSelection.SelectedItem = "Mild steel" & GearFeedPerTooth.Value = 0.4 Then

KC = 1700
ElseIf GearMaterialSelection.SelectedItem = "Mild steel" & GearFeedPerTooth.Value = 0.6 Then

KC = 1580
End If

Calculate = (ToolDiameter * GearDepthCutZ.Value * y * KC) / (1000000 * 60 * 0.9)
GearSafety.Text = Calculate
'If Calculate < GearmaxPower.Value Then

'GearSafety.Text = "Safe"

'Else

'GearSafety.Text = "UnSafe"

'End If

End Sub



I disabled the last 4 lines to see what value fo i get but it keeps giving me a zero.
 
I edited this line to see if all the code was fine.


Calculate = (ToolDiameter * GearDepthCutZ.Value * y * KC) / (1000000 * 60 * 0.9)

Removed KC and i input an integer and the calculation was done.

So it Seems that KC is not being stored and carried forward form the IF .... ELSEIF statement.
 
VB.NET:
        Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper
            Case "Mild steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2200
                    Case 0.2D
                        KC = 1950
                    Case Else
                        'put a safety function here for all unwritten GearFeedPerTooth values
                End Select
            Case Else
                'put a safety function here for all unwritten GearMaterialSelection values
        End Select
 
Now the value of Mild Steel is being Selected properly. Now Case "Medium steel".ToUpper KC values seem not to be selected and they return a "0". Is the line code correct ?

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
VB.NET:
Dim x, y, z, KC, Calculate As Double
x = (ToolDiameter * (Math.PI) * GearSpindleSpeed.Value) / 1000
GearVc.Text = x
y = GearFeedPerTooth.Value * GearTeethCutter.Value * GearSpindleSpeed.Value
GearVf.Text = y
z = (y * GearDepthCutZ.Value * ToolDiameter) / 1000
GearMrr.Text = z
Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper
Case "Mild steel".ToUpper
Select Case GearFeedPerTooth.Value
Case 0.1D
KC = 2200
Case 0.2D
KC = 1950
Case 0.3D
KC = 1820
Case 0.4D
KC = 1700
Case 0.6D
KC = 1580
Case "Medium steel".ToUpper
Select Case GearFeedPerTooth.Value
Case 0.1D
KC = 1980
Case 0.2D
KC = 1800
Case 0.3D
KC = 1730
Case 0.4D
KC = 1600
Case 0.6D
KC = 1570
Case "Hard steel".ToUpper
Select Case GearFeedPerTooth.Value
Case 0.1D
KC = 2520
Case 0.2D
KC = 2200
Case 0.3D
KC = 2040
Case 0.4D
KC = 1850
Case 0.6D
KC = 1740
Case Else
' a safety function here for all unwritten GearFeedPerTooth values
End Select
End Select
End Select
'a safety function here for all unwritten GearMaterialSelection values
End Select
Calculate = (ToolDiameter * GearDepthCutZ.Value * GearVf.Text * KC) / (1000000 * 60 * 0.9)
GearSafety.Text = Calculate
End Sub
'End If
 
'Tool(steel)
'Chrome manganese steel
'Chrome molybdenum steel
'Nickel chrome molybdenum steel
'Cast(iron)
'Meehanite cast iron  
'Gray cast iron
'Hard Cast Iron
'Brass()
'light(Alloy(Al - Mg))
'Light(alloy(Al - Si))
'If Calculate < GearmaxPower.Value Then
'GearSafety.Text = "Safe"
'Else
'GearSafety.Text = "UnSafe"
 
Your End Selects are in the wrong place. I've spaced them out to make it easier to see, but you obviously dont need to include the extra lines.

VB.NET:
        Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper

            Case "Mild steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2200
                    Case 0.2D
                        KC = 1950
                    Case Else
                        'put a safety function here for all unwritten GearFeedPerTooth values for Mild steel
                End Select 'ends the Mild Steel logic


            Case "Medium steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 1980
                    Case 0.2D
                        KC = 1800
                    Case Else
                        'put a safety function here for all unwritten GearFeedPerTooth values for Medium steel
                End Select 'ends the Medium Steel logic


            Case Else
                'put a safety function here for all unwritten GearMaterialSelection values ie any other material
        End Select
 
BTW when i say 0.1D what does D Stand For here ?

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim x, y, z, KC, Calculate, i As Double

x = (ToolDiameter * (Math.PI) * GearSpindleSpeed.Value) / 1000

GearVc.Text = x

y = GearFeedPerTooth.Value * GearTeethCutter.Value * GearSpindleSpeed.Value

GearVf.Text = y

z = (y * GearDepthCutZ.Value * ToolDiameter) / 1000

GearMrr.Text = z



Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper

Case "Mild steel".ToUpper

Select Case GearFeedPerTooth.Value



Case 0.1D

KC = 2200

Case 0.2D

KC = 1950

Case 0.3D

KC = 1820

Case 0.4D

KC = 1700

Case 0.6D

KC = 1580

Case Else

For i.(D) = 0.1 To 10000 + ( How do i rite the code for Skip 0.1D,0.2D,0.3D,0.4D,0.6D)

step = 0.1

Next

KC = ""

End Select

Case "Medium steel".ToUpper

Select Case GearFeedPerTooth.Value

Case 0.1D

KC = 1980

Case 0.2D

KC = 1800

Case 0.3D

KC = 1730

Case 0.4D

KC = 1600

Case 0.6D

KC = 1570

Case Else

For i.(D) = 0.1 To 10000 + ( How do i rite the code for Skip 0.1D,0.2D,0.3D,0.4D,0.6D)

step = 0.1

Next

KC = ""

End Select







Now regarding the Case Else ... Im getting Three Errors:



1-Name 'D' is not declared.

2-Syntax error.

3-Identifier expected.

Regarding 'put a safety function here for all unwritten GearMaterialSelection values. Do you mean when a user inputs text inside the combo Box ? If thats the case then:
I can neglect And make the Combo Box non-editable by changing the DropDownStyle To DropDownList.

Instead i will need a Null


I changed the code to


VB.NET:
Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper
            Case "Mild steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2200
                    Case 0.2D
                        KC = 1950
                    Case 0.3D
                        KC = 1820
                    Case 0.4D
                        KC = 1700
                    Case 0.6D
                        KC = 1580
                    Case Else
                        For i As Integer = 0 To 100000
                            (i/10)D
                            KC = ""
                        Next

                End Select

Im getting a syntax error now regarding the bracket (i/10) "("
 
Last edited:
BTW when i say 0.1D what does D Stand For here ?
It's a number converter, it tells the compiler that you want the 0.1 to be stored as a Decimal. Without a 'D' next to it, the compiler would store the 0.1 as a Double, which may not matter in this case.

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim x, y, z, KC, Calculate, i As Double

x = (ToolDiameter * (Math.PI) * GearSpindleSpeed.Value) / 1000

GearVc.Text = x

y = GearFeedPerTooth.Value * GearTeethCutter.Value * GearSpindleSpeed.Value

GearVf.Text = y

z = (y * GearDepthCutZ.Value * ToolDiameter) / 1000

GearMrr.Text = z



Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper

Case "Mild steel".ToUpper

Select Case GearFeedPerTooth.Value



Case 0.1D

KC = 2200

Case 0.2D

KC = 1950

Case 0.3D

KC = 1820

Case 0.4D

KC = 1700

Case 0.6D

KC = 1580

Case Else

For i.(D) = 0.1 To 10000 + ( How do i rite the code for Skip 0.1D,0.2D,0.3D,0.4D,0.6D)

step = 0.1

Next

KC = ""

End Select

Case "Medium steel".ToUpper

Select Case GearFeedPerTooth.Value

Case 0.1D

KC = 1980

Case 0.2D

KC = 1800

Case 0.3D

KC = 1730

Case 0.4D

KC = 1600

Case 0.6D

KC = 1570

Case Else

For i.(D) = 0.1 To 10000 + ( How do i rite the code for Skip 0.1D,0.2D,0.3D,0.4D,0.6D)

step = 0.1

Next

KC = ""

End Select

Now regarding the Case Else ... Im getting Three Errors:

1-Name 'D' is not declared.

2-Syntax error.

3-Identifier expected.

Try this:
For i = 0.7 To 10000 Step 0.1
 
Heres my working code but ive got some questions.
Now as you can see for the else case i chose a value for KC to be 0.
after the selection is done, the following code is executed:

VB.NET:
[COLOR="Green"]'Take the value of KC no matter what it was proceed with the calcluation[/COLOR]
 Calculate = (ToolDiameter * GearDepthCutZ.Value * GearVf.Text * KC) / (1000000 * 60 * 0.9)
        GearSafety.Text = Calculate
[COLOR="green"]'Now if KC=0 the execute the following code else dont.[/COLOR]
        If KC = 0 Then
            GearSafety.Text = ""
        End If



VB.NET:
  Dim x, y, z, KC, Calculate As Double
        x = (ToolDiameter * (Math.PI) * GearSpindleSpeed.Value) / 1000
        GearVc.Text = x
        y = GearFeedPerTooth.Value * GearTeethCutter.Value * GearSpindleSpeed.Value
        GearVf.Text = y
        z = (y * GearDepthCutZ.Value * ToolDiameter) / 1000
        GearMrr.Text = z


        Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper
            Case "Mild steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2200
                    Case 0.2D
                        KC = 1950
                    Case 0.3D
                        KC = 1820
                    Case 0.4D
                        KC = 1700
                    Case 0.6D
                        KC = 1580
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select

            Case "Medium steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 1980
                    Case 0.2D
                        KC = 1800
                    Case 0.3D
                        KC = 1730
                    Case 0.4D
                        KC = 1600
                    Case 0.6D
                        KC = 1570
                    Case Else

                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Hard steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2520
                    Case 0.2D
                        KC = 2200
                    Case 0.3D
                        KC = 2040
                    Case 0.4D
                        KC = 1850
                    Case 0.6D
                        KC = 1740
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Tool steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 1980
                    Case 0.2D
                        KC = 1800
                    Case 0.3D
                        KC = 1730
                    Case 0.4D
                        KC = 1700
                    Case 0.6D
                        KC = 1600
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Chrome manganese steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2300
                    Case 0.2D
                        KC = 2000
                    Case 0.3D
                        KC = 1880
                    Case 0.4D
                        KC = 1750
                    Case 0.6D
                        KC = 1660
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Chrome molybdenum steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2540
                    Case 0.2D
                        KC = 2250
                    Case 0.3D
                        KC = 2140
                    Case 0.4D
                        KC = 2000
                    Case 0.6D
                        KC = 1800
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Nickel chrome molybdenum steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2000
                    Case 0.2D
                        KC = 1800
                    Case 0.3D
                        KC = 1680
                    Case 0.4D
                        KC = 1600
                    Case 0.6D
                        KC = 1500
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Cast Iron".ToUpper()
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2800
                    Case 0.2D
                        KC = 2500
                    Case 0.3D
                        KC = 2320
                    Case 0.4D
                        KC = 2200
                    Case 0.6D
                        KC = 2040
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Meehanite cast iron".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2180
                    Case 0.2D
                        KC = 2000
                    Case 0.3D
                        KC = 1750
                    Case 0.4D
                        KC = 1600
                    Case 0.6D
                        KC = 1470
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Gray cast iron".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 1750
                    Case 0.2D
                        KC = 1400
                    Case 0.3D
                        KC = 1240
                    Case 0.4D
                        KC = 1050
                    Case 0.6D
                        KC = 970
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Hard Cast Iron".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 3000
                    Case 0.2D
                        KC = 2700
                    Case 0.3D
                        KC = 2500
                    Case 0.4D
                        KC = 2400
                    Case 0.6D
                        KC = 2200
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Brass".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 1150
                    Case 0.2D
                        KC = 950
                    Case 0.3D
                        KC = 800
                    Case 0.4D
                        KC = 700
                    Case 0.6D
                        KC = 630
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "light Alloy (Al-Mg)".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 580
                    Case 0.2D
                        KC = 480
                    Case 0.3D
                        KC = 400
                    Case 0.4D
                        KC = 350
                    Case 0.6D
                        KC = 320
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case "Light alloy (Al-Si)".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 700
                    Case 0.2D
                        KC = 600
                    Case 0.3D
                        KC = 490
                    Case 0.4D
                        KC = 450
                    Case 0.6D
                        KC = 390
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = 0
                        Next
                End Select
            Case Else

        End Select

        Calculate = (ToolDiameter * GearDepthCutZ.Value * GearVf.Text * KC) / (1000000 * 60 * 0.9)
        GearSafety.Text = Calculate
        If KC = 0 Then
            GearSafety.Text = ""
        End If

        'If Calculate < GearmaxPower.Value Then
        'GearSafety.Text = "Safe"
        'Else
        'GearSafety.Text = "UnSafe"
        'End If

    End Sub


I tried modifying the code for KC instead of 0 i Said " "

VB.NET:
  Select Case GearMaterialSelection.SelectedItem.ToString.ToUpper
            Case "Mild steel".ToUpper
                Select Case GearFeedPerTooth.Value
                    Case 0.1D
                        KC = 2200
                    Case 0.2D
                        KC = 1950
                    Case 0.3D
                        KC = 1820
                    Case 0.4D
                        KC = 1700
                    Case 0.6D
                        KC = 1580
                    Case Else
                        For i As Double = 0.7 To 10000 Step 0.1
                            KC = ""
                        Next
                End Select

Which returned an error Conversion From string"" to type Double is not valid Because at the bottom of the code the calculation cant take place.


VB.NET:
        Calculate = (ToolDiameter * GearDepthCutZ.Value * GearVf.Text * KC) / (1000000 * 60 * 0.9)

KC is "".


1-I dont want to use KC = 0 i want to change it to "" and let the code work. How am i able to do so ?


2-Regarding the Material Selection Combo Box can is there anyway to get rid ofthe Null Expected Error ?

For instance if the GearMaterialSelection........ = Null then

Vcg.text=""
Vfg.text=""
GearSafety.text=""
MsgBox("Please Select a Material First", MsgBoxStyle.Information)

or how do i set the combobox to mild steel, Is is similar to the NUD where you can select the desired value ?


3-
VB.NET:
For i As Double = 0.7 To 10000 Step 0.1
.
What im trying to say how did the program execute the following code

VB.NET:
        If KC = 0 Then
            GearSafety.Text = ""
        End If

when the value 0.5 was selected.
 
Last edited:
1-I dont want to use KC = 0 i want to change it to "" and let the code work. How am i able to do so ?

You can't, the equivalent double for an empty string ("") is 0. But you can test the value of KC to see if it's greater than 0 before doing the calculation(s):
VB.NET:
If KC > 0
  Calculate = (ToolDiameter * GearDepthCutZ.Value * GearVf.Text * KC) / (1000000 * 60 * 0.9)
Else
  Calculate = 0
End If

2-Regarding the Material Selection Combo Box can is there anyway to get rid ofthe Null Expected Error ?

For instance if the GearMaterialSelection........ = Null then

Vcg.text=""
Vfg.text=""
GearSafety.text=""
MsgBox("Please Select a Material First", MsgBoxStyle.Information)

or how do i set the combobox to mild steel, Is is similar to the NUD where you can select the desired value ?

Yes:
VB.NET:
GearMaterialSelection.SelectedIndex = 0 '0 is the first item on the list, which is Mild Steel

3. I'm not sure I'm following this one correctly. If KC equals 0, you want the GearSafety.Text to be nothing (equal to "") right? And the questions is "How is it happening when KC equals 0.5?" right? It could be that GearSafety is being cleared somewhere else in the code and isn't being cleared here, because KC does not equal 0.
 
Back
Top