Controls added to panel are moving down when repainted

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz

Controls added to panel are moving down when repainted

 
0
  #1
Sep 25th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,207
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast

Re: Controls added to panel are moving down when repainted

 
0
  #2
Sep 25th, 2009
Can you post your code? This probably depends on the implementation of scrollbars in the panel
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz

Re: Controls added to panel are moving down when repainted

 
0
  #3
Sep 25th, 2009
  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 : /
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,207
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is online now Online
.NET Enthusiast

Re: Controls added to panel are moving down when repainted

 
1
  #4
Sep 25th, 2009
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.

  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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Controls added to panel are moving down when repainted

 
0
  #5
Sep 25th, 2009
Originally Posted by sknake View Post
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?
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Controls added to panel are moving down when repainted

 
0
  #6
Sep 25th, 2009
mark this thread as solved
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz

Re: Controls added to panel are moving down when repainted

 
0
  #7
Sep 28th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC