PageViewCollection.cs
Upload User: zolk2007
Upload Date: 2021-04-29
Package Size: 3504k
Code Size: 4k
Development Platform:

C#

  1. using System;
  2. using System.Web.UI;
  3. using System.ComponentModel;
  4. namespace ComponentArt.Web.UI
  5. {
  6.   /// <summary>
  7.   /// Collection of <see cref="PageView"/> controls.
  8.   /// </summary>
  9.   [Editor("System.Windows.Forms.Design.CollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")]
  10.   public class PageViewCollection : ControlCollection
  11.   {
  12.     // Needed by Intellisense in framework 2, but causes a runtime error in framework 1
  13.     public new PageView this[int index]
  14.     {
  15.       get
  16.       {
  17.         return (PageView)base[index];
  18.       }
  19.     }
  20.     /// <summary>
  21.     /// Initializes a new instance of a PageViewCollection. 
  22.     /// </summary>
  23.     /// <param name="owner">The parent MultiPage control.</param>
  24.     public PageViewCollection(MultiPage owner)
  25.       : base(owner)
  26.     {
  27.     }
  28.     /// <summary>
  29.     /// Verifies that a child control is a PageView.
  30.     /// If it is, then certain properties are set.
  31.     /// If it is not, then an exception is thrown.
  32.     /// </summary>
  33.     /// <param name="child">The child control.</param>
  34.     private void VerifyChild(Control child)
  35.     {
  36.       if (child is PageView)
  37.       {
  38.         ((PageView)child).ParentMultiPage = (MultiPage)Owner;
  39.         return;
  40.       }
  41.       throw new Exception("Child is not a PageView");
  42.     }
  43.     /// <summary>
  44.     /// Adds a control to the collection.
  45.     /// </summary>
  46.     /// <param name="child">The child control.</param>
  47.     public override void Add(Control child)
  48.     {
  49.       VerifyChild(child);
  50.       base.Add(child);
  51.     }
  52.     /// <summary>
  53.     /// Adds a control to the collection at a specific index.
  54.     /// </summary>
  55.     /// <param name="index">The index where the control should be added.</param>
  56.     /// <param name="child">The child control.</param>
  57.     public override void AddAt(int index, Control child)
  58.     {
  59.       VerifyChild(child);
  60.       base.AddAt(index, child);
  61.       MultiPage mpParent = (MultiPage)Owner;
  62.       int curIndex = mpParent.SelectedIndex;
  63.       if (index <= curIndex)
  64.       {
  65.         curIndex++;
  66.         if (curIndex < Count)
  67.         {
  68.           mpParent.SelectedIndex = curIndex;
  69.         }
  70.       }
  71.     }
  72.     /// <summary>
  73.     /// After removing an index, adjust the selected index.
  74.     /// </summary>
  75.     /// <param name="index">The index of the element that was removed.</param>
  76.     private void RemovedIndex(int index)
  77.     {
  78.       MultiPage mpParent = (MultiPage)Owner;
  79.       int curIndex = mpParent.SelectedIndex;
  80.       if ((index >= 0) && (index < curIndex))
  81.       {
  82.         mpParent.SelectedIndex = curIndex - 1;
  83.       }
  84.       else if ((index == curIndex) && (curIndex > 0) && (Count <= curIndex))
  85.       {
  86.         mpParent.SelectedIndex = Count - 1;
  87.       }
  88.     }
  89.     /// <summary>
  90.     /// Removes the specified item from the collection.
  91.     /// </summary>
  92.     /// <param name="value">The item to remove from the collection.</param>
  93.     public override void Remove(Control value)
  94.     {
  95.       int index = IndexOf(value);
  96.       base.Remove(value);
  97.       RemovedIndex(index);
  98.     }
  99.     /// <summary>
  100.     /// Removes the item at the specified index.
  101.     /// </summary>
  102.     /// <param name="index">The index of the item to remove.</param>
  103.     public override void RemoveAt(int index)
  104.     {
  105.       base.RemoveAt(index);
  106.       RemovedIndex(index);
  107.     }
  108.     /// <summary>
  109.     /// Clears the collection.
  110.     /// </summary>
  111.     public override void Clear()
  112.     {
  113.       if (Count > 0)
  114.       {
  115.         // This is to prepare for new items later.
  116.         // When a new item is added later, we want the SelectedIndex to reset to 0.
  117.         MultiPage mpParent = (MultiPage)Owner;
  118.         mpParent.SelectedIndex = 0;
  119.       }
  120.       base.Clear();
  121.     }
  122.   }
  123. }