Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to have a Red progress bar for my battle gui form. but whenichange the colorto red, usingcode i found from some tutorial, there is like a delay on pressing the"attack button"
what it does is it will lowere my Health by 5 and lower the progressbar value by 5, forming a sort of health bar. but when the color is RED this is what happens:

health= 100
PB.value = 100
"Click Attack"
Health = 95
PB.Value = 100
"Click Attack"
Pb.Value = 95
Health = 90

when it is Green this is what happens:
health= 100
PB.value = 100
"Click Attack"
Health = 95
PB.Value = 95
"Click Attack"
Pb.Value = 90
Health = 90
i would like for what happens when the PB is Green, to be the same as when its red, will paste my code for my form below.
VB
Public Class lblMobH

    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Enum ProgressBarColor
        Green = &H1
        Red = &H2
        Yellow = &H3
    End Enum
    Private Shared Sub ChangeProgBarColor(ByVal pbhealth As Windows.Forms.ProgressBar, ByVal ProgressBar_Color As ProgressBarColor)
        SendMessage(pbhealth.Handle, &H410, ProgressBar_Color, 0)
    End Sub
 
    Private Sub btnAttack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttack.Click
        If Module1.health > 100 Then
            Module1.health = 100
            pbHealth.Value = 100
        End If
        ' Module1.health = Module1.health

        Label1.Text = PotionUsed
        'you attacking mob
        crit = (10 * Rnd()) * Now.Millisecond / 1000
        If crit = 6 Then
            attack += 25%
        End If
        If Module1.PotionUsed = 0 Then

            If pbMobHealth.Value > 0 Then
                If attack <= pbMobHealth.Value Then
                    pbMobHealth.Value -= attack
                    Module1.mhealth -= attack

                    TextBox.Text = TextBox.Text & vbNewLine & "You Attacked! -" & attack & "Health" & health & " " & pbHealth.Value & " " & pbMobHealth.Value
                    attack = 5
                    Module1.health = Module1.health

                Else
                    pbMobHealth.Value = 0
                    Module1.mhealth = 0
                    TextBox.Text = TextBox.Text & vbNewLine & "You Attacked! -" & attack & "Health" & health & " " & pbHealth.Value & " " & pbMobHealth.Value
                    attack = 5
                    Module1.health = Module1.health

                End If
            ElseIf pbMobHealth.Value < attack Then
                pbMobHealth.Value = 0
                Module1.health = Module1.health

            End If
            lblmhealth.Text = mhealth
            'mob attacking you


            mcrit = (10 * Rnd()) * Now.Millisecond / 1000

            If mcrit = 6 Then
                mattack += 25%
            End If

            If pbHealth.Value > 0 Then
                If mattack <= pbHealth.Value Then
                    pbHealth.Value -= mattack
                    Module1.health -= mattack
                    TextBox.ForeColor = Color.Red
                    TextBox.Text = TextBox.Text & vbNewLine & "Ogre Attacked! -" & mattack & "Health " & health & " " & pbHealth.Value & " " & pbMobHealth.Value
                    mattack = 5

                Else
                    pbHealth.Value = 0
                    Module1.health = 0
                    TextBox.Text = TextBox.Text & vbNewLine & "Ogre Attacked! -" & mattack & "Health" & health & " " & pbHealth.Value & " " & pbMobHealth.Value
                    mattack = 5

                End If
            ElseIf pbHealth.Value < mattack Then
                pbHealth.Value = 0
            End If
                lblhealth.Text = health
            End If

        If PotionUsed = 1 Then
            If Module1.health > 75 Then
                Module1.health = 100
                pbHealth.Value = 100
                TextBox.Text = TextBox.Text & vbNewLine & "You used 1 Minor Potion ! Health + 15" & health
                Module1.PotionUsed = 0
                Module1.health = Module1.health
                pbHealth.Value = Module1.health
            Else
                pbHealth.Value += 15
                Module1.health += 15
                Module1.PotionUsed = 0
                TextBox.Text = TextBox.Text & vbNewLine & "You used 1 Minor Potion ! Health + 15" & health
                Module1.health = Module1.health
                
            End If
        End If
        Module1.health = Module1.health

        lblhealth.Text = health

        pbHealth.Value = lblhealth.Text
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SendMessage(pbMobHealth.Handle, 1500, 2, 0)

        ChangeProgBarColor(pbHealth, ProgressBarColor.Red)
        SendMessage(ProgressBar3.Handle, 1040, 3, 0)
    End Sub

    Private Sub btnpotion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPotion.Click
        Using PotionForm As New Form2() With {.InvHealth = health} 'Pass health to Form2. Could alternatively pass health to constructor, which I personally often prefer. Depends on the scenario though...

            PotionForm.ShowDialog()

            Dim updatedHealth As Integer = PotionForm.InvHealth 'Retrieve updated health

        End Using

    End Sub


    Private Sub btnDefend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDefend.Click

        If pbHealth.Value > 0 Then
            pbHealth.Value += -mattack
            health += -mattack / 2
            TextBox.Text = TextBox.Text & vbNewLine & "Ogre Attacked! -" & mattack / 2 & "Health"
            mattack = 5
        ElseIf pbHealth.Value < mattack Then
            pbHealth.Value = 0
        End If
        lblhealth.Text = health
    End Sub

   
End Class
Posted

1 solution

Hi ,
If your progress bar is not updating on time then you can use Application.DoEvents(). This Method
processes all Windows messages currently in the message queue.
Read this msdn page
http://msdn.microsoft.com/en-us//library/system.windows.forms.application.doevents.aspx[^]

Call this method at appropriate time.

Hope this might be helpful to you.

--
Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900