locked
Autocomplete on multi-line textbox RRS feed

  • Question

  • Hello,

    I have been working with the new autocomplete features in .NET 2.0.  Unfortunately, when I began to use multi-line textboxes, I discovered that autocomplete no longer works.  I understand the lack of this feature, but I would still like to implment it so  I am looking for feedback on how to make it happen.  Can anyone direct me on how to implement the autocomplete features for the multi-line textbox?  

    I would like to create a user control based on the textbox if this is not immediately possible, but I am not sure how to make the dropdown auto-complete list appear from code.  Does anyone know how to force the auto-complete list to appear from code?  

    I have begun to make this work by implementing code to "auto-append" to the textbox from a data source (string collection, modified to host multi-line strings) but I am lacking a way to show the drop-down box ("auto-suggest") which is so important to auto-complete.  I intend to use the control as a MRU list (string collection is saved to the settings file on the local machine, thus ensuring pass-thru between application instances), but I will most likely also add the ability to do some more high-level databinding (bind to datasources, add displayMember and valueMember, for instance) so as to make this a strong, applicable component for future uses.

    After the user control is created I would be glad to share it with anyone who helps as well as (later) on the web.

    Any help is appreciated.

    Thanks
    Friday, January 6, 2006 3:56 PM

All replies

  • You can do like this....

    namespace

     

    Intellisense

    {

     

    partial class MainForm

    {

     

    /// <summary>

     

    /// Required designer variable.

     

    /// </summary>

     

    private System.ComponentModel.IContainer components = null;

     

    /// <summary>

     

    /// Clean up any resources being used.

     

    /// </summary>

     

    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

     

    protected override void Dispose(bool disposing)

    {

     

    if (disposing && (components != null))

    {

    components.Dispose();

    }

     

    base.Dispose(disposing);

    }

    #region

     

    Windows Form Designer generated code

     

    /// <summary>

     

    /// Required method for Designer support - do not modify

     

    /// the contents of this method with the code editor.

     

    /// </summary>

     

    private void InitializeComponent()

    {

     

    this.rtbInput = new System.Windows.Forms.RichTextBox();

     

    this.lbIntelli = new System.Windows.Forms.ListBox();

     

    this.SuspendLayout();

     

    //

     

    // rtbInput

     

    //

     

    this.rtbInput.Dock = System.Windows.Forms.DockStyle.Fill;

     

    this.rtbInput.Location = new System.Drawing.Point(0, 0);

     

    this.rtbInput.Name = "rtbInput";

     

    this.rtbInput.Size = new System.Drawing.Size(397, 356);

     

    this.rtbInput.TabIndex = 0;

     

    this.rtbInput.Text = "";

     

    this.rtbInput.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.rtbInput_PreviewKeyDown);

     

    //

     

    // lbIntelli

     

    //

     

    this.lbIntelli.FormattingEnabled = true;

     

    this.lbIntelli.Items.AddRange(new object[] {

     

    "!--...--",

     

    "!DOCTYPE ",

     

    "a",

     

    "abbr",

     

    "acronym",

     

    "address",

     

    "applet",

     

    "area",

     

    "b",

     

    "base",

     

    "basefont",

     

    "bdo",

     

    "big",

     

    "blockquote",

     

    "body",

     

    "br",

     

    "button",

     

    "caption",

     

    "center",

     

    "cite",

     

    "code",

     

    "col",

     

    "colgroup",

     

    "dd",

     

    "del",

     

    "dir",

     

    "div",

     

    "dfn",

     

    "dl",

     

    "dt",

     

    "em",

     

    "fieldset",

     

    "font",

     

    "form",

     

    "frame",

     

    "frameset",

     

    "h1 to h6",

     

    "head",

     

    "hr",

     

    "html",

     

    "i",

     

    "iframe",

     

    "img",

     

    "input",

     

    "ins",

     

    "isindex",

     

    "kbd",

     

    "label",

     

    "legend",

     

    "li",

     

    "link",

     

    "map",

     

    "menu",

     

    "meta",

     

    "noframes",

     

    "noscript",

     

    "object",

     

    "ol",

     

    "optgroup",

     

    "option",

     

    "p",

     

    "param",

     

    "pre",

     

    "q",

     

    "s",

     

    "samp",

     

    "script",

     

    "select",

     

    "small",

     

    "span",

     

    "strike",

     

    "strong",

     

    "style",

     

    "sub",

     

    "sup",

     

    "table",

     

    "tbody",

     

    "td",

     

    "textarea",

     

    "tfoot",

     

    "th",

     

    "thead",

     

    "title",

     

    "tr",

     

    "tt",

     

    "u",

     

    "ul",

     

    "var",

     

    "xmp"});

     

    this.lbIntelli.Location = new System.Drawing.Point(124, 92);

     

    this.lbIntelli.Name = "lbIntelli";

     

    this.lbIntelli.Size = new System.Drawing.Size(120, 82);

     

    this.lbIntelli.TabIndex = 1;

     

    this.lbIntelli.Visible = false;

     

    this.lbIntelli.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.lbIntelli_KeyPress);

     

    this.lbIntelli.KeyUp += new System.Windows.Forms.KeyEventHandler(this.lbIntelli_KeyUp);

     

    //

     

    // MainForm

     

    //

     

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

     

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

     

    this.ClientSize = new System.Drawing.Size(397, 356);

     

    this.Controls.Add(this.lbIntelli);

     

    this.Controls.Add(this.rtbInput);

     

    this.Name = "MainForm";

     

    this.Text = "Intellisense Sample | James Gupta 2006";

     

    this.ResumeLayout(false);

    }

    #endregion

     

    private System.Windows.Forms.RichTextBox rtbInput;

     

    private System.Windows.Forms.ListBox lbIntelli;

    }

    }
    ---------------------------

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.ComponentModel;

    using

     

    System.Data;

    using

     

    System.Drawing;

    using

     

    System.Text;

    using

     

    System.Windows.Forms;

    namespace

     

    Intellisense

    {

     

    public partial class MainForm : Form

    {

     

    public int CurrentTagStart = 0;

     

    public string intellisenseBuffer = "";

     

    public bool PassInputToTextbox = true;

     

    public MainForm()

    {

    InitializeComponent();

    }

     

    private void rtbInput_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)

    {

     

    if (e.KeyCode == Keys.OemPeriod && e.Shift == false)

    {

    CurrentTagStart = rtbInput.SelectionStart;

     

    Point p = rtbInput.GetPositionFromCharIndex(rtbInput.SelectionStart);

    p.Y += (

    int)rtbInput.Font.GetHeight()*2;

     

    Console.WriteLine((int)rtbInput.Font.GetHeight());

    lbIntelli.Location = p;

    lbIntelli.Show();

    ActiveControl = lbIntelli;

    }

    }

     

    private void lbIntelli_KeyUp(object sender, KeyEventArgs e)

    {

     

    Object ObjToSelect = new Object();

     

    if (e.KeyCode != Keys.OemPeriod)

     

    if (e.KeyCode != Keys.Back)

    {

     

    bool startswith = false;

    intellisenseBuffer += e.KeyCode;

     

    foreach (object obj in lbIntelli.Items)

    {

     

    string str = obj.ToString();

     

    if (str != "")

    {

    startswith = str.StartsWith(intellisenseBuffer,

    true, null);

     

    if (startswith == true)

    {

    ObjToSelect = obj;

     

    break;

    }

    }

    }

     

    if (startswith == false)

    {

    intellisenseBuffer =

    "";

    lbIntelli.Hide();

    }

    lbIntelli.SelectedItem = ObjToSelect;

    }

     

    if (e.KeyCode == Keys.Back) { rtbInput.Text.Remove(rtbInput.SelectionStart-1,1); }

     

    if (e.KeyCode == Keys.Return)

    {

    rtbInput.Select(CurrentTagStart+1, rtbInput.SelectionStart);

    rtbInput.SelectedText = lbIntelli.SelectedItem.ToString();

    }

    }

     

    private void lbIntelli_KeyPress(object sender, KeyPressEventArgs e)

    {

    rtbInput.SelectedText = e.KeyChar.ToString();

    }

    }

    }

    Tuesday, June 9, 2009 4:34 AM