Example E3040
Visible to All Users

WinForms Lookup - Display custom tooltips

This example demonstrates how to display custom tooltips for drop-down items in the WinForms LookUpEdit control.

C#
void lookUpEditHints1_BeforeShowingTooltip(object sender, EventArgs e) { ToolTipControllerShowEventArgs ee = e as ToolTipControllerShowEventArgs; ee.ToolTip += " + custom tool tip can be added"; }

Files to Review

Documentation

Does this example address your development requirements/objectives?

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

Example Code

LookUpEditWithHints/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.Utils; using DevExpress.XtraEditors.Controls; namespace LookUpEditWithHints { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { lookUpEditHints1.Properties.DataSource = FillTable(); lookUpEditHints1.Properties.DisplayMember = "Customer"; lookUpEditHints1.Properties.DescriptionField = "Description"; lookUpEditHints1.BeforeShowingTooltip += new EventHandler(lookUpEditHints1_BeforeShowingTooltip); // LookUpEdit customization: adding two columns from DataSource // and making one of them invisible LookUpColumnInfoCollection coll = lookUpEditHints1.Properties.Columns; coll.Add(new LookUpColumnInfo("Customer")); coll.Add(new LookUpColumnInfo("Description")); lookUpEditHints1.Properties.Columns["Description"].Visible = false; } void lookUpEditHints1_BeforeShowingTooltip(object sender, EventArgs e) { ToolTipControllerShowEventArgs ee = e as ToolTipControllerShowEventArgs; ee.ToolTip += " + custom tool tip also can be added"; } DataTable FillTable() { DataTable _customersTable = new DataTable(); DataColumn col; DataRow row; _customersTable.TableName = "Customers"; col = new DataColumn(); col.ColumnName = "Customer"; col.DataType = System.Type.GetType("System.String"); _customersTable.Columns.Add(col); col = new DataColumn(); col.ColumnName = "Description"; col.DataType = System.Type.GetType("System.String"); _customersTable.Columns.Add(col); row = _customersTable.NewRow(); row["Customer"] = "John"; row["Description"] = "A description for customer John"; _customersTable.Rows.Add(row); row = _customersTable.NewRow(); row["Customer"] = "Jane"; row["Description"] = "A description for customer Jane"; _customersTable.Rows.Add(row); row = _customersTable.NewRow(); row["Customer"] = "Jack"; row["Description"] = "A description for customer Jack"; _customersTable.Rows.Add(row); return _customersTable; } } }
LookUpEditWithHints/LookUpEditHints.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraEditors; using System.ComponentModel; using DevExpress.XtraEditors.Popup; namespace LookUpEditWithHints { class LookUpEditHints : LookUpEdit { static LookUpEditHints() { RepositoryItemLookUpEditHints.RegisterLookUpEditHints(); } public LookUpEditHints() : base() { } public override string EditorTypeName { get { return RepositoryItemLookUpEditHints.LookUpEditHintsName; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public new RepositoryItemLookUpEditHints Properties { get { return base.Properties as RepositoryItemLookUpEditHints; } } protected override DevExpress.XtraEditors.Popup.PopupBaseForm CreatePopupForm() { return new PopupLookUpEditHintsForm(this); } public string DescriptionField { set { Properties.DescriptionField = value; OnPropertiesChanged(); } get { return Properties.DescriptionField; } } public event EventHandler BeforeShowingTooltip { add { Properties.BeforeShowingTooltip += value; } remove { Properties.BeforeShowingTooltip -= value; } } } }
LookUpEditWithHints/PopupLookUpEditHintsForm.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using DevExpress.XtraEditors.Popup; using System.Collections; using System.Windows.Forms; using DevExpress.XtraEditors.Controls; using DevExpress.Utils; using System.Drawing; namespace LookUpEditWithHints { class PopupLookUpEditHintsForm : PopupLookUpEditForm { Point prevPoint = Point.Empty; ToolTipController tt; DataTable dt; int prevRowIndex = -1; public PopupLookUpEditHintsForm(LookUpEditHints ownerEdit) : base(ownerEdit) { if (this.OwnerEdit.ToolTipController == null) tt = ToolTipController.DefaultController; tt.BeforeShow += new ToolTipControllerBeforeShowEventHandler(tt_BeforeShow); dt = this.OwnerEdit.Properties.DataSource as DataTable; } void tt_BeforeShow(object sender, ToolTipControllerShowEventArgs e) { LookUpEditHints le = OwnerEdit as LookUpEditHints; le.Properties.OnBeforeShowingTooltip(e); } public override void ShowPopupForm() { SetToolTipController(); SetDataTable(); base.ShowPopupForm(); } void SetToolTipController() { if (this.OwnerEdit.ToolTipController != null && this.OwnerEdit.ToolTipController != tt) { tt = this.OwnerEdit.ToolTipController; tt.BeforeShow += new ToolTipControllerBeforeShowEventHandler(tt_BeforeShow); } } void SetDataTable() { dt = dt != this.OwnerEdit.Properties.DataSource as DataTable ? this.OwnerEdit.Properties.DataSource as DataTable : dt; } protected override void CheckMouseCursor(LookUpPopupHitTest ht) { if (prevPoint.X != ht.Point.X || prevPoint.Y != ht.Point.Y) { prevPoint = ht.Point; LookUpEditHints le = this.OwnerEdit as LookUpEditHints; LookUpColumnInfo column; int columnPos = -1; for (int i = 0; i < this.OwnerEdit.Properties.Columns.Count; i++) { column = this.OwnerEdit.Properties.Columns[i]; if (column.FieldName == le.Properties.DescriptionField) { columnPos = i; break; } } if (columnPos != -1) { if (ht.HitType == LookUpPopupHitType.Row) { if (ht.Index != prevRowIndex) { tt.HideHint(); prevRowIndex = ht.Index; } tt.ShowHint(dt.Rows[ht.Index][columnPos].ToString()); } } } base.CheckMouseCursor(ht); } } }
LookUpEditWithHints/RepositoryItemLookUpEditHints.cs(vb)
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraEditors.Repository; using DevExpress.XtraEditors.Registrator; using DevExpress.XtraEditors.Drawing; using DevExpress.XtraEditors.Popup; using DevExpress.XtraEditors.ViewInfo; namespace LookUpEditWithHints { [UserRepositoryItem("RegisterLookUpEditHints")] class RepositoryItemLookUpEditHints : RepositoryItemLookUpEdit { string descriptionField = ""; static readonly object _beforeShowingTooltip = new object(); static RepositoryItemLookUpEditHints() { RegisterLookUpEditHints(); } public RepositoryItemLookUpEditHints() : base() { } // Unique name for custom control public const string LookUpEditHintsName = "LookUpEditHints"; public override string EditorTypeName { get { return LookUpEditHintsName; } } //Register the editor public static void RegisterLookUpEditHints() { EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo( LookUpEditHintsName, typeof(LookUpEditHints), typeof(RepositoryItemLookUpEditHints), typeof(LookUpEditViewInfo), new ButtonEditPainter(), true)); } public string DescriptionField { set { descriptionField = value; OnPropertiesChanged(); } get { return descriptionField; } } public event EventHandler BeforeShowingTooltip { add { Events.AddHandler(RepositoryItemLookUpEditHints._beforeShowingTooltip, value); } remove { Events.RemoveHandler(RepositoryItemLookUpEditHints._beforeShowingTooltip, value); } } protected internal virtual void OnBeforeShowingTooltip(EventArgs e) { EventHandler handler = (EventHandler)Events[RepositoryItemLookUpEditHints._beforeShowingTooltip]; if (handler != null) handler(GetEventSender(), e); } public override void Assign(RepositoryItem item) { BeginUpdate(); base.Assign(item); RepositoryItemLookUpEditHints source = item as RepositoryItemLookUpEditHints; if (source == null) return; this.DescriptionField = source.DescriptionField; EndUpdate(); Events.AddHandler(RepositoryItemLookUpEditHints._beforeShowingTooltip, source.Events[RepositoryItemLookUpEditHints._beforeShowingTooltip]); } } }

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.