.net 只读Windows窗体组合框
有没有办法使windows窗体组合框成为只读的? 具体来说:用户应该能够键入,但只允许框中的那些值(使用自动完成或从列表中选择) 还是使用验证事件的唯一方法 问候.net 只读Windows窗体组合框,.net,winforms,combobox,.net,Winforms,Combobox,有没有办法使windows窗体组合框成为只读的? 具体来说:用户应该能够键入,但只允许框中的那些值(使用自动完成或从列表中选择) 还是使用验证事件的唯一方法 问候 Mario您可以将DropDownStyle设置为DropDownList,但这实际上不允许键入(但允许使用键盘进行选择) 如果您确实希望用户能够键入/查看不完整的单词,则必须使用事件。验证事件将是最佳选择 如果在用户键入内容时设置了AutoCompleteMode=SuggestAppend和AutoCompleteSource=L
Mario您可以将DropDownStyle设置为DropDownList,但这实际上不允许键入(但允许使用键盘进行选择)
如果您确实希望用户能够键入/查看不完整的单词,则必须使用事件。验证事件将是最佳选择 如果在用户键入内容时设置了
AutoCompleteMode=SuggestAppend
和AutoCompleteSource=ListItems
,则组合框会自动显示以键入字符开头的条目
然后,通过处理SelectedIndexChanged
或SelectedValueChanged
事件,您将能够截获用户在值列表中键入的值
如果您也绝对不希望用户键入列表中没有的内容,那么是的,您必须处理例如KeyDown
事件,如:
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
char ch = (char)e.KeyValue;
if (!char.IsControl(ch))
{
string newTxt = this.comboBox1.Text + ch;
bool found = false;
foreach (var item in this.comboBox1.Items)
{
string itemString = item.ToString();
if (itemString.StartsWith(newTxt, StringComparison.CurrentCultureIgnoreCase))
{
found = true;
break;
}
}
if (!found)
e.SuppressKeyPress = true;
}
}
谢谢。除了KeyDown事件代码外,上述方法对我有效。因为组合框附加到数据表。如果combobox附加到DataTable,并且您绝对不希望用户键入列表中没有的内容,请尝试下面的代码
private void cmbCountry_KeyDown(object sender, KeyEventArgs e)
{
char ch = (char)e.KeyValue;
if (!char.IsControl(ch))
{
string newTxt = this.cmbCountry.Text + ch;
bool found = false;
foreach (var item in cmbCountry.Items)
{
DataRowView row = item as DataRowView;
if (row != null)
{
string itemString = row.Row.ItemArray[0].ToString();
if (itemString.StartsWith(newTxt, StringComparison.CurrentCultureIgnoreCase))
{
found = true;
break;
}
}
else
e.SuppressKeyPress = true;
}
if (!found)
e.SuppressKeyPress = true;
}
}
谢谢,我怕我不得不这么做!谢谢我只是想不用自己编码就可以了。。。猜测我的集合的另一个库组件。。。