Use an Autocomplete Combo Box : ComboBox « Components « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Components » ComboBoxScreenshots 
Use an Autocomplete Combo Box
Use an Autocomplete Combo Box


using System;
using System.Windows.Forms;
using System.Drawing;

public class AutoCompleteComboBoxTest : System.Windows.Forms.Form {

   public AutoCompleteComboBoxTest(){
        AutoCompleteComboBox combo = new AutoCompleteComboBox();
        combo.Location = new Point(10,10);
        this.Controls.Add(combo);

        combo.Items.Add("Aaaaaa");
        combo.Items.Add("Bbbbbbbbb");
        combo.Items.Add("Ccccccccccc");

   }
   public static void Main(){
       Application.Run(new AutoCompleteComboBoxTest());
   
}


public class AutoCompleteComboBox : ComboBox {
    private bool controlKey = false;

    protected override void OnKeyPressSystem.Windows.Forms.KeyPressEventArgs e) {
        base.OnKeyPress(e);
        if (e.KeyChar == (int)Keys.Escape) {
            this.SelectedIndex = -1;
            this.Text = "";
            controlKey = true;
        else if (Char.IsControl(e.KeyChar)) {
            controlKey = true;
        else {
            controlKey = false;
        }
    }

    protected override void OnTextChanged(System.EventArgs e) {
        base.OnTextChanged(e);
        if (this.Text != "" && !controlKey) {
            string matchText = this.Text;
            int match = this.FindString(matchText);
            if (match != -1) {
                this.SelectedIndex = match;
                this.SelectionStart = matchText.Length;
                this.SelectionLength = this.Text.Length - this.SelectionStart;
            }
        }
    }
}
           
       
Related examples in the same category
1.Buildin AutoCompleteMode for ComboBoxBuildin AutoCompleteMode for ComboBox
2.Auto Complete ComboBoxAuto Complete ComboBox
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.