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?

Recommended Answers

All 6 Replies

Can you post your code? This probably depends on the implementation of scrollbars in the panel

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.

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
    }
commented: add to my repuation :D +10

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?

mark this thread as solved :D

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.