2016-08-02 3 views
1

안녕하세요 누군가 나를 도와주세요. 저는 사용자가 선택한 항목의 가격을 계산하기위한 시각적 기본 기능을 작성했습니다. If else if 루프를 사용했지만 첫번째 부분 만 출력을 표시합니다. 첫 번째 확인란을 선택하고 주문 버튼을 클릭하면 영수증에 가격과 선택한 항목이 채워지지만 선택하려면 확인란을 사용하고 있지만 두 번째, 세 번째 ... 확인란에 대해서는 발생하지 않습니다. 무엇이 잘못 될 수 있습니까? 여기 도와주세요 누군가가 기능Visual basic 만약 elseIf가 작동하지 않는다면

Private Sub computeCurrentSelection() 
    If chkugalis.Checked = True Then 'ugali fish selected 
     orderAmt = lab.Text 
     total = ugalif * orderAmt 
     subtotal = total 
     lstReceipt.Items.Add(orderAmt & " plates of" & " Ugali n fish " & total & " Kshs") 
    ElseIf chkGitheri.Checked = True Then 'ugali dengu slected 
     orderAmt = lab3.Text 
     total = ugalid * orderAmt 
     lstReceipt.Items.Add(orderAmt & " plates of " & "Ugali n dengu " & total) 
    ElseIf chkUgaliB.Checked = True Then 'githeri selected 
     orderAmt = lab2.Text 
     total = githeri * orderAmt 
     lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & total) 
    ElseIf chkPilau.Checked = True Then 
     orderAmt = lab4.Text 
     total = chapo * orderAmt 
     lstReceipt.Items.Add(orderAmt & " plates of " & "Pilau " & total) 
    ElseIf chkPizza.Checked = True Then 
     orderAmt = lab5.Text 
     total = pilau * orderAmt 
     lstReceipt.Items.Add(orderAmt & " plates of " & "Pizza " & total) 
    ElseIf chkMandazi.Checked = True Then 
     orderAmt = lab6.Text 
     total = pizza * orderAmt 
     lstReceipt.Items.Add(orderAmt & "mandazi " & total) 
    ElseIf chkSamosa.Checked = True Then 
     orderAmt = lab7.Text 
     total = mandazi * orderAmt 
     lstReceipt.Items.Add(orderAmt & "Samosa " & total) 
    ElseIf chkChapon.Checked = True Then 
     orderAmt = lab8.Text 
     total = samosa * orderAmt 
     lstReceipt.Items.Add(orderAmt & "Chapati " & total) 
    ElseIf chkWater.Checked = True And chk300ml.Checked = True Then 
     orderAmt = lab9.Text 
     total = water1 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 300ml" & "Water " & total) 
    ElseIf chkWater.Checked = True And chk500ml.Checked = True Then 
     orderAmt = lab9.Text 
     total = water2 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 500ml" & "Water " & total) 
    ElseIf chkWater.Checked = True And chk1l.Checked = True Then 
     orderAmt = lab9.Text 
     total = water3 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 1l" & "Water " & total) 
    ElseIf chkWater.Checked = True And chk2l.Checked = True Then 
     orderAmt = lab9.Text 
     total = water4 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 2l" & "Water " & total) 
    ElseIf chkSoda.Checked = True And chk300ml.Checked = True Then 
     orderAmt = lab10.Text 
     total = soda1 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 300ml" & "Soda " & total) 
    ElseIf chkSoda.Checked = True And chk500ml.Checked = True Then 
     orderAmt = lab10.Text 
     total = soda2 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 500ml" & "Soda " & total) 
    ElseIf chkSoda.Checked = True And chk1l.Checked = True Then 
     orderAmt = lab10.Text 
     total = soda3 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 1l" & "Soda " & total) 
    ElseIf chkSoda.Checked = True And chk2l.Checked = True Then 
     orderAmt = lab10.Text 
     total = soda4 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 2l" & "Soda " & total) 
    ElseIf chkJuice.Checked = True And chk300ml.Checked = True Then 
     orderAmt = lab11.Text 
     total = juice1 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 300ml" & "juice " & total) 
    ElseIf chkJuice.Checked = True And chk500ml.Checked = True Then 
     orderAmt = lab11.Text 
     total = juice2 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 500ml" & "juice " & total) 
    ElseIf chkJuice.Checked = True And chk1l.Checked = True Then 
     orderAmt = lab11.Text 
     total = juice3 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 1l" & "juice " & total) 
    ElseIf chkJuice.Checked = True And chk2l.Checked = True Then 
     orderAmt = lab11.Text 
     total = juice4 * orderAmt 
     lstReceipt.Items.Add(orderAmt & " Bottles of 2l" & "juice " & total) 
    End If 
End Sub 

답변

1

코드가 처음 선택된 경우에만 CheckBox의 논리가 실행됩니다.
CheckBox마다 If ... Then을 사용하십시오. 그런 식으로 코드는 모든 CheckBox을 확인합니다.

Private Sub computeCurrentSelection() 
    If chkugalis.Checked = True Then 'ugali fish selected 
     orderAmt = lab.Text 
     total = ugalif * orderAmt 
     subtotal = total 
     lstReceipt.Items.Add(orderAmt & " plates of" & " Ugali n fish " & total & " Kshs") 
    End If 

    If chkGitheri.Checked = True Then 'ugali dengu slected 
     orderAmt = lab3.Text 
     total = ugalid * orderAmt 
     lstReceipt.Items.Add(orderAmt & " plates of " & "Ugali n dengu " & total) 
    End If 

    If chkUgaliB.Checked = True Then 'githeri selected 
     orderAmt = lab2.Text 
     total = githeri * orderAmt 
     lstReceipt.Items.Add(orderAmt & " plates of " & "Githeri " & total) 
    End If 

    ' Other Ifs 

End Sub 
+0

실제로 그것은 첫 번째 옵션 이었지만 두 개 이상의 항목이 선택되어 있고 작동하지 않았는지 확인해야하므로 피할 수있었습니다. 그런데 당신이 나에게 내 사용법에 무엇이 잘못되었는지 말해 주시겠습니까? –

+0

하지만 둘 다 왜 작동하지 않는지 알아 냈습니다. 이것은 실제로 언급 한 것처럼 다른 방법입니다. 감사합니다. –

2

확인란 및 ElseIf 내 소스 코드? 하나의 선택 만 중요하면 확인란 대신 라디오 버튼을 사용하는 것이 좋습니다. 또한 모든 if에서 = True을 삭제할 수 있습니다. 또한 If이 true를 반환하면 Else 또는 ElseIf 중 아무 것도 작동하지 않습니다.

+0

여러 항목을 선택할 수 있기 때문에 확인란을 사용했습니다. 사실 내 proble 경우에만 true로 평가, 다른 사람들은 (때 그들이 체크해야)도하지 않는다는 것입니다 –

+0

고마워하는 사람들이 그것을 해결했습니다. 사실 문제는 내가 함수를 호출 할 시점에 있었고, 잊어 버렸고, 내 파트너가 배치 한 이전의 if else 문을 남겨 두었습니다. –