Visual Basic Studio

Discussion in 'Programming' started by Driesje44, Jun 18, 2015.

  1. Ianbow

    Ianbow Member

    LetsRaceBwoi, do you want to admit something? Something ALL of us should know about? Hmmm? ;)
    Anyways, XNA vs Windows Forms, XNA. God forbid something turn out as horrible as TSOEmu True.
  2. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    I admit that I do know how to use Visual Basic..?
    I don't see what I should be admitting to here.
  3. RHY3756547

    RHY3756547 FreeSO Developer Staff Member Moderator

    I already sort of know, Ianbow, I'm just letting it slide for now.
  4. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    Spooky.
  5. Driesje44

    Driesje44 Active Member

    Whell, i'm following the Microsoft Virtual Academy video lessons ;) Maybe in A few years i can help with PD XD
    The translation will stuk a bit now, 'cause i'm also having examinations ;)
    LetsRaceBwoi likes this.
  6. Driesje44

    Driesje44 Active Member

    Hi!

    So i'mm still learning Visual Basic 'cause i want to learn that first before XNA. And it' going pretty good! But now i want to make a simple animation with pictureboxes. So i used this code:
    Code:
    Public Class Form1
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            My.Settings.animation = My.Settings.animation + 1
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Enabled = True
            My.Settings.animation = 0
            Do Until My.Settings.animation = 4
                If My.Settings.animation = 1 Then
                    PictureBox1.Show()
                    PictureBox2.Hide()
                    PictureBox3.Hide()
                Else
                    If My.Settings.animation = 2 Then
    
                        PictureBox1.Hide()
                        PictureBox2.Show()
                        PictureBox3.Hide()
                    Else
                        If My.Settings.animation = 3 Then
                            PictureBox1.Hide()
                            PictureBox2.Hide()
                            PictureBox3.Show()
                        End If
    
                    End If
    
                End If
            Loop
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Show()
            PictureBox2.Hide()
            PictureBox3.Hide()
        End Sub
    End Class
    But when I click on the button, the whole program stops working :s, it's probably a stupid mistake I made, could someone tell me what I did wrong? Thanks!
    Last edited: Aug 11, 2015
  7. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    You should put your code in a CODE tag!
    To fix your problem: you're doing a Do Until loop where My.settings.animation will never be 4 because you aren't adding on to it each time.
  8. Driesje44

    Driesje44 Active Member

    Sorry! changed it!

    But in the first lines there is written that each tick my.settings.animation is my.settings.animations + 1, so i do add on it?
    Code:
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    My.Settings.animation = My.Settings.animation + 1
    End Sub
    
    And after clicking, i litteraly can't do anything, not minimize it, or closing it with the cross
  9. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    Timer1.Enabled doesn't start it AFAIK. Use Timer1.Start() - that always works for me.
  10. Driesje44

    Driesje44 Active Member

    Nope, it still crashes :/
    So this is my code now, 3 pictureboxes and the interval is 200
    Code:
    Public Class Form1
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            My.Settings.animation = My.Settings.animation + 1
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Start()
            My.Settings.animation = 0
            Do Until My.Settings.animation = 4
                If My.Settings.animation = 1 Then
                    PictureBox1.Show()
                    PictureBox2.Hide()
                    PictureBox3.Hide()
                Else
                    If My.Settings.animation = 2 Then
    
                        PictureBox1.Hide()
                        PictureBox2.Show()
                        PictureBox3.Hide()
                    Else
                        If My.Settings.animation = 3 Then
                            PictureBox1.Hide()
                            PictureBox2.Hide()
                            PictureBox3.Show()
                        End If
    
                    End If
    
                End If
            Loop
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Show()
            PictureBox2.Hide()
            PictureBox3.Hide()
        End Sub
    End Class
    
  11. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    There appears to be nothing wrong with your code.
  12. Driesje44

    Driesje44 Active Member

    But it still crashes! Do you really have no idea? :s
  13. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    I'll run the code myself when I get home. If you want you can zip the solution and send it but I don't necessarily need it.
  14. RHY3756547

    RHY3756547 FreeSO Developer Staff Member Moderator

    Your code busy loops, meaning that it will loop forever until another thread sets "My.Settings.animation" to 4. The timer element runs on the UI thread with everything else, so it'll never happen as the UI thread is stuck in the busy loop. (not that this was a good idea in the first place, since it'll slam your cpu either way)

    You want to move the animation handling to the timer. If your animation variable is less than 4 when the animation starts, do the animation state switch and increment it by one (else stop the timer, set back to whatever state you want it to be in afterward):

    Code:
    Public Class Form1
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            My.Settings.animation += 1
            Select Case My.Settings.animation
                Case 1
                    PictureBox1.Show()
                    PictureBox2.Hide()
                    PictureBox3.Hide()
                Case 2
                    PictureBox1.Hide()
                    PictureBox2.Show()
                    PictureBox3.Hide()
                Case 3
                    PictureBox1.Hide()
                    PictureBox2.Hide()
                    PictureBox3.Show()
                Case Else
                    Timer1.Stop()
                    // set state for future
            End Select
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Start()
            My.Settings.animation = 0
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Show()
            PictureBox2.Hide()
            PictureBox3.Hide()
        End Sub
    End Class
    
    This way, the logic to change the animation will only run when the timer ticks, rather than busy loop and cause nothing to happen.

    >.>
  15. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    I was on a phone when I sent that, I can hardly analyze code correctly on a 6" screen when I normally write code on a screen multiple times that size.
  16. RHY3756547

    RHY3756547 FreeSO Developer Staff Member Moderator

    I read it on my phone and saw the problem, that's not really an excuse.
    pisarz1958 likes this.
  17. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    A 6 inch phone, it's tiny
  18. RHY3756547

    RHY3756547 FreeSO Developer Staff Member Moderator

    The iPhone 5 has a 4 inch screen. I'm not sure why this is relevant.
    xXJDrocks450Xx and pisarz1958 like this.
  19. Driesje44

    Driesje44 Active Member

    It works! Thanks! I it we're like 17 pictures and some of them came multiple times, so in changed it to this:
    Code:
    Public Class Form1
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            My.Settings.animation += 1
            Select Case My.Settings.animation
                Case 1
                    PictureBox1.Image = My.Resources.BurbyKitchen1
    
                Case 2
                    PictureBox1.Image = My.Resources.BurbyKitchen2
                Case 3
                    PictureBox1.Image = My.Resources.BurbyKitchen3
                Case 4
                    PictureBox1.Image = My.Resources.BurbyKitchen4
                Case 5
                    PictureBox1.Image = My.Resources.BurbyKitchen5
                Case 6
                    PictureBox1.Image = My.Resources.BurbyKitchen6
                Case 7
                    PictureBox1.Image = My.Resources.BurbyKitchen7
                Case 8
                    PictureBox1.Image = My.Resources.BurbyKitchen8
                Case 9
                    PictureBox1.Image = My.Resources.BurbyKitchen9
                Case 10
                    PictureBox1.Image = My.Resources.BurbyKitchen10
                Case 11
                    PictureBox1.Image = My.Resources.BurbyKitchen11
                Case 12
                    PictureBox1.Image = My.Resources.BurbyKitchen12
                Case 13
                    PictureBox1.Image = My.Resources.BurbyKitchen13
                Case 14
                    PictureBox1.Image = My.Resources.BurbyKitchen14
                Case 15
                    PictureBox1.Image = My.Resources.BurbyKitchen15
                Case 16
                    PictureBox1.Image = My.Resources.BurbyKitchen16
                Case 17
                    PictureBox1.Image = My.Resources.BurbyKitchen17
                Case 18
                    PictureBox1.Image = My.Resources.BurbyKitchen15
                Case 19
                    PictureBox1.Image = My.Resources.BurbyKitchen16
              
                Case 20
                    PictureBox1.Image = My.Resources.BurbyKitchen17
                Case 21
                    PictureBox1.Image = My.Resources.BurbyKitchen15
                Case 22
                    PictureBox1.Image = My.Resources.BurbyKitchen16
                Case 23
                    PictureBox1.Image = My.Resources.BurbyKitchen17
                Case 24
                    PictureBox1.Image = My.Resources.BurbyKitchen15
                Case 25
                    PictureBox1.Image = My.Resources.BurbyKitchen16
                Case 26
                    PictureBox1.Image = My.Resources.BurbyKitchen17
               
    
                Case Else
                    Timer1.Stop()
    
            End Select
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Timer1.Start()
            My.Settings.animation = 0
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox1.Show()
         
        End Sub
    
        Private Sub PictureBox2_Click(sender As Object, e As EventArgs)
        End Sub
    End Class
    Thanks guyz! :)
    RHY3756547 likes this.
  20. LetsRaceBwoi

    LetsRaceBwoi Well-Known Member

    How do you live?
    On-topic, nice work! Why do you have an unused PictureBox2_Click handler? There's no use for it. You can freely delete it without worry (in most versions of VS).

Share This Page