using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace Tomot.GUI.Controls { #region class pageClass /// /// helperclass for a dictionary /// class pageClass { // public pageClass() public pageClass(Panel panel, ListViewItem lvi) { this._panel = panel; this._lvi = lvi; } private Panel _panel; private ListViewItem _lvi; public Panel Panel { get { return this._panel; } } public ListViewItem ListViewItem { get { return this._lvi; } } } #endregion class settingsPanel:Panel { private ListView _lvSection = new ListView(); private Dictionary _panels = new Dictionary(); private ImageList _img; /// /// Creates an instance of the settingsPanel with an adjustable size /// /// ImageList which contains the Images for the listview /// Width of the Panel /// Height of the Panel public settingsPanel(ImageList icons, Int32 width, Int32 height) { this._img = icons; this.InitializeComponent(width, height); this.Controls.Add(this._lvSection); } /// /// Creates an instance of the settingsPanel with default size of 300x500 pixel. /// /// ImageList which contains the Images for the listview public settingsPanel(ImageList icons) { this._img = icons; this.InitializeComponent(300, 500); this.Controls.Add(this._lvSection); } private void InitializeComponent(Int32 width, Int32 height){ this.Width = width; this.Height = height; this._lvSection.Location = new System.Drawing.Point(5, 5); this._lvSection.LargeImageList = this._img; this._lvSection.Size = new System.Drawing.Size(120, height - 20); this._lvSection.TabIndex = 1; this._lvSection.BorderStyle = BorderStyle.FixedSingle; this._lvSection.View = View.LargeIcon; this._lvSection.MultiSelect = false; this._lvSection.HideSelection = false; this._lvSection.UseCompatibleStateImageBehavior = false; this._lvSection.Sorting = SortOrder.None; this._lvSection.SelectedIndexChanged += new EventHandler(lvSection_SelectedIndexChanged); } /// /// This methods chooses the preselected Panel and ListViewItem. /// /// Name-String of the added Panel public void setDefaultPanel(String panelName) { if (!this._panels.ContainsKey(panelName)) { throw new NullReferenceException("The panel " + panelName + " does not exist"); } // select the listviewitem to ensure usability this._panels[panelName].ListViewItem.Selected = true; //place the panel and make it visible this.placePanel(this._panels[panelName].Panel); } private void lvSection_SelectedIndexChanged(object sender, EventArgs e) { if (this._lvSection.SelectedItems.Count == 0) { return; } // in case the developer forgot to make his panels invisible and to foreach (KeyValuePair dictEntry in this._panels) { dictEntry.Value.Panel.Visible = false; } this.placePanel(this._panels[(this._lvSection.SelectedItems[0].Tag as String)].Panel); } private void placePanel(Panel pan) { // place the panel and make it visible pan.Left = this._lvSection.Left + this._lvSection.Width + 20; pan.Top = this._lvSection.Top; pan.Visible = true; this._lvSection.Select(); } /// /// This methods adds a panel into the dictionary and a new ListViewItem into the listview. /// /// String that cleary identifies a panel /// Reference to an existing panel /// ListViewItem text /// imageKey of the ImageList (references in the constructor) public void addPanel(String panelName, Panel pan, String description, String imageKey){ if (this._panels.ContainsKey(panelName)) { throw new ArgumentException(panelName + " is already in the dictionary"); } ListViewItem lvi = new ListViewItem(description); lvi.ImageKey = imageKey; lvi.Tag = panelName; this._lvSection.Items.Add(lvi); pan.Height = this._lvSection.Height; this._panels.Add(panelName, new pageClass(pan, lvi)); } /// /// This methods removes an entry and all of its graphical representations and selects the first listview entry /// /// String that cleary identifies a panel public void removePanel(String panelName) { if (this._panels.ContainsKey(panelName)) { // remove the listview-entry,set the visibile property to false and remove the dictionary-entry this._panels[panelName].ListViewItem.Remove(); this._panels[panelName].Panel.Visible = false; this._panels.Remove(panelName); // select the first existing listviewitem and its panel if (this._lvSection.Items.Count != 0) { this.setDefaultPanel(this._lvSection.Items[0].Tag as String); } } else { throw new ArgumentNullException(); } } } }