Option Strict On Public Class Form1 'unités et valeurs Dim unite As New List(Of String) From {"Mètre", "décimètre", "centimètre", "millimètre", "Décamètre", _ "Hectomètre", "Kilomètre", "Main (hand)", "doigt (finger)", _ "pied (foot)", "pouce (inch)", "coudée (cubit)", "pas (pace)", _ "verge (yard)", "aune (ell)", "brasse(fathom)", "toise", "perche", _ "arpent", "encablure", "mille", "mille américain", "mille marin", _ "lieue postale", "lieue terrestre", "lieue nautique", "année lumière"} Dim valeur As New List(Of String) From {"1", "0,1", "0,01", "0,001", "10", "100", "1000", "0,1016", "0,1143", _ "0,3048", "0,0254", "0,4572", "0,762", "0,9144", "1,186566", "1,8288", _ "1,94903631", "5,847108929", "443,296", "185,2", "1609,344", _ "1609,347219", "1852", "3898,07262", "4443,802786", "5556", _ "960730472580800"} 'contrôles à rajouter sur le form Dim WithEvents txtSaisie As New TextBox With {.Parent Me, .Location New Point(5, 5)} Dim WithEvents cmbUnite1 As New ComboBox With {.Parent Me, .Location New Point(120, 5)} Dim lblTexte As New Label With {.Parent Me, .Location New Point(250, 5), .Text = "font ", .AutoSize = True} Dim txtResultat As New TextBox With {.Parent Me, .Location New Point(300, 5)} Dim WithEvents cmbUnite2 As New ComboBox With {.Parent Me, .Location New Point(420, 5)} 'structure Private Structure Mesure Public Nom As String Public Valeur As String End Structure 'liste accueillant les instances de la structure Dim MesMesures As New List(Of Mesure) Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'redimensions du formulaire Me.Bounds = New Rectangle(Me.Left, Me.Top, 580, 100) 'boucle sur chaque élément For x As Integer = 0 To unite.Count - 1 'instance de la structure avec assignation des données Dim MaMesure As New Mesure With {.Nom unite(x), .Valeur valeur(x)} 'remplissage des combos cmbUnite1.Items.Add(MaMesure.Nom) cmbUnite2.Items.Add(MaMesure.Nom) 'ajout de l'instance à la liste MesMesures.Add(MaMesure) Next End Sub 'on fait pointer 3 événements sur cette procédure (voir Handles) ' 1 et 2 = l'index d'un combo changeant ' 3 = Le texte qui change sur txtSaisie Private Sub Affiche(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbUnite1.SelectedIndexChanged, cmbUnite2.SelectedIndexChanged, txtSaisie.TextChanged 'on affiche le résultat de la fonction calcul txtResultat.Text = Calcul.ToString End Sub Private Function Calcul() As Double Dim Origine, Resultat, Coef1, Coef2 As Double 'si rien n'est sélectionné dans les combos on sors If cmbUnite1.SelectedIndex -1 Or cmbUnite2.SelectedIndex -1 Then Return Nothing 'on obtiens ici les valeurs contenues dans la liste des instances en se servant 'des indexs sélectionnés dans les combo Double.TryParse(MesMesures(cmbUnite1.SelectedIndex).Valeur, Coef1) Double.TryParse(MesMesures(cmbUnite2.SelectedIndex).Valeur, Coef2) 'on tente de trnasformer le texte saisi en Double Double.TryParse(txtSaisie.Text, Origine) If Origine <> Nothing Then 'on fait le calcul Resultat = Origine * (Coef1 / Coef2) 'on retourne le résultat Return Resultat Else 'on ne retourne rien Return Nothing End If End Function End Class
Dim txtCoef1 As New TextBox With {.Parent Me, .Location New Point(120, 30)} Dim txtCoef2 As New TextBox With {.Parent Me, .Location New Point(420, 30)}
'on obtiens ici les valeurs contenues dans la liste des instances en se servant 'des indexs sélectionnés dans les combo Double.TryParse(MesMesures(cmbUnite1.SelectedIndex).Valeur, Coef1) txtCoef1.Text = MesMesures(cmbUnite1.SelectedIndex).Valeur.ToString Double.TryParse(MesMesures(cmbUnite2.SelectedIndex).Valeur, Coef2) txtCoef2.Text = MesMesures(cmbUnite2.SelectedIndex).Valeur.ToString
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
activer Option Strict
Option Strict On Option Explicit On Imports System.Collections.Generic Public Class Form1 Dim Noms() As String = {"Mètre", "décimètre", "centimètre", "millimètre", "Décamètre", _ "Hectomètre", "Kilomètre", "Main (hand)", "doigt (finger)", _ "pied (foot)", "pouce (inch)", "coudée (cubit)", "pas (pace)", _ "verge (yard)", "aune (ell)", "brasse(fathom)", "toise", "perche", _ "arpent", "encablure", "mille", "mille américain", "mille marin", _ "lieue postale", "lieue terrestre", "lieue nautique", "année lumière"} Dim Valeurs() As String = {"1", "0.1", "0.01", "0.001", "10", "100", "1000", "0.1016", "0.1143", _ "0.3048", "0.0254", "0.4572", "0.762", "0.9144", "1.186566", "1.8288", _ "1.94903631", "5.847108929", "25920/443.296", "185.2", "1609.344", _ "1609.347219", "1852", "3898.07262", "4443.802786", "5556", _ "960730472580800"} Dim coef As Long, Quantite As Double, element As Integer, element2 As Integer, Resultat As Double, _ decimales As Integer Structure Mesure Dim Nom As String Dim Valeur As String End Structure Dim unite As New List(Of Mesure)(Noms) Dim unite2 As New List(Of Mesure)(Valeurs) Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged ComboBox1.DataSource = unite End Sub Private Sub ComboBox2_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged ComboBox2.DataSource = unite2 End Sub Private Sub Chaine_Quantite_TextChanged(sender As System.Object, e As System.EventArgs) Handles Chaine_Quantite.TextChanged Quantite = Val(Chaine_Quantite) End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click If Quantite = 0 Then Quantite = 1 End If Resultat = Val(Coefficient_1.Text) / Val(Coefficient_2) * Quantite Chaine_Resultat.Text = CStr(Resultat) End Sub End Class
Dim unite As New List(Of String) From {"Mètre", "décimètre", "centimètre", "millimètre", "Décamètre", _ "Hectomètre", "Kilomètre", "Main (hand)", "doigt (finger)", _ "pied (foot)", "pouce (inch)", "coudée (cubit)", "pas (pace)", _ "verge (yard)", "aune (ell)", "brasse(fathom)", "toise", "perche", _ "arpent", "encablure", "mille", "mille américain", "mille marin", _ "lieue postale", "lieue terrestre", "lieue nautique", "année lumière"} Dim unite2 As New List(Of String) From {"1", "0.1", "0.01", "0.001", "10", "100", "1000", "0.1016", "0.1143", _ "0.3048", "0.0254", "0.4572", "0.762", "0.9144", "1.186566", "1.8288", _ "1.94903631", "5.847108929", "25920/443.296", "185.2", "1609.344", _ "1609.347219", "1852", "3898.07262", "4443.802786", "5556", _ "960730472580800"}
ComboBox1.DataSource = unite ComboBox2.DataSource = unite2
Option Strict On Option Explicit On Imports System.Collections.Generic Public Class Form1 Dim unite As New List(Of String) From {"Mètre", "décimètre", "centimètre", "millimètre", "Décamètre", _ "Hectomètre", "Kilomètre", "Main (hand)", "doigt (finger)", _ "pied (foot)", "pouce (inch)", "coudée (cubit)", "pas (pace)", _ "verge (yard)", "aune (ell)", "brasse(fathom)", "toise", "perche", _ "arpent", "encablure", "mille", "mille américain", "mille marin", _ "lieue postale", "lieue terrestre", "lieue nautique", "année lumière"} Dim unite2 As New List(Of String) From {"1", "0.1", "0.01", "0.001", "10", "100", "1000", "0.1016", "0.1143", _ "0.3048", "0.0254", "0.4572", "0.762", "0.9144", "1.186566", "1.8288", _ "1.94903631", "5.847108929", "25920/443.296", "185.2", "1609.344", _ "1609.347219", "1852", "3898.07262", "4443.802786", "5556", _ "960730472580800"} Dim coef As Long, Quantite As Double, element As Integer, element2 As Integer, Resultat As Double, _ decimales As Integer Private Sub Chaine_Quantite_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Chaine_Quantite.TextChanged Quantite = Val(Chaine_Quantite) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Quantite = 0 Then Quantite = 1 End If Resultat = Val(Coefficient_1.Text) / Val(Coefficient_2) * Quantite Chaine_Resultat.Text = CStr(Resultat) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBox1.DataSource = unite ComboBox2.DataSource = unite2 End Sub End Class
for i as integer = 0 to unite.length - 1 ComboBox1.items.add(unite(i)) ComboBox2.items.add(unite2(i)) next
Option Strict On Option Explicit On Imports System.Collections.Generic Public Class Form1 Dim cUnite As New List(Of String) From {"Mètre", "décimètre", "centimètre", "millimètre", "Décamètre", _ "Hectomètre", "Kilomètre", "Main (hand)", "doigt (finger)", _ "pied (foot)", "pouce (inch)", "coudée (cubit)", "pas (pace)", _ "verge (yard)", "aune (ell)", "brasse(fathom)", "toise", "perche", _ "arpent", "encablure", "mille", "mille américain", "mille marin", _ "lieue postale", "lieue terrestre", "lieue nautique", "année lumière"} Dim cValeur As New List(Of String) From {"1", "0.1", "0.01", "0.001", "10", "100", "1000", "0.1016", "0.1143", _ "0.3048", "0.0254", "0.4572", "0.762", "0.9144", "1.186566", "1.8288", _ "1.94903631", "5.847108929", "25920/443.296", "185.2", "1609.344", _ "1609.347219", "1852", "3898.07262", "4443.802786", "5556", _ "960730472580800"} Dim lCoeff As Long, dQuantite As Double, dResultat As Double Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ComboBox1.DataSource = cUnite ComboBox2.DataSource = cUnite End Sub Private Sub TextBoxCoefficient_1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxCoefficient_1.TextChanged TextBoxCoefficient_1.Text = CStr(ComboBox1.Text) End Sub Private Sub TextBoxCoefficient_2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxCoefficient_2.TextChanged TextBoxCoefficient_2.Text = CStr(ComboBox2.Text) End Sub Private Sub Chaine_Quantite_TextChanged(sender As System.Object, e As System.EventArgs) Handles Chaine_Quantite.TextChanged dQuantite = Val(Chaine_Quantite.Text) If dQuantite = 0 Then dQuantite = 1 End If End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click dResultat = Val(TextBoxCoefficient_1.Text) / Val(TextBoxCoefficient_2) * dQuantite Chaine_Resultat.Text = CStr(dResultat.ToString) End Sub End Class
Option Strict On Option Explicit On Imports System.Collections.Generic Public Class Form1 Dim cUnite As New List(Of String) From {"Mètre", "décimètre", "centimètre", "millimètre", "Décamètre", _ "Hectomètre", "Kilomètre", "Main (hand)", "doigt (finger)", _ "pied (foot)", "pouce (inch)", "coudée (cubit)", "pas (pace)", _ "verge (yard)", "aune (ell)", "brasse(fathom)", "toise", "perche", _ "arpent", "encablure", "mille", "mille américain", "mille marin", _ "lieue postale", "lieue terrestre", "lieue nautique", "année lumière"} Dim cValeur As New List(Of String) From {"1", "0.1", "0.01", "0.001", "10", "100", "1000", "0.1016", "0.1143", _ "0.3048", "0.0254", "0.4572", "0.762", "0.9144", "1.186566", "1.8288", _ "1.94903631", "5.847108929", "25920/443.296", "185.2", "1609.344", _ "1609.347219", "1852", "3898.07262", "4443.802786", "5556", _ "960730472580800"} Dim lCoeff As Long, dQuantite, dCoefficient1, dCoefficient2, dResultat As Double Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ComboBox1.DataSource = cUnite ComboBox2.DataSource = cUnite End Sub Private Sub TextBoxCoefficient_1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxCoefficient_1.TextChanged dCoefficient1 = CDbl(ComboBox1.Text) End Sub Private Sub TextBoxCoefficient_2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxCoefficient_2.TextChanged dCoefficient2 = CDbl(ComboBox2.Text) End Sub Private Sub Chaine_Quantite_TextChanged(sender As System.Object, e As System.EventArgs) Handles Chaine_Quantite.TextChanged dQuantite = Val(Chaine_Quantite.Text) If dQuantite = 0 Then dQuantite = 1 End If End Sub Private Sub BtnConvertir_Click(sender As System.Object, e As System.EventArgs) Handles BtnConvertir.Click dResultat = dCoefficient1 * dCoefficient2 * dQuantite Chaine_Resultat.Text = CStr(dResultat.ToString) End Sub End Class
ComboBox2.DataSource = cUnite
ComboBox2.DataSource = cValeur
Structure Mesure Nom As String Valeur As Double End Structure
dim UneMesure as structure