| | |
Controls added to panel are moving down when repainted
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
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?
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?
C# Syntax (Toggle Plain Text)
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 : /
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)
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 }
Last edited by sknake; Sep 25th, 2009 at 1:24 pm.
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 118
•
•
•
•
Can you post your code? This probably depends on the implementation of scrollbars in the panel
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.
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

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
![]() |
Similar Threads
- A couple of queries (C#)
- panel->Visible = true; /false; (C++)
- Drag and Drop the controls using .net 3.0+WPF (C#)
- How to encapsulate a GridView in a <fieldset> tag? (ASP.NET)
- Getting values from GridView Controls (ASP.NET)
- Web Controls (ASP.NET)
- Displaying Label over other controls (C#)
- Reading Controls from Panel (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: direct robot
- Next Thread: read file
| Thread Tools | Search this Thread |
.net 2007 access algorithm appportability array barchart bitmap box broadcast c# camera check checkbox client combobox control conversion cs4 csharp custom database datagrid datagridview dataset date datetime degrees development draganddrop drawing encryption enum event eventcloseformc# excel file form format forms function gdi+ handler httpwebrequest image index input install java keypress label list listbox listener listview load mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting resolved. richtextbox search security server sleep socket sql statistics stream string table text textbox thread time timer update usercontrol validation view visual visualstudio webbrowser windows winforms wordautomation wpf xml






