Search KB Filter article types
Implementing a Persistent Splitter in Visual WebGui
Categories: Splitters, UI Behaviors, Control Behaviors
Tags: Architects, Developers, C#, 2. Intermediate, 3. Advanced, Customization, Layouting, Pre v6.3, v6.3, v6.4 and Later
Revision: 1
Posted: 05/Jan/2008
Updated: 05/Jan/2008
Status: Publish
Types: Code

The splitter is a great component. It allows splitting the workspace on one or more panels, each with their own controls.
However, one nice feature for splitters would be to allow users to customize the size of the panels, to widen / narrow them to meet various needs, and remember the size when the application is used next time
So I enhanced the build-in VWG splitter to implement this feature.
The things I faced when I tried to implement this were how to set the splitter position at runtime. After some tests I noticed the position of the splitter is actually determined not by some properties of the splitter itself, but by the size of the control the splitter is docked on.
To save / restore the splitter position, I use a cookie.
The Persistent Splitter component has design time support, and it has two properties that can be set at design time:
· CookieName - the name of the cookie used to save / load the splitter name
· DockedControl - the name of the control the splitter is docked on
The Persistent splitter has two main working private methods:
· SaveSplitterPosition – This is called from the Resize event handler for the control the splitter is docked on, and, depending on the dock style of the splitter, it saves in the cookie the value of the width (for vertical splitter) or height (for horizontal splitter) of the docking control.
· RestoreSplitterPosition – This is called from the SET accessor of the HostControlLoaded property. It tells the splitter that the component that hosts the splitter and related controls (particularly the control the splitter is docked on) was loaded, so their position can be manipulated in code).
The PersistentSplitter functionality is controlled by the HostControlLoaded property. This is write/only, and is used to notify the splitter when the component the splitter is docked on is available. In that moment the splitter can restore its position by adjusting the size of it’s docking component. This should happen in Load event of the host component (form or user control) by setting the HostControlLoaded property to true. This tells the PersistentSplitter control that all controls on the form was loaded, so it can set the size of left control from the cookie to restore its position (as below):

private void Form1_Load(object sender, EventArgs e)
{
 persistentSplitter1.HostControlLoaded = true;
}
The code in SaveSplitterPosition is straightforward:
 
private void SaveSplitterPosition()
{
 // if we have the cookie name set
 if (m_cookieName != string.Empty)
    // if this vertical splitter, save the width of docked control
    if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
      VWGHelper.SetCookiesParameter(m_cookieName,
        m_dockedControl.Width.ToString());
    else
      // if this is horizontal splitter, save the height
      if (this.Dock == DockStyle.Top || this.Dock == DockStyle.Bottom)
        VWGHelper.SetCookiesParameter(m_cookieName,
          m_dockedControl.Height.ToString());
      else
        // for safety
        throw new Exception(
          string.Format("{0}: Invalid Dock value", this.Name));
}

RestoreSplitterPosition works similarily. There is only one issue – since in this method we change the size of the docked control, this will fire Resize event, which will cause the event handler to call SaveSplitterPosition, which will attempt to save the cookie value again. To prevent this, we unbind from Resize event when we start the procedure, and bind again when finishing it. The code is shown below

private void RestoreSplitterPosition()
{
 if (m_dockedControl != null && m_cookieName != string.Empty)
 {
    // we unbind from resize event of docked control,
    // to avoid saving the position again in cookie
    m_dockedControl.Resize -= new EventHandler(
                                     m_dockedControl_Resize);
    try
    {
      // read the last location from cookie
      string offsetStr = VWGHelper.GetCookiesParameter(m_cookieName);
      if (offsetStr != string.Empty)
      {
        int offsetInt = Int32.Parse(offsetStr);
        bool dockValid = false;
 
        // if vertical splitter
        if (this.Dock == DockStyle.Left ||
            this.Dock == DockStyle.Right)
        {
          m_dockedControl.Width = offsetInt;
          dockValid = true;
        }
        else
          // if vertical splitter
          if (this.Dock == DockStyle.Top ||
                 this.Dock == DockStyle.Bottom)
          {
            m_dockedControl.Height = offsetInt;
            dockValid = true;
          }
        if (!dockValid)
          throw new Exception(
            string.Format("{0}: Invalid Dock value", this.Name));
      }
    }
    catch (Exception ex)
    {
      throw new Exception(
        "Error in PersistentSplitter.RestoreSplitterPosition", ex) ;
    }
    finally
    {
      // we make sure we rebind to resize event,
      // to save new splitter position if user change it
      m_dockedControl.Resize  = new EventHandler(
                                    m_dockedControl_Resize);
    }
 }
}

You can view the original article and a sample application at
http://zbconsulting.dyndns.org/ArticlesandTutorials/VisualWebGuiArticles/PersistentSplittercomponentinVisualWebGui/tabid/92/Default.aspx

[Read More...]



 

About the author

Related Articles

Splitters  
Title Update Author
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Architects, Developers, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Developers, 1. Beginner, 2. Intermediate, Customization, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
24/May/2010    2010/05/24
Tags: Developers, C#, JavaScript, VB.NET, 2. Intermediate, 3. Advanced, Customization, DHTML, jQuery, v6.3, v6.4 and Later
24/Nov/2010    2010/11/24
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS