2016-02-26 155 views
0

您好我是在Visual Studio的編碼新。 我正在使用Visual Studio 2012 我有一個問題。 我想將組合框和文本框31連接到文本框1 前。如果ComboBox是100和textbox31是1,因此TextBox1的將是100如果陳述在案例陳述(VB.Net)

我結束了這段代碼:

Dim c As String 

    c = ComboBox1.Text 


    Select Case "c" 

     Case 100 
      If TextBox31.Text >= 2.25 Then 
       TextBox1.Text = 100 
      ElseIf TextBox31.Text >= 2.5 Then 
       TextBox1.Text = 75 
      ElseIf TextBox31.Text >= 2.75 Then 
       TextBox1.Text = 50 
      ElseIf TextBox31.Text >= 3 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 3.25 Then 
       TextBox1.Text = 0 

      End If 

     Case 75 
      If TextBox31.Text >= 2.25 Then 
       TextBox31.Text = 75 
      ElseIf TextBox1.Text >= 2.5 Then 
       TextBox1.Text = 75 
      ElseIf TextBox31.Text >= 2.75 Then 
       TextBox1.Text = 50 
      ElseIf TextBox31.Text >= 3 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 3.25 Then 
       TextBox1.Text = 0 

      End If 

     Case 50 
      If TextBox31.Text >= 2.25 Then 
       TextBox1.Text = 50 
      ElseIf TextBox31.Text >= 2.5 Then 
       TextBox1.Text = 50 
      ElseIf TextBox31.Text >= 2.75 Then 
       TextBox1.Text = 50 
      ElseIf TextBox31.Text >= 3 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 3.25 Then 
       TextBox1.Text = 0 

      End If 


     Case 25 
      If TextBox31.Text >= 2.25 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 2.5 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 2.75 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 3 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 3.25 Then 
       TextBox1.Text = 0 

      End If 

    End Select 

,但是當我在組合框和文本框31 TextBox1的didnt響應或我想要什麼iput要得到。

+1

'選擇案例 「C」'不正確。它應該是'Select Case c' – snoopen

+0

我只是刪除了「」我都一樣。 – Hawiie13

+0

如果您使用的是Visual Studio,這是VB.net而不是VBA。檢查你的變量類型。我認爲用VB.net你可以依賴它爲你鑄造,但是很好意識到你將c作爲字符串與Case 100作爲Integer進行比較。 – snoopen

回答

0

你使用有嵌套有點超過我通常是舒適的邏輯。在這種情況下,你可以考慮更多的東西是這樣的:

Dim c As Integer 
    Dim aintValues() As Integer = {0, 25, 50, 75, 100} 

    If IsNumeric(ComboBox1.Text) And IsNumeric(TextBox31.Text) Then 
     c = ComboBox1.Text 
    Else 
     Exit Sub 
    End If 

    If c <= 25 Then 
     aintValues = {0, 25, 25, 25, 25} 
    ElseIf c <= 50 Then 
     aintValues = {0, 25, 50, 50, 50} 
    ElseIf c <= 75 Then 
     aintValues = {0, 25, 50, 75, 75} 
    End If 

    If TextBox31.Text >= 3.25 Then 
     TextBox1.Text = aintValues(0) 
    ElseIf TextBox31.Text >= 3.0 Then 
     TextBox1.Text = aintValues(1) 
    ElseIf TextBox31.Text >= 2.75 Then 
     TextBox1.Text = aintValues(2) 
    ElseIf TextBox31.Text >= 2.5 Then 
     TextBox1.Text = aintValues(3) 
    ElseIf TextBox31.Text >= 2.25 Then 
     TextBox1.Text = aintValues(4) 
    ElseIf TextBox31.Text >= 0 Then 
     TextBox1.Text = 42 
    Else 
     ' negative 
    End If 

或者如下你仍然可以使用select情況:

Dim c As Integer 
    Dim aintValues() As Integer = {0, 25, 50, 75, 100} 

    If IsNumeric(ComboBox1.Text) And IsNumeric(TextBox31.Text) Then 
     c = ComboBox1.Text 
    Else 
     Exit Sub 
    End If 

    If c <= 25 Then 
     aintValues = {0, 25, 25, 25, 25} 
    ElseIf c <= 50 Then 
     aintValues = {0, 25, 50, 50, 50} 
    ElseIf c <= 75 Then 
     aintValues = {0, 25, 50, 75, 75} 
    End If 

    Select Case TextBox31.Text 
     Case Is >= 3.25 
      TextBox1.Text = aintValues(0) 
     Case Is >= 3.0 
      TextBox1.Text = aintValues(1) 
     Case Is >= 2.75 
      TextBox1.Text = aintValues(2) 
     Case Is >= 2.5 
      TextBox1.Text = aintValues(3) 
     Case Is >= 2.25 
      TextBox1.Text = aintValues(4) 
     Case Is >= 0 
      TextBox1.Text = 42 
     Case Else 
      ' negative 
    End Select 
+0

我試過這也是它也 但我調整的Textbox31.Text的值從2.25到1,2.5到2.25等等 但我沒有了解很多代碼。 它是如何完成的。 謝謝我試着理解,所以我有新的知識。 :) – Hawiie13

