/// <summary>Select an item in the list matching the given value.</summary> /// <param name="value">The value to search.</param> /// <returns>Returns whether an item was selected.</returns> public bool TrySelect(TItem value) { var entry = this.Items.FirstOrDefault(p => (p.Value == null && value == null) || p.Value?.Equals(value) == true ); if (entry == null) { return(false); } this.Selected = entry; return(true); }
/// <summary>Select an item in the list if it's under the cursor.</summary> /// <param name="x">The X-position of the item in the UI.</param> /// <param name="y">The Y-position of the item in the UI.</param> /// <param name="selected">The selected item, if found.</param> /// <returns>Returns whether an item was selected.</returns> public bool TrySelect(int x, int y, out TItem selected) { for (int i = 0; i < this.ItemComponents.Count; i++) { var component = this.ItemComponents[i]; if (component.containsPoint(x, y)) { this.Selected = this.Items[i]; selected = this.SelectedItem; return(true); } } selected = default; return(false); }
/// <summary> /// Trigger of the translate. /// </summary> private void TriggerActive() { DropListItem selectedInputType = ((ComboBoxItem)comBoxInputType.SelectedItem).Tag as DropListItem; DropListItem selectTargetType = ((ComboBoxItem)comBoxOutPut.SelectedItem).Tag as DropListItem; if (null != selectedInputType && null != selectTargetType && !string.IsNullOrEmpty(txtBoxInput.Text.Trim())) { string[] args = { txtBoxInput.Text, selectedInputType.Code, selectTargetType.Code }; back.RunWorkerAsync(args); } }
public TextNDropTestDialog() : base("TextField and DropList Test", 200, 200, 320, 200) { girdLayout = new GirdLayout(4, 1); girdLayout.Right = 16; girdLayout.Left = 16; girdLayout.Top = 8; girdLayout.Bottom = 8; girdLayout.Spacer = 4; girdLayout.SetHorizontalAlignment(1, 0, EHAlignment.HLeft); girdLayout.SetHorizontalAlignment(2, 0, EHAlignment.HCenter); girdLayout.SetHorizontalAlignment(3, 0, EHAlignment.HRight); closeButton = new Button("Close"); //textField=new TextField(160); dropList = new DropList(); option1 = new DropListItem("Option one"); option2 = new DropListItem("Option Two"); option3 = new DropListItem("Option Three"); dropList.Add(option1); dropList.Add(option2); dropList.Add(option3); textLabel = new Label("Text input here:"); optionLabel = new Label("Drop List test:"); Layout = girdLayout; Add(textLabel); //Add(textField); Add(optionLabel); Add(dropList); Add(closeButton); Pack(); closeButton.MouseReleasedEvent += new MouseReleasedHandler(closeButton_MouseReleasedEvent); }
private Collection <DropListItem> InitDropList() { list = new Collection <DropListItem>(); XmlDocument doc = new XmlDocument(); doc.Load(Environment.CurrentDirectory + @"\Configuration\BaseConfiguration.xml"); XmlNode xmlRoot = doc.SelectSingleNode("dropitems"); XmlNodeList xnl = xmlRoot.ChildNodes; DropListItem item; foreach (var i in xnl) { item = new DropListItem(); XmlElement xe = (XmlElement)i; item.DisplayName = xe.GetAttribute("displayname"); item.Code = xe.GetAttribute("code"); list.Add(item); } return(list); }
/********* ** Public methods *********/ /// <summary>Construct an instance.</summary> /// <param name="selectedItem">The selected item.</param> /// <param name="items">The items in the list.</param> /// <param name="getLabel">Get the display label for an item.</param> /// <param name="x">The X-position from which to render the list.</param> /// <param name="y">The Y-position from which to render the list.</param> /// <param name="font">The font with which to render text.</param> public DropdownList(TItem selectedItem, TItem[] items, Func <TItem, string> getLabel, int x, int y, SpriteFont font) : base(new Rectangle(), nameof(DropdownList <TItem>)) { // save values this.Items = items .Select((item, index) => new DropListItem(index, getLabel(item), item)) .ToArray(); this.Font = font; this.MaxLabelHeight = (int)font.MeasureString("abcdefghijklmnopqrstuvwxyz").Y; this.GetLabel = getLabel; // set initial selection int selectedIndex = Array.IndexOf(items, selectedItem); this.Selected = selectedIndex >= 0 ? this.Items[selectedIndex] : this.Items.First(); // initialize UI this.bounds.X = x; this.bounds.Y = y; this.ReinitializeComponents(); }
/// <summary>Render the UI.</summary> /// <param name="sprites">The sprites to render.</param> /// <param name="opacity">The opacity at which to draw.</param> public void Draw(SpriteBatch sprites, float opacity = 1) { // draw dropdown items foreach (ClickableComponent component in this.ItemComponents) { // draw background if (component.containsPoint(Game1.getMouseX(), Game1.getMouseY())) { sprites.Draw(Sprites.DropDown.Sheet, component.bounds, Sprites.DropDown.HoverBackground, Color.White * opacity); } else if (component.name.Equals(this.SelectedIndex.ToString())) { sprites.Draw(Sprites.DropDown.Sheet, component.bounds, Sprites.DropDown.ActiveBackground, Color.White * opacity); } else { sprites.Draw(Sprites.DropDown.Sheet, component.bounds, Sprites.DropDown.InactiveBackground, Color.White * opacity); } // draw text DropListItem <TItem> item = this.Items.First(p => p.Index == int.Parse(component.name)); Vector2 position = this.ToRight ? new Vector2(component.bounds.X + DROPDOWN_PADDING, component.bounds.Y + Game1.tileSize / 16) : new Vector2(component.bounds.X + component.bounds.Width - this.Font.MeasureString(item.Name).X - DROPDOWN_PADDING, component.bounds.Y + Game1.tileSize / 16); sprites.DrawString(this.Font, item.Name, position, Color.Black * opacity); } // draw up/down arrows if (this.FirstVisibleIndex > 0) { sprites.Draw(Sprites.Icons.Sheet, new Vector2(this.bounds.X - Sprites.Icons.UpArrow.Width, this.bounds.Y), Sprites.Icons.UpArrow, Color.White * opacity); } if (this.FirstVisibleIndex < this.MaxFirstVisibleIndex) { sprites.Draw(Sprites.Icons.Sheet, new Vector2(this.bounds.X - Sprites.Icons.UpArrow.Width, this.bounds.Y + this.bounds.Height - Sprites.Icons.DownArrow.Height), Sprites.Icons.DownArrow, Color.White * opacity); } }
/// <summary>Render the UI.</summary> /// <param name="sprites">The sprites to render.</param> /// <param name="opacity">The opacity at which to draw.</param> public void Draw(SpriteBatch sprites, float opacity = 1) { // draw dropdown items for (int i = 0; i < this.ItemComponents.Count; i++) { var component = this.ItemComponents[i]; // draw background if (component.containsPoint(Game1.getMouseX(), Game1.getMouseY())) { sprites.Draw(CommonSprites.DropDown.Sheet, component.bounds, CommonSprites.DropDown.HoverBackground, Color.White * opacity); } else if (i == this.Selected.Index) { sprites.Draw(CommonSprites.DropDown.Sheet, component.bounds, CommonSprites.DropDown.ActiveBackground, Color.White * opacity); } else { sprites.Draw(CommonSprites.DropDown.Sheet, component.bounds, CommonSprites.DropDown.InactiveBackground, Color.White * opacity); } // draw text DropListItem item = this.Items.First(p => p.Index == int.Parse(component.name)); Vector2 position = new Vector2(component.bounds.X + DropdownList <TItem> .DropdownPadding, component.bounds.Y + Game1.tileSize / 16); sprites.DrawString(this.Font, item.Name, position, Color.Black * opacity); } // draw up/down arrows if (this.FirstVisibleIndex > 0) { sprites.Draw(CommonSprites.Icons.Sheet, new Vector2(this.bounds.X - CommonSprites.Icons.UpArrow.Width, this.bounds.Y), CommonSprites.Icons.UpArrow, Color.White * opacity); } if (this.FirstVisibleIndex < this.MaxFirstVisibleIndex) { sprites.Draw(CommonSprites.Icons.Sheet, new Vector2(this.bounds.X - CommonSprites.Icons.UpArrow.Width, this.bounds.Y + this.bounds.Height - CommonSprites.Icons.DownArrow.Height), CommonSprites.Icons.DownArrow, Color.White * opacity); } }
public abstract void PaintDropListItem(DropListItem component);
public abstract Size GetDropListItemPreferedSize(DropListItem component);