Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Flickering effect with panel scrollbars
 

Flickering effect with panel scrollbars

Hello all.

I have developed one user control "Grid" in C# and I am using it in another user control inside one panel.

The AutoScroll property for the panel is enabled.

When the height or width of "Grid" increases and becomes more than panel size, scrollbars of panel are enabled.

When I scroll using any scrollbar and click on the "Grid" which is inside panel, the position of the scrolled scrollbar changes automatically and scrollbars get reset to their original position.

To solve this problem, I saved the position of the scrollbars in the Panel_Scroll() event using

private void panel_Scroll(object sender, ScrollEventArgs e)
{
m_scrollPosition = panel.AutoScrollPosition; // m_scrollPosition is a Point
}

Now, I am setting this position again in the MouseDown event for the "Grid" like

private void Grid_MouseDown(object sender, MouseEventArgs e)
{
panel.AutoScrollPosition = new Point(Math.Abs(m_scrollPosition.X), Math.Abs(m_scrollPosition.Y));
}

this works fine, but I am getting the flickering effect.

I think this is because, when i am setting the "panel.AutoScrollPosition" in MouseDown event, the value of the "panel.AutoScrollPosition" is x = -3 & y = -3.

So scrollbars are getting reset and set again in flash and I am getting the flickering effect.

Is there any solution for this to avoid flickering effect?
Kulabhishek  Monday, January 12, 2009 9:03 AM

Hi Abhishek,


Thank you for your sample project. I run it on my machine and did see the problem on my side.

When a child control inside a container, e.g. a Panel receives the focus, it will ask its container to scroll (if possible) so as to display the child control completely. This behavior is by design.

If you don’t want the container to scroll when the child control gets focused, a workaround is to catch the WM_LBUTTONDOWN Windows message sent to the child control and discard it. To do this, override the WndProc method in the child control and set the container focused when the WM_LBUTTONDOWN message is caught.

public partial class Grid : UserControl

{

int WM_LBUTTONDOWN = 0x201;

protected override void WndProc(ref Message m)

{

if (m.Msg == WM_LBUTTONDOWN)

{

if (this.Parent != null)

{

this.Parent.Focus();

}

}

else

{

base.WndProc(ref m);

}

}

}

It works well on my side. Please try it on your machine to see if it solves your problem.

Sincerely,
Linda Liu

Linda Liu  Thursday, January 15, 2009 4:44 AM

Hi Kulabhishek,

> When I scroll using any scrollbar and click on the "Grid" which is inside panel, the position of the scrolled scrollbar changes automatically and scrollbars get reset to their original position.

I perform a test based on your description. I put someTextBox on the UserControl and then add the UserControl on a form. Run the application and scroll thepanel.Then I clickon oneTextBoxinthe user control inside the panel. The scrollbars of the panel change a bit justto show the clickedTextBox completely. I don't see the scrollbarsget reset to their original position.

If possible, could you please send me a simple application that could just reprodue the problem? My email address is v-lliu@microsoft.com.

I look forward to your reply!

Sincerely,
Linda Liu

Linda Liu  Tuesday, January 13, 2009 9:28 AM
Hi Linda,

I have mailed you simple demo application for Grid along with exe files.

To see the flickering effect, scroll using both of the ScrollBars and click on the TextBox and then click on Grid. if you do not get flickering please try two three times with clicking on TextBox in between, as this effect is seen with first click on Grid after focus lost.

Regards
Abhishek
Kulabhishek  Wednesday, January 14, 2009 8:38 AM

Hi Abhishek,


Thank you for your sample project. I run it on my machine and did see the problem on my side.

When a child control inside a container, e.g. a Panel receives the focus, it will ask its container to scroll (if possible) so as to display the child control completely. This behavior is by design.

If you don’t want the container to scroll when the child control gets focused, a workaround is to catch the WM_LBUTTONDOWN Windows message sent to the child control and discard it. To do this, override the WndProc method in the child control and set the container focused when the WM_LBUTTONDOWN message is caught.

public partial class Grid : UserControl

{

int WM_LBUTTONDOWN = 0x201;

protected override void WndProc(ref Message m)

{

if (m.Msg == WM_LBUTTONDOWN)

{

if (this.Parent != null)

{

this.Parent.Focus();

}

}

else

{

base.WndProc(ref m);

}

}

}

It works well on my side. Please try it on your machine to see if it solves your problem.

Sincerely,
Linda Liu

Linda Liu  Thursday, January 15, 2009 4:44 AM

You can use google to search for other answers

Custom Search

More Threads

• Merging Cells in Listview/Datagridview etc...?
• Thumbnail Generation
• Source Code
• How to Create Simple MDI Forms
• Need expert to review application
• Has anyone changed IssuVision?
• User control problem
• Ver 1.1/Terrarium Server is not working 'n What about the contest rules?
• Introducing Creatures - Naming problems
• SQL-Server setup