943,673 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1251
  • C# RSS
Sep 25th, 2009
0

Controls added to panel are moving down when repainted

Expand Post »
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?
Similar Threads
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Sep 25th, 2009
0

Re: Controls added to panel are moving down when repainted

Can you post your code? This probably depends on the implementation of scrollbars in the panel
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 25th, 2009
0

Re: Controls added to panel are moving down when repainted

C# Syntax (Toggle Plain Text)
  1. private const int Pad = 25;
  2.  
  3. private void PaintMessages()
  4. {
  5. Point CurrentPoint = panel1.AutoScrollPosition;
  6. panel1.SuspendLayout();
  7. panel1.Controls.Clear();
  8. CurrentY = 0;
  9. for (int i = 0; i < Messages.Count; i++)
  10. {
  11. int offset = ((panel1.Width - (2 * Pad)) / 3);
  12.  
  13. ControlTitleBar title = new ControlTitleBar(Messages[i].ID);
  14. WebBrowser browser = new WebBrowser();
  15. browser.DocumentText = Messages[i].Content;
  16. title.isFlagged = Messages[i].isFlagged;
  17. title.FlagClicked += new btnFlagClickedHandler(title_FlagClicked);
  18. browser.Width = 2 * offset;
  19. title.Width = 2 * offset;
  20. browser.Height = browser.GetPreferredSize(new Size(2 * offset, 0)).Height; //this does NOT work with Webbrowsers...solution needed
  21.  
  22. Point loc;
  23. if (Messages[i].Type == MessageType.Received)
  24. {
  25. loc = new Point(Pad, CurrentY + Pad);
  26. if (ReplyToId == "")
  27. ReplyToId = Messages[i].ExternalID;
  28. }
  29. else
  30. {
  31. loc = new Point(Pad + offset, CurrentY + Pad);
  32. }
  33.  
  34. title.Location = loc;
  35. loc.Y += title.Height;
  36. browser.Location = loc;
  37. panel1.Controls.Add(browser);
  38. panel1.Controls.Add(title);
  39. CurrentY = browser.Location.Y + browser.Height;
  40. }
  41. panel1.AutoScrollPosition = new Point(Math.Abs(panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y));
  42. panel1.ResumeLayout();
  43. }

Is that what you need? Ive checked in code and the locations are being set correctly...just not displaying correctly : /
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Sep 25th, 2009
1

Re: Controls added to panel are moving down when repainted

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.

C# Syntax (Toggle Plain Text)
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. int top = 0;
  4. Point pos = panel1.AutoScrollPosition; //retain the scrolled point
  5.  
  6. panel1.SuspendLayout();
  7. panel1.Controls.Clear();
  8. panel1.AutoScrollPosition = Point.Empty; //clear the scrolled point
  9. for (int i1 = 0; i1 < 10; i1++)
  10. {
  11. TextBox txt = new TextBox();
  12. txt.Width = this.Width;
  13. txt.Top = top;
  14. txt.Text = "btn" + (i1 + 1).ToString();
  15. panel1.Controls.Add(txt);
  16. top += txt.Height;
  17. }
  18. panel1.ResumeLayout();
  19. panel1.AutoScrollPosition = new Point(Math.Abs(pos.X), Math.Abs(pos.Y)); //restore it
  20. }
Last edited by sknake; Sep 25th, 2009 at 1:24 pm.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 25th, 2009
0

Re: Controls added to panel are moving down when repainted

Click to Expand / Collapse  Quote originally posted by sknake ...
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?
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 25th, 2009
0

Re: Controls added to panel are moving down when repainted

mark this thread as solved
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Sep 28th, 2009
0

Re: Controls added to panel are moving down when repainted

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
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: direct robot
Next Thread in C# Forum Timeline: read file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC