CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2011
    Posts
    2

    Double Var keeps adding to increment

    -Synopsis-
    I've recently developed a simple physics program, which presents a modifiable velocity setting.

    It updates the current time(in secs) as well as the current position which happens to add to the

    current position the missile image is set to.

    -Problem-
    Overall the program works just fine, there just happens to be one problem , in order to update the

    newtime I've created a double variable called "Currentime" which is set equal to the current text in

    the textbox plus the time increment(0.5). This is called by a timer object which is created for the

    form and calls it every 0.05 seconds. The thing is though that everytime i reset the time fields to

    zero it for some reason adds like this 0.5 ,1.0, 1.5, 2.0 etc (1st round) then 1.0 ,2.0, 3.0., 4.0,etc(2nd round) and keeps increasing

    if you need more code or want a copy i can provide it i really need help i don't know how to solve it
    to see exactly what i mean here is segment of the code
    private void button1_Click(object sender, EventArgs e)// If this button is clicked start the timer
    {
    timestarted = true;
    this.timer1.Enabled = true;
    this.timer1.Interval = 100;
    this.timer1.Start();
    this.timer1.Tick += new EventHandler(timer1_Tick);
    }

    void timer1_Tick(object sender, EventArgs e)
    {
    this.textBox1.ResumeLayout();
    Update();
    }
    public void Update()
    {

    double newtime = double.Parse(this.textBox1.Text) + double.Parse(this.textBox4.Text);
    this.pictureBox1.Refresh();
    //textbox 1 = time text textbox 4 = time increment( 0.5)

    System.Drawing.Image Missile = Image.FromFile(@"C:\Users\Brandon Fleury\Documents\Visual Studio 2008\2dProjectileTest\2dProjectileTest\Resources\Missile.jpg");
    InitialMissileHeight = 332.0;
    InitialTime = 0.0;
    InitialVelocity = 20.0;
    InitialAcceleration = 0.0;
    // Y-Position -- Position(Final) = Posiiton(Initial) + Velocity(Initial)* Current Time + 1/2*Acceleration* Current Time*Current Time

    double Yposition = InitialMissileHeight - double.Parse(this.textBox1.Text) * Double.Parse(this.textBox1.Text); ;
    double newheight = InitialMissileHeight - Yposition;
    if (newheight <500.0)
    {
    this.textBox1.Text = newtime.ToString();
    this.textBox3.Text = newheight.ToString();
    this.textBox3.Update();
    this.pictureBox1.CreateGraphics().DrawImage(Missile, 225, Convert.ToInt32(Yposition), 10, 10);
    this.pictureBox1.Update();

    }

    }

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Double Var keeps adding to increment

    Just for sure, what is expected behavior?
    1st: 0.0 0.5 1.0 1.5 2.0 ...
    2nd: 0.0 0.5 1.0 1.5 2.0 ...
    3rd: 0.0 0.5 1.0 1.5 2.0 ...
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Double Var keeps adding to increment

    Why do you add another event handler with every button click?

  4. #4
    Join Date
    Jun 2010
    Posts
    56

    Re: Double Var keeps adding to increment

    Quote Originally Posted by zips View Post
    Why do you add another event handler with every button click?
    Code:
    this.timer1.Tick += new EventHandler(timer1_Tick);
    I would check this first. Either remove the event handler later in the code like so...

    Code:
    this.timer1.Tick -= new EventHandler(timer1_Tick);
    Or add the event handler outside of the button click but before the button click takes place, like a constructor or another method thats called on the constructor. This way its only added once.

  5. #5
    Join Date
    May 2011
    Posts
    2

    Re: Double Var keeps adding to increment

    ah presto that was it thanks guys i appreciate it it works just fine now, i have to take note of that for future projects thanks tons and it wasn't intentional btw just a small thing i overlooked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured