Spero che l'autore del programma non ne avrà a male se ho pasticciato un po' il suo codice.
Per le temperature non ho trovato dei valori universali da caricare in c1 e c2 che soddisfino tutte le combinazioni.
Ho apportato modifiche solo nelle conversioni delle temperature ed evitato che sul secondo combobox si carichi la stessa unità di misura del primo combobox.
Ora le temperature vengono correttamente convertite, per le conversioni delle altre grandezze non ho eseguito alcun controllo, deve essere implementato il rifiuto dell'inserimrnto delle lettere.
Posto il sorgente modificato poiché il progetto allegato non è più in vb .net 2005 come l'originale ma in vb .net 2010.
Public Class Form1
Dim Nome As String = ""
' Modificato separatore da "-" a ";" per poter inserire un valore negativo
Dim Lunghezza() As String = {"Lunghezza", "AnnoLuce;9461000000000000", "Pollice;0,025399986", "Centimetro;0,01", "Piede;0,3048", "Metro;1", _
"Yarda;0,9144", "Miglio Terrestre;1609,34", "Kilometro;1000", "Miglio Marino;1852", "Li;500", "Jou;3,03", "Millimetro;0,001"}
Dim Superficie() As String = {"Superficie", "Acro;4046,86", "Kilometroq;1000000", "Metroq;1", "Ara;100", "Centiara;100", "Polliceq;0,00064516", "Piedeq;0,092903", _
"Centimetroq;0,0001", "Miglio terrestreq;2590000", "Yardaq;0,9999999613"}
Dim Capacità() As String = {"Capacita", "Gallone Usa;3,78541", "Litro;1", "Gallone Uk;4,54608785742778", "Ettolitro;100", "Pinta USA;0,473176249991928", "Quarto USA;0,946352499983856"}
Dim Velocità() As String = {"Velocità", "Kilometri / Ora;1", "Metri / Secondo;3,6", "Miglia terrestri / Ora;2,23694", "Nodo;1,9438477170141"}
Dim Peso() As String = {"Peso", "Oncia;28,3495", "Grammo;1", "Libbra;453,592000004704", "Ettogrammo;100", "Kilogrammo;1000", "Mace;3,77994", "Jin;600", "Tonnellata;1000000", "Quintale;100000"}
Dim Pressione() As String = {"Pressione", "Atmosfera;1,01325", "Pascal;0,00001", "Bar;0,986923", "Torr;0,001315789117884332", "Libbra x polliceq;0,06804594551879439"}
Dim Potenza() As String = {"Potenza", "Cavallo Vapore;1", "Watt;0,00135962"}
Dim Temperatura() As String = {"Temperatura", "Fahrenheit;1,8", "Celsius;-273,15", "Kelvin;273,15"}
Dim Forza() As String = {"Forza,", "Newton;1", "Kilogrammo forza; 0.1020"}
Dim MainArr()() As String = {Lunghezza, Superficie, Capacità, Velocità, Peso, Pressione, Potenza, Temperatura, Forza}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'carico il primo combobox
For Each a() As String In MainArr
For k As Byte = 1 To a.Length - 1
Dim splt() As String = Split(a(k), ";") ' Modificato separatore da "-" a ";" per poter inserire un valore negativo
cbx1.Items.Add(splt(0))
Next
Next
End Sub
'******************************************************
'* Carico il secondo combobox con gli elementi dello *
'* stesso tipo di quello scelto con il primo combobox *
'******************************************************
Private Sub cbx1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx1.SelectedIndexChanged
Dim a() As String
cbx2.Text = ""
cbx2.Items.Clear()
tbxResult.Text = ""
Do
For Each a In MainArr
For k As Byte = 1 To a.Length - 1
Dim d() As String = Split(a(k), ";") ' Modificato separatore da "-" a ";" per poter inserire un valore negativo
If d(0) = cbx1.Text Then
Nome = a(0)
Exit Do
End If
Next
Next
Loop
Select Case Nome
Case "Lunghezza"
a = Lunghezza
Case "Superficie"
a = Superficie
Case "Capacità"
a = Capacità
Case "Velocità"
a = Velocità
Case "Peso"
a = Peso
Case "Pressione"
a = Pressione
Case "Potenza"
a = Potenza
Case "Temperatura"
a = Temperatura
Case "Forza"
a = Forza
End Select
For kkk As Byte = 1 To a.Length - 1
Dim splt() As String = Split(a(kkk), ";") ' Modificato separatore da "-" a ";" per poter inserire un valore negativo
If cbx1.SelectedItem <> splt(0) Then cbx2.Items.Add(splt(0)) ' Modificato per evitare di inserire se stesso
Next
End Sub
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
If String.IsNullOrEmpty(tbxQty.Text) Then
tbxQty.Text = "1"
End If
DoConvert()
End Sub
Sub DoConvert()
If Not String.IsNullOrEmpty(cbx1.Text) AndAlso Not String.IsNullOrEmpty(cbx2.Text) Then
Select Case Nome
Case "Lunghezza"
ConvertAll(Lunghezza, cbx1, cbx2, tbxResult)
Case "Superficie"
ConvertAll(Superficie, cbx1, cbx2, tbxResult)
Case "Capacità"
ConvertAll(Capacità, cbx1, cbx2, tbxResult)
Case "Velocità"
ConvertAll(Velocità, cbx1, cbx2, tbxResult)
Case "Peso"
ConvertAll(Peso, cbx1, cbx2, tbxResult)
Case "Pressione"
ConvertAll(Pressione, cbx1, cbx2, tbxResult)
Case "Potenza"
ConvertAll(Potenza, cbx1, cbx2, tbxResult)
Case "Temperatura"
ConvertAll(Temperatura, cbx1, cbx2, tbxResult)
Case "Forza"
ConvertAll(Forza, cbx1, cbx2, tbxResult)
End Select
Else
MsgBox("SCEGLIERE DUE GRANDEZZE!")
End If
End Sub
Sub ConvertAll(ByVal myarr() As String, ByVal cb1 As ComboBox, ByVal cb2 As ComboBox, ByVal rs As TextBox)
Dim c1, c2 As Double
For Each s As String In myarr
Dim splt() As String = Split(s, ";") ' Modificato separatore da "-" a ";" per poter inserire un valore negativo
If splt(0) = cb1.Text Then
c1 = Convert.ToDouble(splt(1))
Exit For
End If
Next
For Each s As String In myarr
Dim splt() As String = Split(s, ";") ' Modificato separatore da "-" a ";" per poter inserire un valore negativo
If splt(0) = cb2.Text Then
c2 = Convert.ToDouble(splt(1))
Exit For
End If
Next
If Nome <> "Temperatura" Then
rs.Text = Convert.ToDouble(tbxQty.Text.Replace(".", ",")) * (c1 / c2) ' Modificato per accettare sia la virgola che il punto decimale
Else
Dim app As Double = Convert.ToDouble(tbxQty.Text.Replace(".", ",")) ' Modificato per accettare sia la virgola che il punto decimale
' aggiunta/modificata routine conversione
If cbx1.Text = "Kelvin" And app < 0 Then MsgBox("IN QUESTO CASO IL VALORE DI TEMPERATURA KELVIN NON PUO' ESSERE < 0!") : Exit Sub
If cbx1.Text = "Celsius" And app < -273.15 Then MsgBox("IN QUESTO CASO IL VALORE DI TEMPERATURA CELSIUS NON PUO' ESSERE < -273.15!") : Exit Sub
If cbx1.Text = "Fahrenheit" And app < -459.67 Then MsgBox("IN QUESTO CASO IL VALORE DI TEMPERATURA FAHRENHEIT NON PUO' ESSERE < -459.67!") : Exit Sub
If cbx1.Text = "Kelvin" And cbx2.Text = "Fahrenheit" Then
rs.Text = Math.Round(app * c2 - 459.67, 3)
ElseIf cbx1.Text = "Celsius" And cbx2.Text = "Fahrenheit" Then
rs.Text = Math.Round(app * c2 + 32, 3)
ElseIf cbx1.Text = "Fahrenheit" And cbx2.Text = "Celsius" Then
rs.Text = Math.Round((app - 32) / c1, 3)
ElseIf cbx1.Text = "Fahrenheit" And cbx2.Text = "Kelvin" Then
rs.Text = Math.Round((app - 32) / c1 + c2, 3)
Else ' Kelvin -> Celsius, Celsius -> Kelvin
rs.Text = Math.Round(app + c2, 3)
End If
End If
End Sub
Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
Application.Exit()
End Sub
Private Sub tbxQty_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbxQty.TextChanged
tbxResult.Text = ""
End Sub
Private Sub cbx2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx2.SelectedIndexChanged
tbxResult.Text = ""
End Sub
End Class