webForumDet fria alternativet

Summa av label

.NET

6 svar · 533 visningar · startad av liverpool

Medlem sedan nov. 200468 inlägg
Frågan#1

Hej

Jag vill summera min värden i labels? Någon som vet


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = TextBox1.Text * ("5,5").ToString
        Label2.Text = TextBox2.Text * ("0,75").ToString
        Label3.Text = TextBox3.Text * ("1,4").ToString
        Label4.Text = TextBox4.Text * ("0,4").ToString
        Label5.Text = Här vill jag summera Label 1+2+3+4
    End Sub
Medlem sedan jan. 2001950 inlägg
#2

Label1.Text = (float.Parse(TextBox1.Text) * 5,5).ToString();
osv

Label5.Text = (float.Parse(Label1.Text) + ... + float.Parse(Label4.Text)).ToString();

Lite hjälp på vägen iaf.

Medlem sedan nov. 200468 inlägg
#3

Är detta skrivet i C#. Jag använder VB. Får upp ett felmeddelande där det står:
float is not declare?

Medlem sedan nov. 200468 inlägg
#4

Hej

Kanske världens bästa kod men det fungerar. Men summa blir i heltal. Jag vill ha med 2 decimaler. Någon som vet ?

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = TextBox1.Text * ("5,5").ToString
        Label2.Text = TextBox2.Text * ("0,75").ToString
        Label3.Text = TextBox3.Text * ("1,4").ToString
        Label4.Text = TextBox4.Text * ("0,4").ToString
        Label5.Text = TextBox5.Text * ("1").ToString
        
        Dim intTotal As Integer
        Dim int1 As Integer
        Dim int2 As Integer
        Dim int3 As Integer
        Dim int4 As Integer
        Dim int5 As Integer
        
        ' Get the values from the input boxes.
        int1 = CInt(Label1.Text)
        int2 = CInt(Label2.Text)
        int3 = CInt(Label3.Text)
        int4 = CInt(Label4.Text)
        int5 = CInt(Label5.Text)
        ' Get the total and display it.
        intTotal = int1 + int2 + int3 + int4 + int5
        Label13.Text = CStr(intTotal) **** Här vill jag ändra svaret till 2 decimaler!!
        
        
    End Sub
Medlem sedan dec. 2005664 inlägg
#5

Integer är heltal, testa decimal, float eller double

Medlem sedan nov. 200468 inlägg
#6

Nope. Samma problem

Medlem sedan nov. 200468 inlägg
#7

Löste det med Dim int1 As Decimal och med CDec istället för CInt

247 ms totalt · 4 externa anrop · v20260731065814-full.86ec41c2
120 ms — deklarationer (db)
0 ms — hämta statistik (cache)
125 ms — hämta tråd, inlägg och bilagor (db)
119 ms — ändringar (db)