查看完整版本: visual basic 2015問題 急~
頁: [1]

happyjimmy00000 發表於 2018-1-1 06:25 PM

visual basic 2015問題 急~

本帖最後由 darkjack 於 2018-1-6 05:59 PM 編輯

請問如果想要把下列每個熱量的數值直接總和在一個TEXTBOX 內請問要怎麼寫程式碼?
以下附件截圖為的目前執行出來的程式檔




以下是目前程式碼

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim clickedCount = 0
        If Button1.Tag <> Nothing Then
            Int32.TryParse(Button1.Tag.ToString(), clickedCount)
        End If
        clickedCount += 1
        DoSomethingAccordingToClickedCount(clickedCount)
        Button1.Tag = clickedCount
    End Sub

    Private Sub DoSomethingAccordingToClickedCount(clickedCount As Integer)
        'Todo:
        If clickedCount = 1 Then
            Randomize()
            Select Case Int((3) * Rnd())
                Case Is = 0
                    Label1.Text = "麵 350大卡"
                Case Is = 1
                    Label1.Text = "飯 370大卡"
                Case Is = 2
                    Label1.Text = "水餃 400大卡"
            End Select

        End If

        If clickedCount = 2 Then
            Randomize()
            Select Case Int((3) * Rnd())
                Case Is = 0
                    Label2.Text = "空心菜  150大卡"
                Case Is = 1
                    Label2.Text = "湯匙菜  160大卡"
                Case Is = 2
                    Label2.Text = "高麗菜  180大卡"
            End Select
        End If
        If clickedCount = 3 Then
            Randomize()
            Select Case Int((3) * Rnd())
                Case Is = 0
                    Label3.Text = "蔥爆牛肉  250大卡"
                Case Is = 1
                    Label3.Text = "炸排骨  280大卡"
                Case Is = 2
                    Label3.Text = "三杯雞  260大卡"
            End Select
        End If
        If clickedCount = 4 Then
            Randomize()
            Select Case Int((3) * Rnd())
                Case Is = 0
                    Label4.Text = "涼拌黃瓜  180大卡"
                Case Is = 1
                    Label4.Text = "皮蛋豆腐  190大卡"
                Case Is = 2
                    Label4.Text = "海澤皮  150大卡"
            End Select
        End If
        If clickedCount = 5 Then
            Randomize()
            Select Case Int((3) * Rnd())
                Case Is = 0
                    Label5.Text = "南瓜湯  250大卡"
                Case Is = 1
                    Label5.Text = "玉米濃湯  230大卡"
                Case Is = 2
                    Label5.Text = "紫菜蛋花湯  150大卡"
            End Select
        End If
    End Sub
End Class


小弟真的不知道該如何修改程式碼,跪求大大解答,很急~~謝謝

...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

sggleeee 發表於 2018-1-6 01:19 PM

首先, 請先Imports System.Text.RegularExpressions

再來請在您的Button1_Click 那個 Subroutine 的 Button1.Tag = clickedCount 那行底下加入如下代碼, 我想應該就能達到您的需求...
If clickedCount <= 5 Then
            TextBox1.Text = (Val(Regex.Replace(TextBox1.Text, "[^0-9]", "")) +
                             Val(Regex.Replace(Me.Controls("Label" & _
                             clickedCount.ToString()).Text, "[^0-9]", ""))) & "大卡"
End If
頁: [1]