如果用户开始输入组合框集合中包含的字符,如何让组合框显示文本? C# [英] How do I get combobox to display the text if the user starts typing characters contained on the combobox collection? C#
问题描述
例如
该集合有一个项目学生
如果用户开始输入S,组合框文本应查找该字符并开始显示/提出学生文本。如果字符不匹配则保持空白
我尝试过:
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()。包含(cmbType.Text.ToUpper()));
cmbType.Text = items;
}
}
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;
}
}
推荐答案
试试这个:
class DummyComboBoxItem
{
公共字符串显示名称
{
get
{
返回做出选择......;
}
}
}
公共部分类mainForm:表格
{
私人DummyComboBoxItem placeholde r = new DummyComboBoxItem();
public mainForm()
{
InitializeComponent();
myComboBox.DisplayMember =DisplayName;
myComboBox.Items.Add(占位符);
foreach(对象中的对象o)
{
myComboBox .Items.Add(o);
}
myComboBox.SelectedItem =占位符;
}
private void myComboBox_SelectedIndexChanged(object sender,EventArgs e)
{
if(myComboBox.SelectedItem == null)return;
if(myComboBox.SelectedItem == placeholder)返回;
/ *
做你的东西
* /
myComboBox.Items.Add(占位符);
myComboBox.SelectedItem =占位符;
}
private void myComboBox_DropDown(object sender,EventArgs e)
{
myComboBox.Items.Remove(占位符);
}
private void myComboBox_Leave(object sender,EventArgs e)
{
//这包括用户中止选择(通过点击或选择系统空下拉选项)
//控件可能无法立即更改,但如果用户点击其他任何地方则会重置
if(myComboBox.SelectedItem!= placeholder)
{
if(!myComboBox.Items.Contains(占位符))myComboBox.Items.Add(占位符);
myComboBox.SelectedItem =占位符;
}
}
}
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;
}
}
}
表格加载期间......
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;
这篇关于如果用户开始输入组合框集合中包含的字符,如何让组合框显示文本? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!