Example T163562
Visible to All Users

WinForms Token Editor - Create a dropdown with the TreeList control

This example creates a custom dropdown menu with integrated TreeList control:

WinForms Token Editor - Create a dropdown with the WinForms TreeList control

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

TokenEditTest/CustomTokenEditDropDownControl.cs(vb)
C#
using System; using System.Collections.Generic; using System.Windows.Forms; using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.Popup; using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Nodes; namespace TokenEditTest { public partial class CustomTokenEditDropDownControl : CustomTokenEditDropDownControlBase { public CustomTokenEditDropDownControl() { InitializeComponent(); } public override void Initialize(TokenEdit ownerEdit, TokenEditPopupForm ownerPopup) { base.Initialize(ownerEdit, ownerPopup); } public override void InitializeAppearances(AppearanceObject appearanceDropDown) { base.InitializeAppearances(appearanceDropDown); } public override void SetDataSource(object dataSource) { TreeList.DataSource = new TreeListDataSet((IEnumerable<TokenEditToken>)dataSource); TreeList.ExpandAll(); } string currentFilter = string.Empty; public override void SetFilter(string filter, string columnName) { this.currentFilter = filter; TreeList.FilterNodes(); if(TreeList.VisibleNodesCount == 0) OwnerEdit.ClosePopup(PopupCloseMode.Cancel); } public override void OnShowingPopupForm() { base.OnShowingPopupForm(); } public override int GetItemCount() { return TreeList.VisibleNodesCount; } public override int CalcFormWidth() { return 500; } public override int CalcFormHeight(int itemCount) { return 200; } public override object GetResultValue() { TreeListLeaf leaf = GetSelectedDataItem() as TreeListLeaf; return leaf != null ? leaf.Token : null; } public override bool IsTokenSelected { get { return this.selNode != null; } } public override void ResetSelection() { this.selNode = null; TreeList.FocusedNode = null; } public override void ProcessKeyDown(KeyEventArgs e) { base.ProcessKeyDown(e); TreeList.DoKeyDown(e); } public override void OnEditorMouseWheel(MouseEventArgs e) { int step = SystemInformation.MouseWheelScrollLines > 0 ? SystemInformation.MouseWheelScrollLines : 1; if(e.Delta > 0) step *= -1; TreeList.TopVisibleNodeIndex += step; } public override void ResetResultValue() { this.selNode = null; } #region TreeList Handlers TreeListNode selNode = null; void OnTreeListFocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e) { this.selNode = e.Node; } void OnTreeListDoubleClick(object sender, EventArgs e) { TreeListHitInfo hi = TreeList.CalcHitInfo(TreeList.PointToClient(Control.MousePosition)); if(hi.HitInfoType == HitInfoType.Cell && GetSelectedDataItem() is TreeListLeaf) { OwnerEdit.ClosePopup(PopupCloseMode.Normal); } } object GetSelectedDataItem() { return this.selNode != null ? TreeList.GetDataRecordByNode(this.selNode) : null; } void OnTreeListVirtualTreeGetChildNodes(object sender, VirtualTreeGetChildNodesInfo e) { if(e.Node is TreeListDataSet) { e.Children = ((TreeListDataSet)e.Node).GetChildren(); } if(e.Node is TreeListRootObj) { e.Children = new object[] { ((TreeListRootObj)e.Node).Leaf }; } } void OnTreeListVirtualTreeGetCellValue(object sender, VirtualTreeGetCellValueInfo e) { if(e.Node is TreeListRootObj || e.Node is TreeListLeaf) e.CellData = e.Node.ToString(); } void OnTreeListFilterNode(object sender, FilterNodeEventArgs e) { TreeListRootObj obj = TreeList.GetDataRecordByNode(e.Node) as TreeListRootObj; if(obj != null && !obj.Leaf.IsMatch(currentFilter)) { e.Node.Visible = false; e.Handled = true; } } #endregion protected CustomTreeList TreeList { get { return treeList; } } } public class CustomTreeList : TreeList { public CustomTreeList() { BorderStyle = BorderStyles.NoBorder; SetStyle(ControlStyles.UserMouse, false); } public void DoKeyDown(KeyEventArgs e) { OnKeyDown(e); } } }
TokenEditTest/Data.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using DevExpress.XtraEditors; namespace TokenEditTest { public class TreeListDataSet { IList<TreeListRootObj> rootNodes; public TreeListDataSet(IEnumerable<TokenEditToken> tokens) { this.rootNodes = CreateRootCol(tokens); } IList<TreeListRootObj> CreateRootCol(IEnumerable<TokenEditToken> tokens) { List<TreeListRootObj> res = new List<TreeListRootObj>(); foreach(TokenEditToken token in tokens) { res.Add(new TreeListRootObj(token)); } return res; } public int Count { get { return Children.Count; } } public TreeListRootObj[] GetChildren() { return this.rootNodes.ToArray(); } public IList<TreeListRootObj> Children { get { return rootNodes; } } } public class TreeListRootObj { TreeListLeaf leaf; public TreeListRootObj(TokenEditToken token) { this.leaf = new TreeListLeaf(token); } public override string ToString() { return "Root Node"; } public TreeListLeaf Leaf { get { return leaf; } } } public class TreeListLeaf { TokenEditToken token; public TreeListLeaf(TokenEditToken token) { this.token = token; } public bool IsMatch(string filter) { string filterCore = filter.Trim(); if(string.IsNullOrEmpty(filterCore)) return true; return token.ToString().StartsWith(filterCore, StringComparison.OrdinalIgnoreCase); } public override string ToString() { return token.ToString(); } public TokenEditToken Token { get { return token; } } } }
TokenEditTest/Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DevExpress.XtraEditors; namespace TokenEditTest { public partial class Form1 : XtraForm { public Form1() { InitializeComponent(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); InitTokenEdit(); } void InitTokenEdit() { tokenEdit.Properties.CustomDropDownControl = new CustomTokenEditDropDownControl(); for(int i = 0; i < 20; i++) { string desc = string.Format("Token{0}", i.ToString()); tokenEdit.Properties.Tokens.Add(new TokenEditToken(desc, desc)); } } void OnTokenEditValidateToken(object sender, TokenEditValidateTokenEventArgs e) { e.Value = e.Description; e.IsValid = true; } void OnTokenEditValueChanged(object sender, EventArgs e) { Text = ((TokenEdit)sender).EditValue.ToString(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.