3 Replies - 622 Views - Last Post: 29 January 2013 - 12:16 PM Rate Topic: -----

#1 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Value of increment variable jumps in vb.net

Posted 29 January 2013 - 11:48 AM

Hello Friends,
I got another problem.

I am trying to achieve an AutoHide functionality in my label
So I tried to create a Class Library Project named InformationBar

Just a look at my code :
Imports System.Windows.Forms
Imports System.Drawing

Public Class InformationBar
    Inherits DevComponents.DotNetBar.LabelX

    Dim TimeOut As Integer
    Dim TimerForTimeOut As New Timer
    Dim i As Integer

    Public Property Time_Out() As Integer
        Get
            Return TimeOut
        End Get
        Set(ByVal value As Integer)
            TimeOut = value
        End Set
    End Property


    Private Sub InformationBar_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged

        If Me.Visible = True Then
            AddHandler TimerForTimeOut.Tick, AddressOf TimerForTimeOut_Tick
            TimerForTimeOut.Interval = 1000
            TimerForTimeOut.Start()
        End If

    End Sub

    Private Sub TimerForTimeOut_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
        
        i = i + 1

        If i = TimeOut Then
            i = 0
            Me.Visible = False
            TimerForTimeOut.Stop()
        End If

        Form1.Label1.Text = i
        Form1.Label2.Text = TimeOut
    End Sub

End Class


Here Time_Out Property is Created. For the value of that Variable TimeOut is declared
i is another variable which increments by 1 everytime the timer is tick

This code should show a label for some time given by the user while a button is clicked and after that it should be vanished
While running this code :
If I give the value of timeOut = 10
then I click on the button the label is shown for 10 seconds after that it is hidden
Then I click the button Second time then the label should be shown again for 10 seconds but it is being shown for 7 to 8 seconds only and then again........the time of label shown decreases

I think this code should work but it didnt
It does not give me any error
So I decided that there should be some problem with variable i
I kept breakpoints but still I could not find any Problems
So I placed a label on the form and examined the values of i on each Tick of Timer

Here I got some strange results
the value of i when the button is clicked for the first time : 1,2,3,4,5,6,7,8,9,10.
the value of i when the button is clicked for second time : 1,3,5,7,9,1
the value of i when the button is clicked third time : 1,2,4,6,8,10
fourth time : 6, 10
and so on..........

thanks in advanced

Is This A Good Question/Topic? 0
  • +

Replies To: Value of increment variable jumps in vb.net

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: Value of increment variable jumps in vb.net

Posted 29 January 2013 - 12:04 PM

Hi,

One problem I see is with the code in the InformationBar_VisibleChanged() method. You are subscribing to the Tick event of the timer every time the label is made visible, but you aren't unsubscribing. Therefore, on the first click of the button, the code in TimerForTimeOut_Tick() will run once per tick, on the second click it will run two times per tick, and so on.

You need to unsubscribe from the Tick event when the time out is reached, (EDIT) using RemoveHandler.

This post has been edited by CodingSup3rnatur@l-360: 29 January 2013 - 12:05 PM

Was This Post Helpful? 2
  • +
  • -

#3 Vishal1419  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 141
  • Joined: 19-May 12

Re: Value of increment variable jumps in vb.net

Posted 29 January 2013 - 12:15 PM

Thank You CodingSup3rnatur@l-360
This is the point I never knew and now it works fine

Now one another problem (not with this code)
I want to make fade out effect for label but it does not have any opacity property, so how can I achieve the animation or how can I create a Property for opacity
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Value of increment variable jumps in vb.net

Posted 29 January 2013 - 12:16 PM

I'm guessing that the addition of a handler being added with each new occurrence is causing both to run and i being in the scope of the entire class is accumulating for each handler ran. Therefore you continue to double with each execution of the routine.

Maybe add the handler only once and see what you come up with.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1