Esatto, se vuoi qualcosa di più complesso devi fartelo a mano
Ma a me è capitato di fare qualcosa di analogo, se vuoi ti posto il codice del controllo:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Linq;
namespace Win32.Controlli
{
public partial class AutocompleteTextBox : UserControl
{
public AutocompleteTextBox()
{
InitializeComponent();
this.BringToFront();
listBox.Visible = false;
}
public IEnumerable<OggettoTest> Elementi { get; set; }
private void AutocompleteTextBox_Leave(object sender, EventArgs e)
{
if(!listBox.Focused)
listBox.Visible = false;
}
private void txt_KeyUp(object sender, KeyEventArgs e)
{
//--- se premi in basso ti sposto il fuoco
if (e.KeyCode == Keys.Down)
{
listBox.Focus();
return;
}
string testo = txt.Text;
if (!this.Parent.Controls.Contains(listBox))
this.Parent.Controls.Add(listBox);
listBox.ValueMember = "testo";
listBox.Location = this.Location;
listBox.Top += this.Height;
listBox.BringToFront();
listBox.Width = this.Width;
listBox.Height = 100;
listBox.DataSource = Elementi.Where(x => x.Testo.Contains(testo)).ToArray();
listBox.Visible = true;
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (!listBox.Focused)
return;
OggettoTest o = listBox.SelectedItem as OggettoTest;
if (o != null)
txt.Text = o.Testo;
}
private void listBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
listBox.Visible = false;
}
private void listBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
listBox.Visible = false;
}
}
}
Come puoi notare, vai dal controllo stesso, ad aggiungere la listBox nel suo contenitore (Parent) in modo che vada sopra agli altri controlli della form.
Dopodichè ti basta solo settare la sorgente dati da fuori (proprietà "Elementi" dell'esempio) e chiaramente modificare la query di ricerca etc...insomma con qualche ritocco dovresti riuscire.
Ciao.
Matteo Raumer
[MCAD .net, MVP Visual C#]
http://blogs.dotnethell.it/freeteo