Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Autocomplete Menu

, 1 Oct 2012 LGPL3
Customizable autocomplete menu for RichTextBox, TextBox and other controls
AutocompleteMenu-noexe.zip
AutocompleteMenu
AutocompleteMenu.suo
AutocompleteMenu
obj
Debug
AutocompleteMenuNS.AutocompleteMenu.resources
Properties
Tester
bin
Debug
Tester.vshost.exe.manifest
obj
x86
Debug
Tester.AdvancedSample.resources
Tester.ComboboxSample.resources
Tester.CustomItemSample.resources
Tester.CustomListViewSample.resources
Tester.DataGridViewSample.resources
Tester.DynamicMenuSample.resources
Tester.DynamicTooltipSample.resources
Tester.ExtraLargeSample.resources
Tester.MainForm.resources
Tester.MultiColumnSample.resources
Tester.Properties.Resources.resources
Tester.Sandbox.resources
Tester.SimplestSample.resources
Properties
Settings.settings
AutocompleteMenu.zip
AutocompleteMenu.suo
bin
Debug
AutocompleteMenu.dll
AutocompleteMenu.dll
AutocompleteMenuNS.AutocompleteMenu.resources
AutocompleteMenu.dll
Tester.exe
Tester.vshost.exe
Tester.vshost.exe.manifest
Tester.AdvancedSample.resources
Tester.ComboboxSample.resources
Tester.CustomItemSample.resources
Tester.CustomListViewSample.resources
Tester.DataGridViewSample.resources
Tester.DynamicMenuSample.resources
Tester.DynamicTooltipSample.resources
Tester.exe
Tester.ExtraLargeSample.resources
Tester.MainForm.resources
Tester.MultiColumnSample.resources
Tester.Properties.Resources.resources
Tester.Sandbox.resources
Tester.SimplestSample.resources
Settings.settings
AutocompleteMenuDemo.zip
AutocompleteMenuDemo
AutocompleteMenu.dll
Tester.exe
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace AutocompleteMenuNS
{
    /// <summary>
    /// Item of autocomplete menu
    /// </summary>
    public class AutocompleteItem
    {
        public object Tag;
        string toolTipTitle;
        string toolTipText;
        string menuText;

        /// <summary>
        /// Parent AutocompleteMenu
        /// </summary>
        public AutocompleteMenu Parent { get; internal set; }

        /// <summary>
        /// Text for inserting into textbox
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// Image index for this item
        /// </summary>
        public int ImageIndex{get; set; }

        /// <summary>
        /// Title for tooltip.
        /// </summary>
        /// <remarks>Return null for disable tooltip for this item</remarks>
        public virtual string ToolTipTitle
        {
            get { return toolTipTitle; }
            set { toolTipTitle = value; }
        }

        /// <summary>
        /// Tooltip text.
        /// </summary>
        /// <remarks>For display tooltip text, ToolTipTitle must be not null</remarks>
        public virtual string ToolTipText
        {
            get { return toolTipText; }
            set { toolTipText = value; }
        }

        /// <summary>
        /// Menu text. This text is displayed in the drop-down menu.
        /// </summary>
        public virtual string MenuText
        {
            get { return menuText; }
            set { menuText = value; }
        }


        public AutocompleteItem()
        {
            ImageIndex = -1;
        }

        public AutocompleteItem(string text):this()
        {
            Text = text;
        }

        public AutocompleteItem(string text, int imageIndex)
            : this(text)
        {
            this.ImageIndex = imageIndex;
        }

        public AutocompleteItem(string text, int imageIndex, string menuText)
            : this(text, imageIndex)
        {
            this.menuText = menuText;
        }

        public AutocompleteItem(string text, int imageIndex, string menuText, string toolTipTitle, string toolTipText)
            : this(text, imageIndex, menuText)
        {
            this.toolTipTitle = toolTipTitle;
            this.toolTipText = toolTipText;
        }

        /// <summary>
        /// Returns text for inserting into Textbox
        /// </summary>
        public virtual string GetTextForReplace()
        {
            return Text;
        }

        /// <summary>
        /// Compares fragment text with this item
        /// </summary>
        public virtual CompareResult Compare(string fragmentText)
        {
            if (Text.StartsWith(fragmentText, StringComparison.InvariantCultureIgnoreCase) &&
                   Text != fragmentText)
                return CompareResult.VisibleAndSelected;

            return CompareResult.Hidden;
        }

        /// <summary>
        /// Returns text for display into popup menu
        /// </summary>
        public override string ToString()
        {
            return menuText ?? Text;
        }

        /// <summary>
        /// This method is called after item was inserted into text
        /// </summary>
        public virtual void OnSelected(SelectedEventArgs e)
        {
        }

        public virtual void OnPaint(PaintItemEventArgs e)
        {
            e.Graphics.DrawString(ToString(), e.Font, Brushes.Black, e.TextRect, e.StringFormat);
        }
    }

    public enum CompareResult
    {
        /// <summary>
        /// Item do not appears
        /// </summary>
        Hidden,
        /// <summary>
        /// Item appears
        /// </summary>
        Visible,
        /// <summary>
        /// Item appears and will selected
        /// </summary>
        VisibleAndSelected
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

Pavel Torgashov
Freelancer
Ukraine Ukraine
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.
 
My contact email is p_torgashov[at]ukr.net

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 1 Oct 2012
Article Copyright 2012 by Pavel Torgashov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid