Any Idea why this keeps freezing?

TALSOFT

Active member
Joined
Feb 23, 2012
Messages
34
Programming Experience
5-10
VB.NET:
Public Class Colors    Dim cs As Integer = 1
    Dim LastPick As Integer = 0
    Public Function ScreenX() As Integer
        Dim X As Integer = Screen.PrimaryScreen.Bounds.Width
        Return X
    End Function
    Public Function ScreenY() As Integer
        Dim Y As Integer = Screen.PrimaryScreen.Bounds.Height
        Return Y
    End Function
    Public Function Grow_cS()


        If cs = 1 Then
            If cS1.Visible = False Then cS1.Show()
            If cS1.Height < Me.Height Or cS1.Width < Me.Width Then
                cS1.Height += 8
                cS1.Width += 16
                GoTo Skip
            End If
            cs = 2
            cS1.BackColor = ChangeColor()
            cS1.Height = 1
            cS1.Width = 1
            cS1.Hide()
Skip:


        End If
        If cs = 2 Then
            If cS2.Visible = False Then cS2.Show()
            If cS2.Height < Me.Height Or cS1.Width < Me.Width Then
                cS2.Height += 8
                cS2.Width += 16
                GoTo Skip
            End If


            cs = 1
            cS2.BackColor = ChangeColor()
            cS2.Height = 1
            cS2.Width = 1
            cS2.Hide()
        End If
        Return 0
    End Function
    Private Sub InitialTest_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        'Close the form when escape is press


        If e.KeyValue = Keys.Escape Then
            Me.Close()
        End If
    End Sub
    Private Sub Colors_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '############
        'Center Form in screen
        '############
        Me.Size = New System.Drawing.Size(ScreenX, ScreenY)
        '####END#####
        '############


        'Center Word Label
        '############
        Label1.SetBounds((Me.ClientSize.Width - Label1.Width) / 2, (Me.ClientSize.Height - Label1.Height) / 2, 0, 0, BoundsSpecified.Location)
        '####END#####
        TimerCS.Start()


    End Sub
    Public Function ChangeColor() As Color
Top:


        Dim Pick As New System.Random
        Dim Number As Integer = 0
        Number = Pick.Next(0, 5)
        If Number = LastPick Then GoTo Top
        If Number = 0 Then Return Color.Black
        If Number = 1 Then Return Color.Red
        If Number = 2 Then Return Color.Blue
        If Number = 3 Then Return Color.Yellow
        If Number = 4 Then Return Color.White
        If Number = 5 Then Return Color.Green
        LastPick = Number
        Number = 0
    End Function
    Private Sub TimerCS_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerCS.Tick
        Grow_cS()
    End Sub
End Class
I've limited the issue to Function CS_Grow where it resets the panels cs1.height & width (and I am sure the event exists for cs2 also)

Basically, I have a forum that is fullscreen, when a timer ticks at a 1/100th interval (timer tick is set to 1) a panel grows until it has reached the screen dimensions, then it is suppose to switch to the other panel, change its color and hide itself. The second panel is suppose to complete that same task and reset its size to default.

