伊莉討論區

標題: visual basic 2015問題 急~ [打印本頁]

作者: happyjimmy00000    時間: 2018-1-1 06:25 PM     標題: visual basic 2015問題 急~

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

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

[attach]121687609[/attach]

以下是目前程式碼

  1. Public Class Form1

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

  3.         Dim clickedCount = 0
  4.         If Button1.Tag <> Nothing Then
  5.             Int32.TryParse(Button1.Tag.ToString(), clickedCount)
  6.         End If
  7.         clickedCount += 1
  8.         DoSomethingAccordingToClickedCount(clickedCount)
  9.         Button1.Tag = clickedCount
  10.     End Sub

  11.     Private Sub DoSomethingAccordingToClickedCount(clickedCount As Integer)
  12.         'Todo:
  13.         If clickedCount = 1 Then
  14.             Randomize()
  15.             Select Case Int((3) * Rnd())
  16.                 Case Is = 0
  17.                     Label1.Text = "麵 350大卡"
  18.                 Case Is = 1
  19.                     Label1.Text = "飯 370大卡"
  20.                 Case Is = 2
  21.                     Label1.Text = "水餃 400大卡"
  22.             End Select

  23.         End If

  24.         If clickedCount = 2 Then
  25.             Randomize()
  26.             Select Case Int((3) * Rnd())
  27.                 Case Is = 0
  28.                     Label2.Text = "空心菜  150大卡"
  29.                 Case Is = 1
  30.                     Label2.Text = "湯匙菜  160大卡"
  31.                 Case Is = 2
  32.                     Label2.Text = "高麗菜  180大卡"
  33.             End Select
  34.         End If
  35.         If clickedCount = 3 Then
  36.             Randomize()
  37.             Select Case Int((3) * Rnd())
  38.                 Case Is = 0
  39.                     Label3.Text = "蔥爆牛肉  250大卡"
  40.                 Case Is = 1
  41.                     Label3.Text = "炸排骨  280大卡"
  42.                 Case Is = 2
  43.                     Label3.Text = "三杯雞  260大卡"
  44.             End Select
  45.         End If
  46.         If clickedCount = 4 Then
  47.             Randomize()
  48.             Select Case Int((3) * Rnd())
  49.                 Case Is = 0
  50.                     Label4.Text = "涼拌黃瓜  180大卡"
  51.                 Case Is = 1
  52.                     Label4.Text = "皮蛋豆腐  190大卡"
  53.                 Case Is = 2
  54.                     Label4.Text = "海澤皮  150大卡"
  55.             End Select
  56.         End If
  57.         If clickedCount = 5 Then
  58.             Randomize()
  59.             Select Case Int((3) * Rnd())
  60.                 Case Is = 0
  61.                     Label5.Text = "南瓜湯  250大卡"
  62.                 Case Is = 1
  63.                     Label5.Text = "玉米濃湯  230大卡"
  64.                 Case Is = 2
  65.                     Label5.Text = "紫菜蛋花湯  150大卡"
  66.             End Select
  67.         End If
  68.     End Sub
  69. End Class
複製代碼



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


作者: sggleeee    時間: 2018-1-6 01:19 PM

首先, 請先Imports System.Text.RegularExpressions

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





歡迎光臨 伊莉討論區 (http://www49.eyny.com/) Powered by Discuz!