By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,676 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,676 IT Pros & Developers. It's quick & easy.

Moving Panel

P: n/a
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?
Aug 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"Diogo Alves - Software Developer"
<Di*************************@discussions.microsoft .comwrote in message
news:42**********************************@microsof t.com...
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?

Aug 29 '06 #2

P: n/a
hum... that produced a less fluid animation... it's jumping..

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

"Kevin Spencer" wrote:
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.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

It takes a tough man to make a tender chicken salad.
"Diogo Alves - Software Developer"
<Di*************************@discussions.microsoft .comwrote in message
news:42**********************************@microsof t.com...
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?


Aug 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.