13 Replies - 1317 Views - Last Post: 06 July 2010 - 03:53 PM Rate Topic: -----

#1 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

C# Help With Code

Posted 06 July 2010 - 02:42 PM

I made this code
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (pictureBox1.Width == 427)
            {
                if (pictureBox1.Width >= 121) timer2.Stop();
                else pictureBox1.Width += 95;
            }
            else
            {
                if (pictureBox1.Width <= 427) timer2.Stop();
                else pictureBox1.Width -= 95;
            }

        }


But I feel like I did something wrong, can anyone help me fix up this code, im trying to make this like a 2 button press to make it bigger and smaller.
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            timer2.Start(); 
        }



Is This A Good Question/Topic? 0
  • +

Replies To: C# Help With Code

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Help With Code

Posted 06 July 2010 - 02:53 PM

I think you're going to have to be much more specific about what you are trying to achieve...because I have no idea what you are trying to do.

For example, why is this done in a timer's tick event? And another thing...what do you mean to accomplish with this:
    if (pictureBox1.Width == 427)
    {
        if (pictureBox1.Width >= 121) timer2.Stop();
        else pictureBox1.Width += 95;
    }
    else
    {
        if (pictureBox1.Width <= 427) timer2.Stop();
        else pictureBox1.Width -= 95;
    }


Think about what that is going to do:
Is Width exactly 427?
--Yes: Ask if width is greater than or equal to 121. This will always be true if the code makes it to this point, because we just checked if it is equal to 427.
--No: Ask if it is less than 427. But what if it is greater than 427?

I think you need to tell us what your goal is so we can help you refactor a bit.
Was This Post Helpful? 0
  • +
  • -

#3 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: C# Help With Code

Posted 06 July 2010 - 02:55 PM

You'll have to explain more what it is that you are trying to do.

Now, your if statement isn't going to work. The first check requires that width == 427. The second check wants to know if it is > 121, which it is since it has to be 427. So the 'else' condition will never happen and your picturebox will not grow larger.

You also only shut off timer2, is this what you wanted?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# Help With Code

Posted 06 July 2010 - 02:57 PM

You think its wrong. Why? You haven't really described a problem or error or execption or unexpected behavior.

You want to make "it" smaller. A little vague there.

"Help with C# code" - a little vague for a title as all posts in the C# help area are for help with C# code. Just a mention for future questions.

I see no point to timer1 as you never use it in the code shown.

"Like a two button press to make it bigger or smaller"... Which 'it'? There is no button click (press) event used here. Just a timer event.


I think what you are trying to say is that when you click the button you want it to shrink then expand back to its normal size. Does that sound right?

So let's walk through this and what will happen.
private void timer2_Tick(object sender, EventArgs e)
{
    if (pictureBox1.Width == 427)
    {
        if (pictureBox1.Width >= 121) timer2.Stop();
        else pictureBox1.Width += 95;
    }
    else
    {
        if (pictureBox1.Width <= 427) timer2.Stop();
        else pictureBox1.Width -= 95;
    }

}




User clicks the picture box. That starts timer2
Timer2 then goes _Tick after whatever interval you set it for.
If the picturebox is exactly equal to 427 then the timer will stop. The if width >=121 is pointless since it is only read if the width is 427 so it will always be >= 121

else the picturebox is then less than or equal to 427 (kinda silly since you already handled the equal to 427) then the timer2 stops.

So basically, no matter what, the only action that will happen is that timer2 stops.

If you put a breakpoint on the first line of your if, and walk through with the F10 key you can see this happening.
Was This Post Helpful? 0
  • +
  • -

#5 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

Re: C# Help With Code

Posted 06 July 2010 - 03:02 PM

Well Im bacicly trying to make it bigger when I click it the first time and then the 2nd click it will get smaller,

View PosttlhIn, on 06 July 2010 - 01:57 PM, said:

You think its wrong. Why? You haven't really described a problem or error or execption or unexpected behavior.

You want to make "it" smaller. A little vague there.

"Help with C# code" - a little vague for a title as all posts in the C# help area are for help with C# code. Just a mention for future questions.

I see no point to timer1 as you never use it in the code shown.

"Like a two button press to make it bigger or smaller"... Which 'it'? There is no button click (press) event used here. Just a timer event.


I think what you are trying to say is that when you click the button you want it to shrink then expand back to its normal size. Does that sound right?

So let's walk through this and what will happen.
private void timer2_Tick(object sender, EventArgs e)
{
    if (pictureBox1.Width == 427)
    {
        if (pictureBox1.Width >= 121) timer2.Stop();
        else pictureBox1.Width += 95;
    }
    else
    {
        if (pictureBox1.Width <= 427) timer2.Stop();
        else pictureBox1.Width -= 95;
    }

}




User clicks the picture box. That starts timer2
Timer2 then goes _Tick after whatever interval you set it for.
If the picturebox is exactly equal to 427 then the timer will stop. The if width >=121 is pointless since it is only read if the width is 427 so it will always be >= 121

else the picturebox is then less than or equal to 427 (kinda silly since you already handled the equal to 427) then the timer2 stops.

So basically, no matter what, the only action that will happen is that timer2 stops.

If you put a breakpoint on the first line of your if, and walk through with the F10 key you can see this happening.

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# Help With Code

Posted 06 July 2010 - 03:06 PM

