2015-09-14 59 views
0

我有一個計算,其中我將面板數乘以瓦特數,並將輸出轉換爲KW。這是我的VB代碼。計算結果,如果組合框的值已更改

Total_watt = (Watt_In * Npanel)/1000.0 

如果我更改DomainUpDown,將會生成結果。如果我選擇一個ComboBox項目,值不會改變。我已經添加了幾行來處理組合框,但我正在逐漸

「」類型的異常出現在Microsoft.VisualBasic.dll中,但在用戶代碼中 其他信息沒有處理錯誤:轉換從字符串「」到類型「整數」無效。

沒有ComboBox代碼,它工作正常,但無法從ComboBox中獲取數據。

Public Class Solar_Panel 
    Dim Total_watt As Double 
    Dim Npanel As Integer 
    Dim Watt_In As Integer 
    Private Sub Solar_Panel_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     RadioButton_Direct_Data_In.Select() 
     Watt_In = CInt(ComboBox_PanelWatt.SelectedItem) 
     Npanel = CInt(DomainUpDown_NModule.Text) 
     Total_watt = (Watt_In * Npanel)/1000.0 
     Label_Tout.Text = CStr(Total_watt) 
    End Sub 

    Private Sub DomainUpDown_NModule_SelectedItemChanged(sender As Object, e As EventArgs) Handles DomainUpDown_NModule.SelectedItemChanged 
     Watt_In = CInt(ComboBox_PanelWatt.SelectedItem) 
     Npanel = CInt(DomainUpDown_NModule.Text) 
     Total_watt = (Watt_In * Npanel)/1000.0 
     Label_Tout.Text = CStr(Total_watt) 
    End Sub 

    Private Sub ComboBox_PanelWatt_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_PanelWatt.SelectedIndexChanged 
     If ComboBox_PanelWatt.Text = True Then 
      Watt_In = CInt(ComboBox_PanelWatt.SelectedItem) 
      Npanel = CInt(DomainUpDown_NModule.Text) 
      Total_watt = (Watt_In * Npanel)/1000.0 
      Label_Tout.Text = CStr(Total_watt) 
     End If 
    End Sub 
End Class 

回答

0

你的錯誤可能是試圖在沒有ComboBox項已被選中的文本轉換在ComboBox成整數的結果(等Text屬性是一個空字符串)。

我有幾個建議,我認爲會改善你的類:

  1. ,你要正確對待它的值爲數字使用NumericUpDown控件,而不是一個DomainUpDown。
  2. 將計算總瓦數的代碼移動到Sub,以便您只需編寫一次代碼即可。
  3. 如果ComboBox文本爲空,則跳過計算。
  4. 使用Integer.TryParse驗證ComboBox文本並將其轉換爲整數。
  5. 如果您允許用戶在ComboBox中鍵入自己的值,則需要處理ComboBox的TextChanged事件而不是SelectedIndexChanged事件。

    Public Class Solar_Panel 
        Private TotalKWatts As Double 
        Private Watt_In, NPanel As Integer 
    
        Private Sub Solar_Panel_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
         'The NumericUpDown properties could be set in the designer instead of here 
         NumericUpDown_NModule.Minimum = 0 
         NumericUpDown_NModule.Maximum = 100 
         NumericUpDown_NModule.DecimalPlaces = 0 
         NumericUpDown_NModule.Increment = 1 
    
         RadioButton_Direct_Data_In.Select() 
         GetTotalWatts 
        End Sub 
    
        Sub NumericUpDown_NModule_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown_NModule.ValueChanged 
         GetTotalWatts 
        End Sub 
    
        Sub ComboBox_PanelWatt_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox_PanelWatt.SelectedIndexChanged 
         GetTotalWatts 
        End Sub 
    
        Private Sub GetTotalWatts()  
         If ComboBox_PanelWatt.Text = "" Then Exit Sub 
         If Integer.TryParse(ComboBox_PanelWatt.Text, Watt_In) Then 
          NPanel = CInt(NumericUpDown_NModule.Value) 
          TotalKWatts = NPanel * Watt_In/1000 
          Label_Tout.Text = TotalKWatts.ToString() 
         Else 
          Label_Tout.Text = "Invalid wattage" 
         End If 
        End Sub 
    End Class