Controls added to panel are moving down when repainted
I have a Panel on a form to which i have added a selection of controls with (x,y) coordinates.
The panel has Autoscroll enabled. If there are enough controls to require a scrollbar, and the bar has been scrolled down when the paint event fires, the controls all move down when the panel paints.
Each time this process is repeated the controls are shuffled down. This results in a gap at the top of the panel : /
I have checked through debugging and the coordinates arent changing so the control is still at (25, 25) but now (25,25) is further down the panel : / Im really stumped on this one. Can anyone shed some light?
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
Can you post your code? This probably depends on the implementation of scrollbars in the panel
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
private const int Pad = 25;
private void PaintMessages()
{
Point CurrentPoint = panel1.AutoScrollPosition;
panel1.SuspendLayout();
panel1.Controls.Clear();
CurrentY = 0;
for (int i = 0; i < Messages.Count; i++)
{
int offset = ((panel1.Width - (2 * Pad)) / 3);
ControlTitleBar title = new ControlTitleBar(Messages[i].ID);
WebBrowser browser = new WebBrowser();
browser.DocumentText = Messages[i].Content;
title.isFlagged = Messages[i].isFlagged;
title.FlagClicked += new btnFlagClickedHandler(title_FlagClicked);
browser.Width = 2 * offset;
title.Width = 2 * offset;
browser.Height = browser.GetPreferredSize(new Size(2 * offset, 0)).Height; //this does NOT work with Webbrowsers...solution needed
Point loc;
if (Messages[i].Type == MessageType.Received)
{
loc = new Point(Pad, CurrentY + Pad);
if (ReplyToId == "")
ReplyToId = Messages[i].ExternalID;
}
else
{
loc = new Point(Pad + offset, CurrentY + Pad);
}
title.Location = loc;
loc.Y += title.Height;
browser.Location = loc;
panel1.Controls.Add(browser);
panel1.Controls.Add(title);
CurrentY = browser.Location.Y + browser.Height;
}
panel1.AutoScrollPosition = new Point(Math.Abs(panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y));
panel1.ResumeLayout();
}
Is that what you need? Ive checked in code and the locations are being set correctly...just not displaying correctly : /
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
I see. You need to set the scroll position to zero after clearing the controls, add your controls, then scroll it back where it was.
private void button1_Click(object sender, EventArgs e)
{
int top = 0;
Point pos = panel1.AutoScrollPosition; //retain the scrolled point
panel1.SuspendLayout();
panel1.Controls.Clear();
panel1.AutoScrollPosition = Point.Empty; //clear the scrolled point
for (int i1 = 0; i1 < 10; i1++)
{
TextBox txt = new TextBox();
txt.Width = this.Width;
txt.Top = top;
txt.Text = "btn" + (i1 + 1).ToString();
panel1.Controls.Add(txt);
top += txt.Height;
}
panel1.ResumeLayout();
panel1.AutoScrollPosition = new Point(Math.Abs(pos.X), Math.Abs(pos.Y)); //restore it
}
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Can you post your code? This probably depends on the implementation of scrollbars in the panel
We should create a generic c# thread class in daniweb. that class will be inherited by all the threads and will add some common things that we keep repeating. for example, post your code, attach your application, mark as solved and such.
that will add these texts like a header to the thread, so as soon as the guy posted it, he will see all these and take the required action. How is that?
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
mark this thread as solved :D
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
setting the scrol to zero solved it, cheers sknake :)
Sorry for late reply/solved, had no time at all over the weekend to look at this.
Like the idea serkan, im new to the board, i've always managed to resolve issues or just ignored them so im still gettin used to the requirements for posting problems :p A reminder when posting would be helpful haha
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246