In Q2 2012 we introduced a cool brand new control in Telerik’s WinForms suite – RadAutoCompleteBox. This control allows end-users to easily fill-in text thanks to autocomplete functionality and tokens of text coming “out of the box”. This behavior enables you to easily simulate the Outlook and Facebook autocomplete functionality available to users each time they send a new message and pick the recipients in the ‘To:’ field of their outgoing message from a convenient pop-up list with suggestions.
As you can see above, Lee Cooper is marked as offline, Adam Peter is marked as busy, Joe Smith is away, Daniel Finger is online, there is one e-mail address unknown to our organization, and we are about to enter a suggested name starting with “R”. So let’s see how this is done.
RadListDataItemCollection items = this.radAutoCompleteBox1.AutoCompleteItems;items.Add(new RadListDataItem("Joe Smith", "joe@newcompany.com"));items.Add(new RadListDataItem("Adam Petersen", "adam@qwerty.com"));BindingList<Person> persons = new BindingList<Person>();persons.Add(new Person(0, "Joe Smith", "joe@fakecompany.com", Status.Away));persons.Add(new Person(6, "Lee Cooper", "lee.cooper@coopercoorp.com", Status.Offline));persons.Add(new Person(1, "Adam Petersen", "adam@qwerty.com", Status.Busy));persons.Add(new Person(3, "Daniel Finger", "daniel.pinger@gmail.com", Status.Online));// Persons skipped for brevitythis.radAutoCompleteBox1.AutoCompleteDataSource = persons;this.radAutoCompleteBox1.AutoCompleteValueMember = "Email";this.radAutoCompleteBox1.AutoCompleteDisplayMember = "FullName";
void radAutoCompleteBox1_TokenValidating(object sender, TokenValidatingEventArgs e){ // sample mail validation for brevity bool isEmailAddress = e.Text.Contains('@'); bool isKnownPerson = persons.Where(x => x.FullName.Equals(e.Text)).FirstOrDefault() != null; e.IsValidToken = isEmailAddress || isKnownPerson;}
public class TokenizedBlockWithStatusBullet : TokenizedTextBlockElement{ private LightVisualElement statusBullet; protected override Type ThemeEffectiveType { get { return typeof(TokenizedTextBlockElement); } } protected override void CreateChildElements() { base.CreateChildElements(); this.statusBullet = new LightVisualElement(); this.statusBullet.StretchVertically = true; this.statusBullet.StretchHorizontally = false; this.statusBullet.Margin = new System.Windows.Forms.Padding(5, 0, 0, 0); this.Children.Insert(0, this.statusBullet); this.statusBullet.Image = RadAutoCompleteBoxWF.Properties.Resources.status_offline; } public Image StatusImage { get { return statusBullet.Image; } set { statusBullet.Image = value; } }}
void radAutoCompleteBox1_TextBlockFormatting(object sender, TextBlockFormattingEventArgs e){ TokenizedBlockWithStatusBullet token = e.TextBlock as TokenizedBlockWithStatusBullet; Person p = persons.Where(x => x.FullName.Contains(e.TextBlock.Text)).FirstOrDefault(); if (p != null) { switch (p.Status) { case Status.Offline: token.BackColor = Color.LightGray; token.BorderColor = Color.DarkGray; token.StatusImage = RadAutoCompleteBoxWF.Properties.Resources.status_offline; break; case Status.Away: token.BackColor = Color.Yellow; token.BorderColor = Color.Orange; token.StatusImage = RadAutoCompleteBoxWF.Properties.Resources.status_away; break;
// Status.Busy and Status.Online skipped for brevity
} }}Check our Getting Started with RadAutoCompleteBox for WinForms video that guides you through this scenario in a friendly step-by-step manner.
Nikolay Diyanov Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.
Find him on Twitter @n_diyanov or on LinkedIn.