Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example
the collection has an item "Student"
if the user start typing "S", the combobox text should look for that character and start displaying/ suggesting the "Student" text. if character dont match it stays blank

What I have tried:

private void cmbType_TextChanged(object sender, EventArgs e)
{
if (cmbType.Text.Contains(cmbType.Text.ToUpper()))
{
var items = this.cmbType.GetItemText(this.cmbType.SelectedItem);
//var type = cmbType.Items.ToString().Where(c =>
cmbType.Text.ToUpper().Contains(cmbType.Text.ToUpper()));
cmbType.Text = items;
}
}
Posted
Updated 10-Sep-18 8:19am
Comments
littleGreenDude 10-Sep-18 13:50pm    
What combobox is this? Standard Microsoft, Telerik, other?

Look for AutoComplete or AutoCompleteMode property
Member 13975729 10-Sep-18 13:53pm    
Windows.Forms.Combobox
it's a .NET app

try this:
class DummyComboBoxItem
{
public string DisplayName
{
get
{
return "Make a selection ...";
}
}
}
public partial class mainForm : Form
{
private DummyComboBoxItem placeholder = new DummyComboBoxItem();
public mainForm()
{
InitializeComponent();

myComboBox.DisplayMember = "DisplayName";
myComboBox.Items.Add(placeholder);
foreach(object o in Objects)
{
myComboBox.Items.Add(o);
}
myComboBox.SelectedItem = placeholder;
}

private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (myComboBox.SelectedItem == null) return;
if (myComboBox.SelectedItem == placeholder) return;
/*
do your stuff
*/
myComboBox.Items.Add(placeholder);
myComboBox.SelectedItem = placeholder;
}

private void myComboBox_DropDown(object sender, EventArgs e)
{
myComboBox.Items.Remove(placeholder);
}

private void myComboBox_Leave(object sender, EventArgs e)
{
//this covers user aborting the selection (by clicking away or choosing the system null drop down option)
//The control may not immedietly change, but if the user clicks anywhere else it will reset
if(myComboBox.SelectedItem != placeholder)
{
if(!myComboBox.Items.Contains(placeholder)) myComboBox.Items.Add(placeholder);
myComboBox.SelectedItem = placeholder;
}
}
}
 
Share this answer
 
Comments
Member 13975729 10-Sep-18 14:45pm    
I did not really understand what this code is doing.
During form load...

// set up source for combo items
string[] items = { "item1", "Student", "Teacher", "Professor", "Teaching Assistent" };

// configure autocompletion for the combobox
AutoCompleteStringCollection allowedTypes = new AutoCompleteStringCollection();
allowedTypes.AddRange(items);
comboBox1.AutoCompleteCustomSource = allowedTypes;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
 
Share this answer
 
Comments
Member 13975729 10-Sep-18 14:44pm    
This example won't let me enter more than on character in the combo and wont let display suggestions.
it also throws an exception in case i type a letter and then hit backspace.
littleGreenDude 10-Sep-18 14:54pm    
Works for me...

I created a Windows form app and draggeda standard combobox control on to the form. In design, clicked on the form. Selected "Events" in the Properties window. Scrolled down to "Load" in the list and added the "Form1_Load" event. Full form code looks like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
// set up source for combo items
string[] items = { "item1", "Student", "Teacher", "Professor", "Teaching Assistent" };

// configure autocompletion for the combobox
AutoCompleteStringCollection allowedTypes = new AutoCompleteStringCollection();
allowedTypes.AddRange(items);
comboBox1.AutoCompleteCustomSource = allowedTypes;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900