AutoComplete ComboBox : ComboBox « GUI Windows Forms « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » GUI Windows Forms » ComboBox 
22. 10. 8. AutoComplete ComboBox
AutoComplete ComboBox
/*
Revised from cdoe 

Visual C# 2005 Recipes A Problem-Solution Approach

# By Allen Jones
Matthew MacDonald
Rakesh Rajan
# ISBN: 1590595890
# ISBN-13: 9781590595893
# 592 pp.
# Published: Jan 2006
*/

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

public partial class ComboBoxHolder : Form
{
    public ComboBoxHolder()
    {
        this.SuspendLayout();

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292266);
        this.Name = "ComboBoxHolder";
        this.Text = "ComboBoxHolder";
        this.ResumeLayout(false);


    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        AutoCompleteComboBox combo = new AutoCompleteComboBox();
        combo.Location = new Point(1010);
        this.Controls.Add(combo);

        combo.Items.Add("word");
        combo.Items.Add("world");
        combo.Items.Add("wording");
        combo.Items.Add("worse");
        
    }

    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new ComboBoxHolder());
    }
}

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

    protected override void OnKeyPress(System.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;
            }
        }
    }
}
22. 10. ComboBox
22. 10. 1. Add Items to ComboBoxAdd Items to ComboBox
22. 10. 2. Get Items in a ComboBoxGet Items in a ComboBox
22. 10. 3. ComboBox selection changed eventComboBox selection changed event
22. 10. 4. ComboBox focus leave eventComboBox focus leave event
22. 10. 5. ComboBox text changedComboBox text changed
22. 10. 6. Find item in a ComboBoxFind item in a ComboBox
22. 10. 7. OwnerDraw ComboBox
22. 10. 8. AutoComplete ComboBoxAutoComplete ComboBox
w_ww__._j_ava___2__s__.__com | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.