Results 1 to 3 of 3

Thread: Scrolling in a panel doesn't work in beginning

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Scrolling in a panel doesn't work in beginning

    Hi,

    I'm filling a panel with a set of controls in OnLoad. Autoscroll is set to true. I want to scroll to the top but it never works. The same exact code does work however, when I execute the code when everything is up and running (for instance on a button press).

    Is this an initialisation problem?

    Code:
      
    protected override void OnLoad(EventArgs e)
          {
          base.OnLoad(e);
    
          m_projects_split_main.Panel1.AutoScroll = true;
    
          // Set up filters;
          m_filter_comment3 = new FilterControlText("Comment 3");
          _AddProjectsFilter(m_filter_comment3);
    
          m_filter_comment2 = new FilterControlText("Comment 2");
          _AddProjectsFilter(m_filter_comment2);
    
          m_filter_comment1 = new FilterControlText("Comment 1");
          _AddProjectsFilter(m_filter_comment1);  
    
          m_filter_id            = new FilterControlText("Project id");
          _AddProjectsFilter(m_filter_id);
          
          // this code or equivalent (setting vscroll manually) does not work yet
          m_projects_split_main.Panel1.ScrollControlIntoView(m_filter_id);
          }
    
        private void _AddProjectsFilter(FilterControl i_filter)
          {
          i_filter.Dock = DockStyle.Top;
          m_projects_split_main.Panel1.Controls.Add(i_filter);
          i_filter.InputChanged += ActionFilterProjects;
          }
    So only after everything is running, and I call ScrollControlIntoView again, then it does what's expected. Can anyone help please?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Scrolling in a panel doesn't work in beginning

    I'd advise handling the Load event rather than overriding the OnLoad method.

    Your problem is occurring because the OnLoad method is executed BEFORE the form becomes visible, so you cannot scroll a control into view when it can't be seen. If you're using .NET 2.0 (please specify in future) then you can handle the Shown event, which is raised immediately after the form is displayed for the first time. If you're using an earlier version then you can use the Activated event, but you'll need to use a class-level variable to ensure that your code is only executed the first time the form is activated:
    Code:
    private bool firstActivation = true;
    
    private void Form1_Activated(object sender, EventArgs e)
    {
        if (this.firstActivation)
        {
            // Do whatever here.
            this.firstActivation = false;
        }
    }
    Also, it looks like some of what you're doing there would be more appropriately perfromed in the constructor or even in the designer. Is there any reason you've chosen not to set the AutoScroll property of a Panel in the designer?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    2

    Re: Scrolling in a panel doesn't work in beginning

    Thank you. I am indeed using .NET 2.0, sorry for forgetting to mention it.

    Your solution works perfectly when I use the control in a Form. However my control is can also be displayed in a webpage, and then there is no Shown event to hook into it seems. Would the PreRender event be okay there?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width