0

有與您的代碼一對夫婦的問題,而是一個地方開始會明白,你的條件

Case 100 
    If TextBox31.Text >= 2.25 Then 
     TextBox1.Text = 100 
    ElseIf TextBox31.Text >= 2.5 Then 
     TextBox1.Text = 75 
    ElseIf TextBox31.Text >= 2.75 Then 
     TextBox1.Text = 50 
    ElseIf TextBox31.Text >= 3 Then 
     TextBox1.Text = 25 
    ElseIf TextBox31.Text >= 3.25 Then 
     TextBox1.Text = 0 
    End If 

在功能上等同於

Case 100 
    If TextBox31.Text >= 2.25 Then 
     TextBox1.Text = 100 
    End If 

的其他條件永遠不會因爲測試如果TextBox31.Text是大於或等於2.25的任何值,則它會通過,我們就完成了。

解決這個問題,你可以扭轉你的條件的順序,即

Case 100 
    If TextBox31.Text >= 3.25 Then 
     TextBox1.Text = 0 
    ElseIf TextBox31.Text >= 3 Then 
     TextBox1.Text = 25 
    ElseIf TextBox31.Text >= 2.75 Then 
     TextBox1.Text = 50 
    ElseIf TextBox31.Text >= 2.5 Then 
     TextBox1.Text = 75 
    ElseIf TextBox31.Text >= 2.25 Then 
     TextBox1.Text = 100 
    End If 

此外,這聽起來像你想覆蓋其中TextBox31.Text < 2.25,你可以用一個Else

做的情況
Case 100 
    If TextBox31.Text >= 3.25 Then 
     TextBox1.Text = 0 
    ElseIf TextBox31.Text >= 3 Then 
     TextBox1.Text = 25 
    ElseIf TextBox31.Text >= 2.75 Then 
     TextBox1.Text = 50 
    ElseIf TextBox31.Text >= 2.5 Then 
     TextBox1.Text = 75 
    ElseIf TextBox31.Text >= 2.25 Then 
     TextBox1.Text = 100 
    Else 
     TextBox1.Text = 100000 
    End If 
+0

Mr.sfletche '情況下,我會更新100 如果TextBox31.Text> = 3.25然後 TextBox31.Text = 0 elseif的TextBox31.Text> = 3然後 TextBox1.Text = 25 elseif的TextBox31.Text> = 2.75然後 TextBox1.Text = 50 elseif的TextBox31.Text> = 2.5然後 TextBox1.Text = 75 elseif的TextBox31.Text> = 2.25然後 TextBox1.Text = 100 結束If' 什麼也沒有發生 – Hawiie13

+0

@HarryReyes:這可能是因爲'TextBox31.Text'做不包含值> = 2.25(在這種情況下,它什麼都不做) – sfletche

+0

我只是現在編輯,但沒有。 我只想做 了'ComboBox1.Text'是以前的獎學金比例 那麼'Textbox31.Text'是檔次。 'Textbox1.Text'是他/她得到的新獎學金比例 繼承人我想要做什麼 當以前的獎學金百分比是100並且等級不會降低到2.25新的獎學金百分比維持但是如果等級低於2.25的新的獎學金比例將下降到75個 – Hawiie13

0

我不知道你的腳本是什麼,但有一點可能是你的失敗:

第一if..then-S語句設置TextBox31的文本而不是TextBox1。 我寧願這樣寫:

If TextBox31.Text >= 2.25 Then 
    TextBox1.Text = 100 
+0

我的代碼爲約 的'ComboBox1.Text'是以前學術百分比 那麼'Textbox31.Text'是年級。 'Textbox1.Text'是他/她得到的新獎學金比例 繼承人我想要做什麼 當以前的獎學金百分比是100並且等級不會降低到2.25新的獎學金百分比維持但是如果等級低於2.25新的獎學金比例將下降到75等 – Hawiie13

0

我解決現在的問題 我用這個代碼:

Case 100 
      If TextBox31.Text >= 3 Then 
       TextBox1.Text = 0 
      ElseIf TextBox31.Text >= 2.75 Then 
       TextBox1.Text = 25 
      ElseIf TextBox31.Text >= 2.5 Then 
       TextBox1.Text = 50 
      ElseIf TextBox31.Text >= 2.25 Then 
       TextBox1.Text = 75 
      ElseIf TextBox31.Text >= 1 Then 
       TextBox1.Text = 100 
      Else 
       TextBox1.Text = 0 

謝謝Mr.sfletche不放棄幫助我。 同樣的,其他誰幫我thanyo非常 Mr.sfletche如果我有這方面的其他問題,你會幫助我在福特

非常感謝你們

0

這是數學序列的問題。你不能在這裏使用if語句。如果是這樣,不幸的是,當工作修改發生時你會精神失常。

'Example Data 
    Dim c = "50" 
    Dim Text = "3.12" 

    Dim value As Double 
    Double.TryParse(Text, value) 

    Dim num = Math.Ceiling((3.25 - value)/0.25) 

    Console.WriteLine(Math.Min(Integer.parseInt(c), 25 * num)) 

我假設你想要做的就是上述。