我是编码和尝试的新手。我希望有人可以提供帮助。我有一个健康栏的代码,表示攻击和治疗。但是,我想创建一个文本框,以数字格式指示运行状况。我不知道如何处理这个问题。 txtHP.text是我想要出现数字的地方。到目前为止,这是我的代码:
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 :(得分:4)
创建一个文本框,然后分配Text
属性,如下所示:
TextBox1.Text = HpBarG.ToString()
如果要将值显示为百分比,请执行以下操作:
TextBox1.Text = HpBarG.ToString("P")
有关将数字格式化为字符串的不同方法的详细信息,请阅读Standard Numeric Format Strings。