Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [V.B.6.0]problema istr condizionali...



    ho creato un programma che calcola il BMI:
    Peso/ ( altezza/100)^2

    volevo fare in modo che, ottenendo un risultato amggiore di un numero pre-stabilito, la label del risultato ei suo testo diventasse si un altro colore....

    riporto il codice:
    Codice:



    Option Explicit
    Dim Peso As Integer
    Dim Altezza As Integer
    Dim Risultato As Integer

    Private Sub cmdCalcola_Click()


    lblRisultato.Caption = Peso / (Altezza / 100) ^ 2
    Risultato = lblRisultato.Caption


    If 0 > Risultato <= 18.5 Then
    lblRisultato.ForeColor = &HFF00FF
    End If

    If 18.5 >= Risultato <= 25 Then
    lblRisultato.ForeColor = &HFF0000
    End If

    If 25 <= Risultato >= 30 Then
    lblRisultato.ForeColor = &H400040
    End If

    If 30 <= Risultato >= 40 Then
    lblRisultato.ForeColor = &HFF00&
    End If

    If Risultato >= 40 Then
    lblRisultato.ForeColor = &HFF&
    End If


    End Sub

    Private Sub cmdReset_Click()
    lblRisultato.Caption = ""
    txtAltezza.Text = ""
    txtPeso.Text = ""

    End Sub

    Private Sub txtAltezza_Change()
    Altezza = txtAltezza.Text
    End Sub

    Private Sub txtPeso_Change()
    Peso = txtPeso.Text
    End Sub








    vorrei che cambiassero i colori come in queta tabella:




    http://www.adieta.it/bmi.html

  2. #2
    Utente di HTML.it L'avatar di LMondi
    Registrato dal
    Sep 2004
    Messaggi
    1,291
    Se usi Select Case tutto si semplifica:
    Private Sub cmdCalcola_Click()
    Risultato.Text = Format(Ctr(Peso.Text) / Ctr((Altezza.Text / 100) ^ 2), "#,##0.0")
    Select Case (Risultato)
    Case 0 To 18.5
    Risultato.ForeColor = &HFF00FF
    Case 18.6 To 25
    Risultato.ForeColor = &HFF0000
    Case 25.1 To 30
    Risultato.ForeColor = &H800000
    Case 30.1 To 40
    Risultato.ForeColor = &HFF00&
    Case Is > 40
    Risultato.ForeColor = &HFF&
    End Select
    End Sub
    '------------------------------------------------
    'Trasforma la stringa di testo in Numero Double:
    Public Function Ctr(strTesto As String) As Double
    On Error Resume Next
    If IsNumeric(strTesto) Then
    Ctr = CDbl(strTesto)
    Else: Ctr = 0
    End If
    End Function
    In questo modo puoi evitare di dichiarare anche le variabili.
    Ho aggiunto la su esposta funzione per il calcolo dell'algoritmo peso/altezza^2 in modo da trasformare la stringa del Txt in numero Double.
    Mi sembra che funzioni.
    LM

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.