If you are trying to get the button to shrink then return try something like this...

private void pictureBox1_Click(object sender, EventArgs e)
        {
            BlinkButton((PictureBox)sender); 
        }


void BlinkButton(PictureBox theButton)
{
    int OriginalWidth = theButton.Width;
    theButton.Width -= 50;
    Thread.Sleep(200);
    theButton.Width = OriginalWidth;
}



The BlinkButton method can now be used for all of your pictureboxes as it is not hard-coded to any one picturebox. You could direct several boxes to call this same method. Though if you were going to do that then you should make a new custom button that inherits from Button. Then all the blink code is inside the custom button. There is a good example of such custom control creation here:
Bulding an application - Part 1




It's far from ideal, and I wrote it here on the forum without testing in Visual Studio, but it is simple enough that it should be fine.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# Help With Code

Posted 06 July 2010 - 03:12 PM

Quote

Well Im bacicly trying to make it bigger when I click it the first time and then the 2nd click it will get smaller

Then you are way off base. What does any of that have to do with timers?

bool make1bigger = true;
button1_click(object sender, eventargs e)
{
   if (make1bigger) 
   {
      picturebox1.width += 50;
      make1bigger = false;
   }
   else
   {
      picturebox1.width -= 50;
      make1bigger = true;
   }
   
}

Was This Post Helpful? 0
  • +
  • -

#8 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

Re: C# Help With Code

Posted 06 July 2010 - 03:23 PM

I want to make it slide


View PosttlhIn, on 06 July 2010 - 02:12 PM, said:

Quote

Well Im bacicly trying to make it bigger when I click it the first time and then the 2nd click it will get smaller

Then you are way off base. What does any of that have to do with timers?

bool make1bigger = true;
button1_click(object sender, eventargs e)
{
   if (make1bigger) 
   {
      picturebox1.width += 50;
      make1bigger = false;
   }
   else
   {
      picturebox1.width -= 50;
      make1bigger = true;
   }
   
}

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# Help With Code

Posted 06 July 2010 - 03:25 PM

Quote

I want to make it slide


You really need to work on the explanation skills.

Are you saying that you want to make the picturebox grow and shrink? That on the first click it should grow from 200-400 in steps. On the second click it should shrink from 400-200 in steps. Giving it an animated effect rather than suddenly change from 200 to 400, then from 400 to 200. Does that sound about right?
Was This Post Helpful? 0
  • +
  • -

#10 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

Re: C# Help With Code

Posted 06 July 2010 - 03:29 PM

Yes


View PosttlhIn, on 06 July 2010 - 02:25 PM, said:

Quote

I want to make it slide


You really need to work on the explanation skills.

Are you saying that you want to make the picturebox grow and shrink? That on the first click it should grow from 200-400 in steps. On the second click it should shrink from 400-200 in steps. Giving it an animated effect rather than suddenly change from 200 to 400, then from 400 to 200. Does that sound about right?

Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Help With Code

Posted 06 July 2010 - 03:38 PM

Well, growing/shrinking by 95 pixels every tick won't give you a smooth animation, that's for sure.

Have you considered using WPF instead? They have a Storyboard construct that can easily animate objects by applying a change to a property from value X to value Y over time Z, either linearly or by spline curve.

This post has been edited by insertAlias: 06 July 2010 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: C# Help With Code

Posted 06 July 2010 - 03:39 PM

Then you are really way off.

Picture this logic... Make one method called "Grow" and one called "Shrink". Use my earlier example to send any of the pictureboxes you want to those methods.


void Grow(PictureBox theBox)
{
   while (theBox.Width < 600)
   {
     theBox.Width += 10;
     theBox.Refresh();//Make it redraw before sleeping the thread
     Thread.Sleep(20);
   }
}

Was This Post Helpful? 0
  • +
  • -

#13 systemerror   User is offline

  • D.I.C Head

Reputation: -19
  • View blog
  • Posts: 205
  • Joined: 15-August 09

Re: C# Help With Code

Posted 06 July 2010 - 03:43 PM

I have been thinking of using WPF's But I have no idea on how to use them. I must finish this project 1st anyway

View PostinsertAlias, on 06 July 2010 - 02:38 PM, said:

Well, growing/shrinking by 95 pixels every tick won't give you a smooth animation, that's for sure.

Have you considered using WPF instead? They have a Storyboard construct that can easily animate objects by applying a change to a property from value X to value Y over time Z, either linearly or by spline curve.

Was This Post Helpful? 0
  • +
  • -

#14 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Help With Code

Posted 06 July 2010 - 03:53 PM

Ok, I decided to give you a break because you made an effort.

Look at this code, and modify it for your own purposes.
private bool grow = false;
Timer t;
int maxwidth = 400;
int minwidth = 300;
int step = 3;
int interval = 20;
public Form1()
{
    InitializeComponent();
    t = new Timer();
    t.Interval = interval;
    t.Tick += new EventHandler(t_Tick);
}

void t_Tick(object sender, EventArgs e)
{
    if (grow)
    {
        if (pictureBox1.Width >= maxwidth)
            t.Stop();
        else
            pictureBox1.Width += step;
    }
    else
    {
        if (pictureBox1.Width <= minwidth)
            t.Stop();
        else
            pictureBox1.Width -= step;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    t.Stop();
    grow = !grow;
    t.Start();
}

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1