Can anyone please advise:
I have a Windows form user control of my own creation which I've placed on a standard WinForm. The code of the user control is trivial:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
and
private void MyUserControl_Paint(object sender, PaintEventArgs e)
{
Graphics clientDC = this.CreateGraphics();
Bitmap bitmap = new Bitmap(Bounds.Width, Bounds.Height);
Graphics graphicsSurface = Graphics.FromImage(bitmap);
graphicsSurface.Clear(Color.Black);
clientDC.DrawImage(bitmap, 0, 0);
GC.Collect();
}
On the resize event of the main WinForm I resize the user control to fill the entire form as so:
MyUserControl.Bounds = new Rectangle(0, 0, ClientRectangle.Width, ClientRectangle.Height);
Good. It all works fine. As I drag a corner of the main form around the user control fills the form with it's black rectangle. No Problems.
Now, I place a scroll bar in my control and on resize I keep it "docked" to the right hand side. Relevant code as follows:
** So the folowing code is in the user control **
VScrollBar m_verticalScrollBar = new VScrollBar();
Controls.Add(m_verticalScrollBar);
And on the resize event of my control I have
m_verticalScrollBar.Bounds = new Rectangle(Width - SystemInformation.VerticalScrollBarWidth, 0, SystemInformation.VerticalScrollBarWidth, Height);
Ok, it works. The scroll bar is "pinned" to the right hand side BUT as I resize the form I can see the scroll bar being positioned well after the rest of the control has redrawn - it's position is "laggy". If I reduce the width of the form the scroll bar can be seen to be "overtaken" by the right hand edge of the form briefly until it redraws in the correct position. The delay is small but annoying.
Can anyone make any suggestions as to how to get the scroll bar to be "in-sync" with the forms\controls size? (I don't want to have to be drawing a scroll bar from scratch on my graphics surface).
Thanks, Mitch.
Thanks for your responses, So I've simplified the Paint method to the following:
private void MyUserControl(object sender, PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
Draw(e.Graphics); // Draw my stuff
}
I've added:
DoubleBuffered = true;
On the load event, (also tried SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);
)
I've removed the resizing of the scroll bar on the controls resize event. Now I just set:
m_verticalScrollBar.Dock = DockStyle.Right;
Still the scroll bar behaves the same on resizing of the user control. Thanks for any further suggestions.
(I appreciate I'm drawing the scroll bar over some of the user controls area, but I don't think that is my problem here. It is the slightly later drawing of the scroll bar that is the problem.)