From 15b462d2c049f11f2f05f65531690b1556ea7562 Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 27 May 2019 20:21:40 +0100 Subject: [PATCH] Added: PanelBook --- 2019/05/PanelBook.Designer.cs | 37 ++++++++ 2019/05/PanelBook.cs | 159 ++++++++++++++++++++++++++++++++++ 2019/05/PanelCollection.cs | 80 +++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 2019/05/PanelBook.Designer.cs create mode 100644 2019/05/PanelBook.cs create mode 100644 2019/05/PanelCollection.cs diff --git a/2019/05/PanelBook.Designer.cs b/2019/05/PanelBook.Designer.cs new file mode 100644 index 0000000..576b43a --- /dev/null +++ b/2019/05/PanelBook.Designer.cs @@ -0,0 +1,37 @@ +namespace RyzStudio.Windows.Forms +{ + partial class PanelBook + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + } + + #endregion + } +} diff --git a/2019/05/PanelBook.cs b/2019/05/PanelBook.cs new file mode 100644 index 0000000..190ec28 --- /dev/null +++ b/2019/05/PanelBook.cs @@ -0,0 +1,159 @@ +namespace RyzStudio.Windows.Forms +{ + using System; + using System.ComponentModel; + using System.Windows.Forms; + + [ToolboxItem(true)] + public partial class PanelBook : UserControl + { + protected PanelCollection panelCollection = null; + + public PanelBook() + { + InitializeComponent(); + + panelCollection = new PanelCollection(this); + + } + + public Panel ActivePanel { get; set; } = null; + + [Category("Collection")] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] + public PanelCollection Pages + { + get { return panelCollection; } + } + + [Category("Collection")] + public int SelectedIndex + { + get + { + if (panelCollection.Count <= 0) + { + return -1; + } + + return panelCollection.IndexOf(this.ActivePanel); + } + set + { + if (panelCollection.Count <= 0) + { + return; + } + + if (value < 0) + { + return; + } + + if (value > (panelCollection.Count - 1)) + { + return; + } + + if (value == this.SelectedIndex) + { + return; + } + + ActivatePanel(value); + } + } + + protected internal int PageIndex + { + get + { + return panelCollection.IndexOf(this.ActivePanel); + } + set + { + if (panelCollection.Count <= 0) + { + ActivatePanel(-1); + return; + } + + if ((value < -1) || (value >= panelCollection.Count)) + { + throw new ArgumentOutOfRangeException("PageIndex", value, "The page index must be between 0 and " + Convert.ToString(panelCollection.Count - 1)); + } + + ActivatePanel(value); + } + } + + protected internal void ActivatePanel(int index) + { + if ((panelCollection.Count == 0) && (index >= panelCollection.Count) && (index <= 0)) + { + return; + } + + Panel p = (Panel)panelCollection[index]; + + ActivatePage(p); + } + + protected internal void ActivatePage(Panel page) + { + if (this.ActivePanel != null) + { + this.ActivePanel.Visible = false; + } + + this.ActivePanel = page; + if (this.ActivePanel != null) + { + this.ActivePanel.Parent = this; + if (!this.Contains(this.ActivePanel)) + { + this.Container.Add(this.ActivePanel); + } + + this.ActivePanel.Dock = DockStyle.Fill; + this.ActivePanel.Visible = true; + this.ActivePanel.BringToFront(); + } + + if (this.ActivePanel != null) + { + this.ActivePanel.Invalidate(); + } + else + { + this.Invalidate(); + } + } + +#if DEBUG + + protected override void OnResize(EventArgs e) + { + base.OnResize(e); + + if (this.DesignMode) + { + this.Invalidate(); + } + } + +#endif + + protected override void DestroyHandle() + { + base.DestroyHandle(); + + foreach (Panel p in panelCollection) + { + p.Dispose(); + } + } + + + } +} diff --git a/2019/05/PanelCollection.cs b/2019/05/PanelCollection.cs new file mode 100644 index 0000000..af9838b --- /dev/null +++ b/2019/05/PanelCollection.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace RyzStudio.Windows.Forms +{ + [ToolboxItem(true)] + public class PanelCollection : CollectionBase + { + protected PanelBook panelBook = null; + + public PanelCollection() + { + + } + + public PanelCollection(PanelBook parent) : base() + { + panelBook = parent; + } + + public PanelBook Parent => panelBook; + + public Panel this[int index] { get => (Panel)List[index]; set => List[index] = value; } + + public int Add(Panel value) => List.Add(value); + + public void AddRange(Panel[] pages) + { + foreach (Panel page in pages) + { + this.Add(page); + } + } + + public int IndexOf(Panel value) => (List.IndexOf(value)); + + public void Insert(int index, Panel value) => List.Insert(index, value); + + public void Remove(Panel value) => List.Remove(value); + + public bool Contains(Panel value) => List.Contains(value); + + protected override void OnInsertComplete(int index, object value) + { + base.OnInsertComplete(index, value); + + if (panelBook != null) + { + panelBook.PageIndex = index; + } + } + + protected override void OnRemoveComplete(int index, object value) + { + base.OnRemoveComplete(index, value); + + if (panelBook != null) + { + if (panelBook.PageIndex == index) + { + if (index < InnerList.Count) + { + panelBook.PageIndex = index; + } + else + { + panelBook.PageIndex = InnerList.Count - 1; + } + } + } + } + + } +}