PS I know its been ages since I have been on here, my wife and I just got out of the homeless shelter :( thanks to anyone who can help
 
Last edited:
Ok so I stopped the freezing, now the colors wont change the panels just disappear after cs1 fills the screen
VB.NET:
  Public Function Grow_cS()

        If cs = 1 Then
            cS1.Show()
            If cS1.Height < Me.Height Or cS1.Width < Me.Width Then
                cS1.Height += 8
                cS1.Width += 16
                GoTo Skip
            End If
            TimerCS.Stop()
            cS1.BackColor = ChangeColor()
            cS1.Height = 1
            cS1.Width = 1
            cS1.Hide()
            cs = 2
            TimerCS.Start()
Skip:


        End If
        If cs = 2 Then


            cS2.Show()
            If cS2.Height < Me.Height Or cS1.Width < Me.Width Then
                cS2.Height += 8
                cS2.Width += 16
                GoTo ENDCS
            End If
            TimerCS.Stop()
            cS2.BackColor = ChangeColor()
            cS2.Height = 1
            cS2.Width = 1
            cS2.Hide()
            cs = 1
            TimerCS.Start()
        End If
EndCS:


        Return 0
    End Function
 
Hi,

Whilst GoTo will work, it's a bit dark-ages, isn't it? an If...Then...Else block would do the same job

As to the problem, should the cS1 in
VB.NET:
If cS2.Height < Me.Height Or cS1.Width < Me.Width Then
not be cS2?
 
Hey Menthos thanks for your willingness to help. And thank you for point out that santax error, it would have been at least a day before I myself noticed! I am quite sorry, I do not fully understand the scope of what you are talking about regarding Whilst GoTo statements (example please)... I am all self taught here and that doesnt sound like anything I've done before.

Needless to say, I've resolved that issue but came across a different one.

Now the Panels grow, but not on all four corners, instead they grow from the bottom and the right.

Here is the working code for that sub
VB.NET:
 Public Function Grow_cS()

        If cs = 1 Then


            If cS1.Height < Me.Height Or cS1.Width < Me.Width Then
                TimerCS.Stop()
                Do Until cS1.Height = Me.Height
                    cS1.Height += 1
                    cS1.Width += 2
                Loop
                cS1.BackColor = ChangeColor()
                cS1.Height = 0
                cS1.Width = 0
                cS1.Hide()
                cS2.Show()


                cs = 2
                TimerCS.Start()


            End If


Skip:


        End If
        If cs = 2 Then




            'If cS2.Height < Me.Height Or cS2.Width < Me.Width Then
            'MsgBox("Growing 2")
            'cS2.Height += 8
            'cS2.Width += 16
            'GoTo ENDCS
            'End If
            'TimerCS.Stop()
            'cS2.BackColor = ChangeColor()
            'cS2.Height = 20
            'cS2.Width = 40




            'TimerCS.Start()
            'cs = 1


            If cS2.Height < Me.Height Or cS2.Width < Me.Width Then
                TimerCS.Stop()
                Do Until cS2.Height = Me.Height
                    cS2.Height += 1
                    cS2.Width += 2
                Loop
                cS2.BackColor = ChangeColor()
                cS2.Height = 0
                cS2.Width = 0
                cS2.Hide()
                cS1.Show()
                cs = 1
                TimerCS.Start()
            End If
        End If
EndCS:


        Return 0
    End Function

Please note that when the forum starts I have the panels labled cs1 and cs2 immidiatley centered
VB.NET:
 Private Sub Colors_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New System.Drawing.Size(ScreenX, ScreenY)
 cS1.SetBounds((Me.ClientSize.Width - Label1.Width) / 2, (Me.ClientSize.Height - Label1.Height) / 2, 0, 0, BoundsSpecified.Location)
 cS2.SetBounds((Me.ClientSize.Width - Label1.Width) / 2, (Me.ClientSize.Height - Label1.Height) / 2, 0, 0, BoundsSpecified.Location)

The "Growing" is working, I just want it to fill the screen and not just the bottom 1/4th of the screen.

If I remove the "centering" and programmicaly place the panels in the top left, all is fine... but I want them centered and to grow in all four directions. Any suggestions?
 
No problem - small errors like that happen to us all.

I wasn't referring to a 'Whilst GoTo' statement, I was referring to your use of 'GoTo' and that you could (should) get rid of them. 'GoTo' is a throwback to coding from a long time ago and there are better, more readable ways to code that functionality available to you.

Forms are positioned by giving the top/left corner and then a width/height (as you know), so you're going to have to create a bit of extra code to move the top / left corner proportionally when you grow your forms.
 
Dang, I was really hoping there was a simple suffix I could add to the grow lines. Well moving the panels shouldn't be too complicated I can figure that out, through some net research (Thanks once again). Let me pick your brain for one more thing, have you any idea what is wrong with this santax? It does not Pick a different color if its the same as the last (when application is running, cs1 will be red (as an example) and then so will cs2....

VB.NET:
Public Function ChangeColor() As Color
Repeat:
        Dim Pick As New System.Random
        Dim Number As Integer = 1




        Number = Pick.Next(-1, 6)
        If Number = LastPick Then GoTo Repeat
        If Number = 0 Then Return Color.Black
        If Number = 1 Then Return Color.Red
        If Number = 2 Then Return Color.Blue
        If Number = 3 Then Return Color.Yellow
        If Number = 4 Then Return Color.White
        If Number = 5 Then Return Color.Green
        LastPick = Number


    End Function

I have tried this as opposed to a GoTO statement:
VB.NET:
Do Until Number <> LastPick
            Dim Pick As New System.Random
            Dim Number As Integer = 1
            Number = Pick.Next(-1, 6)
        Loop
Which has the effect of confinually repeating the same color.... frustrating
 
Two things on your loop... declare your Random object outside of the loop - you shouldn't be creating a new object each time - and, you don't appear to be setting LastPick in that code (of course you may be setting that outside this code)


... oh and the third problem with that loop - you're explicitly setting Number to 1 every time ;)
 
Hey Menthos, I'm sorry I tried to real quickly re-create the original code. This is what I have that doesnt work
VB.NET:
 Public Function ChangeColor() As Color
        Dim Pick As New System.Random
        Dim Number As Integer = 1
        Do Until Number <> LastPick
            Number = Pick.Next(-1, 6)
        Loop
        If Number = 0 Then Return Color.Black
        If Number = 1 Then Return Color.Red
        If Number = 2 Then Return Color.Blue
        If Number = 3 Then Return Color.Yellow
        If Number = 4 Then Return Color.White
        If Number = 5 Then Return Color.Green
        LastPick = Number
    End Function

I thought that I was setting LastPick by this statement "Lastpick = Number"
But all it does is continually make both panels the same color.

Also with the moving of my panelbox I have been trying some odd things such as:
VB.NET:
Do Until cS1.Height = Me.Height
                    cS1.Height += 1
                    cS1.Top += 1
                    cS1.Width += 2
                    cS1.Left += 2
                Loop
And:
VB.NET:
   Do Until cS1.Height = Me.Height
                    cS1.Height += 1
                    cS1.Location = New System.Drawing.Point(cS1.Location.X + 2, cS1.Location.Y + 1)
                    cS1.Width += 2


                Loop
With no luck what so ever, actually what happens is a blank form in which my panels are instantly gone, and cs2 ceases to exists (even though I have not applied either trial santax to it)

Any ideas where I am going wrong there and what I should be looking into for an option? I Just would hate to have to do like 1,000 lines of redundant if than statements "If Users Screen Res = XXXXX then 102 102, If Location = 102, 102 then Location = 104 104..... and so on and so fourth..."
 
Sorry, thought you'd replaced that whole block with the loop. Not sure where you are defining your 'lastpick' var, but one thing you may want to add is passing your lastpick value to the function, rather than relying on it being global. It's good practice to keep the scope local to your function/method and keeps things a whole lot tidier too.

0,0 on a winform is the top left of the form, therefore if you want to expand the top left of your panel towards the top left of the form, you're going to need to subtract the value, rather than add as you appear to be doing.
 
Excellent to know regarding location, as well as the Local Tip, I was assuming that by making it global it would not reset its value to its declaired value when the function is called Say for example I declair it as follows:
VB.NET:
Public Function Int_Test()
Dim IntTest as Integer = 1
end function
Wouldn't that immediately set IntTest = 1 each time the function is called?
 
Setting an object at the class level will give it 'global' scope and will persist it's value across that scope - my point was that doing that can (and often is) confusing.

That's correct on your IntTest question, you're declaring the object as an Integer and assigning it 1.
 
Setting an object at the class level will give it 'global' scope and will persist it's value across that scope - my point was that doing that can (and often is) confusing.

That's correct on your IntTest question, you're declaring the object as an Integer and assigning it 1.

Well Menthos I appreciate all your help, but I am stuck in a bind.... gonna take my mind off this project for a couple days and try again.

When I utilize moving the panels, they dissapear no matter the methood.

Also I cannot for the life of me figure out why no matter how I have my santax lined out, colors repeat.
 
Back
Top