/* * Box Social™ * http://boxsocial.net/ * Copyright © 2007, David Lachlan Smith * * $Id:$ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Web; namespace BoxSocial.Forms { /// /// Represents a select box on an XHTML form. /// public sealed class SelectBox : FormField, IEnumerable { private List items; private Dictionary itemKeys; private string selectedKey; protected bool visible; private StyleLength width; private ScriptProperty script; public bool IsVisible { get { return visible; } set { visible = false; } } /// /// Selected item. /// public string SelectedKey { get { return selectedKey; } set { if (itemKeys.ContainsKey(value)) { // unselect previous selected foreach (SelectBoxItem item in items) { item.selected = false; } // select the newly selected selectedKey = value; itemKeys[value].selected = true; } } } public ScriptProperty Script { get { return script; } } /// /// Select box constructor. /// /// Select box name public SelectBox(string name) { this.name = name; items = new List(); itemKeys = new Dictionary(); visible = true; width = new StyleLength(); script = new ScriptProperty(); } /// /// Get a select box item. /// /// Item key /// The select box item with the key public SelectBoxItem this[string key] { get { return itemKeys[key]; } } /// /// Add a select box item to the select box. /// /// public void Add(SelectBoxItem item) { if (!itemKeys.ContainsKey(item.Key)) { items.Add(item); itemKeys.Add(item.Key, item); } } /// /// Returns true is contains the given key. /// /// /// public bool ContainsKey(string key) { return itemKeys.ContainsKey(key); } /// /// Creates a string representing the XHTML syntax for the select box. /// /// Returns XHTML public override string ToString() { StringBuilder selectBox = new StringBuilder(); selectBox.AppendLine(string.Format(""); return selectBox.ToString(); } public IEnumerator GetEnumerator () { return items.GetEnumerator(); } public override int GetHashCode () { return items.GetHashCode(); } } }