2013-12-11 68 views
1

我是新來的編碼和嘗試的東西了。我希望有人能幫忙。我有一個代碼表示攻擊和治療的健康欄。不過,我想創建一個文本框,以數字格式指示健康狀況。我不知道如何解決這個問題。該txtHP.text是我想要的數字出現。這裏是我的代碼到目前爲止:VB NET,累計損壞

Public Class Form1 
    Dim bteNombre As Integer 
    Public DMap As New Bitmap(Me.Width, Me.Height, Me.CreateGraphics) 
    Public GraphicsBuffer As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(DMap) 
    Public HpBarG As Integer = 100 
    Public HpBarR As Integer = 100 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
     Handles MyBase.Load 
     DrawHpBar() 
    End Sub 
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _ 
          System.Windows.Forms.PaintEventArgs) Handles Me.Paint 
     e.Graphics.DrawImage(DMap, 0, 0) 
    End Sub 
    Private Sub DrawHpBar() 
If HpBarG < 0 Then 
    HpBarG = 0 
End If 
If HpBarG > 100 Then 
    HpBarG = 100 
End If 
     GraphicsBuffer.DrawRectangle(Pens.Black, New Rectangle(47 - 1, 5 - 1, 100 + 1, 15)) 
     GraphicsBuffer.FillRectangle(Brushes.Red, New Rectangle(47, 5, HpBarR, 14)) 
     GraphicsBuffer.FillRectangle(Brushes.Lime, New Rectangle(47, 5, HpBarG, 14)) 
     Me.Invalidate(New Rectangle(47 - 1, 5 - 1, 100 + 1, 15)) 


    End Sub 

    Private Sub btnAttack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
       Handles btnAttack.Click 
     Randomize() 
     bteNombre = Int((10 - 1 + 1) * Rnd() + 1) 
     txtAtkHeal.Text = bteNombre.ToString 
     HpBarG -= txtAtkHeal.Text 
     DrawHpBar() 

    End Sub 

    Private Sub btnHeal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
       Handles btnHeal.Click 
     Randomize() 
     bteNombre = Int((5 - 1 + 1) * Rnd() + 1) 
     txtAtkHeal.Text = bteNombre.ToString 
     HpBarG += txtAtkHeal.Text 
     DrawHpBar() 
     txtHP.Text = bteNombre 

    End Sub 

End Class 

謝謝!

+0

'myTextBox.Text = HpBarG'? – woz

+0

感謝您發佈它,並且工作正常!並感謝您的幫助! – user3092463

回答

4

創建一個文本框,然後分配Text屬性,像這樣:

TextBox1.Text = HpBarG.ToString() 

如果你想顯示的數值爲百分比,那麼這樣做:

​​

有關將數字格式化爲字符串的不同方式的更多信息,請參閱Standard Numeric Format Strings

+0

+1但是,可能要刪除';',因爲他使用VB而不是C#= D –

+0

@Scott - 剛剛意識到並修復了它,每天都是C#/ JavaScrip開發人員的習慣。 :-) –

+0

LoL ...我知道你的意思。我一直這樣做! –