0

This code is just changing location, not sliding.

I am using this at the moment:

for (int i = 740; i == 740; i++)
{
   panel2.Location = new Point(panel2.Location.X - i, panel2.Location.Y);      
}

How can I slide the panel slowly?

Hacking Games
  • 63
  • 1
  • 5
  • 14
  • Is this WinForms or WPF? – meilke Sep 27 '13 at 12:47
  • 1
    This is probably going to be more complex than you'd like. See, like a movie, you need more frames. However, both the `Location` and `TRBL` properties are `int` values. You need `float` values so you can build that effect. This means you're actually going to have to **draw** it. – Mike Perrenoud Sep 27 '13 at 12:50
  • 1
    Two things to consider: (1) If you are doing this on the main GUI-thread, then it will block until your loop is finished, which is why you don't see it slide. You need to do it in a separate thread, and tell the GUI thread when to update. (2) Even if you would use another thread, it is happening very fast, so you wouldn't notice it. Slow it down a bit, eg. by waiting 1 ms every time you set a new value. – Lars Kristensen Sep 27 '13 at 12:52
  • @neoistheone, float values for pixel-positions? That doesn't sound like a good idea :) But a custom draw-method might do the trick. Haven't tried it for this kind of "animation" though. – Lars Kristensen Sep 27 '13 at 12:55
  • @LarsKristensen, you know I'd say that, except that the `PointF` class exists for that very reason. :D - There is a big difference between a `Pixel` and a `Point`. – Mike Perrenoud Sep 27 '13 at 12:56
  • @neoistheone, True, it exists for drawing custom shapes. But without knowing more context than what the OP posted, it might be a little overkill to make a custom draw method for a panel (and its contents), just to animate the sliding. Also, why don't you use PointF in your answer then? :) – Lars Kristensen Sep 27 '13 at 13:07

2 Answers2

0

Now, as I stated in my comment you really need to use float values, so you really need to draw it. However, the current implementation only makes a single iteration. The current loop could have been translated into this:

panel2.Location = new Point(panel2.Location.X - 740, panel2.Location.Y);

Consider a loop like this to slide it out:

for (int i = -(panel2.Width); i < 0; i++)
{
   panel2.Location = new Point(i, panel2.Location.Y);
}

That algorithm is assuming that you've set the Location to the - of it's width (e.g. -740x) so that it's simply not visible on the screen. The reverse would hide it.

This will still be a little choppy, but it won't just hide it like your current code.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Yea not bad . Your thing works like I wanted it to . thanks mate – Hacking Games Sep 27 '13 at 13:00
  • Where does the float-values play any part in this? As far as I can see, you are still using Integers and Point-objects :) – Lars Kristensen Sep 27 '13 at 13:29
  • 1
    @LarsKristensen, I think I pretty much made that clear when I prefaced the answer. I'm not going to build out a GDI+ example on SO. I offered the OP a possible intermediate solution. Based on the OP's comment, I accomplished that. – Mike Perrenoud Sep 27 '13 at 13:49
0

If you are just going to slide the panel try to do this

Try this code:

Panel panelArray = new Panel[];
Panel panel2 = panelArray[0];



for (int i = 0; i <= 100; i++)
{
    panel2.Location = new Point(panel2.Location.X - i, panel2.Location.Y);
    System.Threading.Thread.Sleep(10);
}
CMinor
  • 397
  • 1
  • 5
  • 14