Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Moving Panel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Diogo Alves - Software Developer - 29 Aug 2006 16:34 GMT
Greetings

I would like to knowhow can I put a sliding panel...
I've done this:

if (panel1.Width < 300)
{
    while (panel1.Width < 300)
       {
        panel1.Width = panel1.Width + 40;
        ButtonPosition();
        this.Invalidate();
    }
}
else
{
    while (panel1.Width > 0)
    {
        panel1.Width = panel1.Width - 40;
        ButtonPosition();
        this.Invalidate();
    }
}

The problem is that the content of the panel flickers a lot...

I've already tried the function Invalidate() but that doesn't give me a
fluid moviment... how can I do it?
Kevin Spencer - 29 Aug 2006 17:39 GMT
Hi Diogo,

I created a sample project to test with, with a single Form and a Panel on
it colored blue (for visibility). I put an image box in the Panel, to give
it something extra to draw. I created a button to open and close the Panel,
and the code is as follows:

public partial class Form1 : Form
{
private bool close = true;
private bool moving = false;

private void btnSize_Click(object sender, EventArgs e)
{
 if (moving)
 {
  btnSize.Text = (close ? "Open" : "Close");
  moving = false;
  Thread.Sleep(50);
  setClose(!close);
 }
 if (close)
 {
  moving = true;
  while (panel1.Width > panel1.MinimumSize.Width && moving)
  {
   panel1.Width -= 10;
   Thread.Sleep(5);
   Application.DoEvents();
  }
  setClose(false);
  moving = false;
  btnSize.Text = "Open";
 }
 else
 {
  moving = true;
  while (panel1.Width < panel1.MaximumSize.Width && moving)
  {
   panel1.Width += 10;
   Thread.Sleep(5);
   Application.DoEvents();
  }
  setClose(true);
  moving = false;
  moving = false;
  btnSize.Text = "Close";
 }
}

private void setClose(bool value)
{
 close = value;
 status1.Text = (close ? "Close" : "Open");
}

protected override void OnClosing(CancelEventArgs e)
{
 moving = false;
 base.OnClosing(e);
}

public Form1()
{
 InitializeComponent();
}
}

Basically, this does what your code does, with a couple of minor exceptions:

   a. Invalidate is not called.
   b. The width changed per iteration is smaller.

I experienced no flickering issues, even when I did call Invalidate on the
Form, but that is not necessary. I suspect that the call to Invalidate for
the Form may be causing your problems. When you call Invalidate on the Form,
it forces the entire Form to be redrawn. If you app has many more Controls
than mine (highly likely), this could cause the flickering.

Generally speaking, flickering is caused by too much of the Form needing to
be redrawn in a given painting interval. Invalidate should only be called
when necessary. That is, don't use it (by default) unless you see some
behavior that requires it. In addition, when you call Invalidate on a
Control, do so as sparingly as possible. There is an override of the
Invalidate method that takes a rectangle as an argument. If possible, only
Invalidate the smallest rectangle that needs to be redrawn. And remember
that every Control has an Invalidate method. You should almost never have to
call Invalidate on the entire Form.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.

> Greetings
>
[quoted text clipped - 24 lines]
> I've already tried the function Invalidate() but that doesn't give me a
> fluid moviment... how can I do it?
Diogo Alves - Software Developer - 30 Aug 2006 14:26 GMT
hum... that produced a less fluid animation... it's jumping..

:( pretty bad... do you have any other sugestions?

> Hi Diogo,
>
[quoted text clipped - 113 lines]
> > I've already tried the function Invalidate() but that doesn't give me a
> > fluid moviment... how can I do it?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.