我有一个字符串"测试1"和我的组合框包含test1
,test2
和test3
.如何将所选项目设置为"test1"?也就是说,如何将我的字符串与其中一个comboBox项匹配?
我在考虑下面这一行,但这不起作用.
comboBox1.SelectedText = "test1";
Run Code Online (Sandbox Code Playgroud)
nor*_*rtB 262
这应该做的伎俩:
Combox1.SelectedIndex = Combox1.FindStringExact("test1")
Run Code Online (Sandbox Code Playgroud)
And*_*nan 203
你试过Text属性吗?这个对我有用.
ComboBox1.Text = "test1";
Run Code Online (Sandbox Code Playgroud)
SelectedText属性用于组合框的文本框部分中可编辑文本的选定部分.
Spe*_*nce 48
假设你的组合框不是数据绑定的,你需要在表单的"items"集合中找到对象的索引,然后将"selectedindex"属性设置为适当的索引.
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
Run Code Online (Sandbox Code Playgroud)
请记住,如果找不到该项,IndexOf函数可能会引发争论.
Fre*_*els 37
如果ComboBox中的项目是字符串,您可以尝试:
comboBox1.SelectedItem = "test1";
Run Code Online (Sandbox Code Playgroud)
小智 10
对我来说这只有效:
foreach (ComboBoxItem cbi in someComboBox.Items)
{
if (cbi.Content as String == "sometextIntheComboBox")
{
someComboBox.SelectedItem = cbi;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
MOD:如果你有自己的对象作为组合框中设置的项目,那么用其中一个替换ComboBoxItem,如:
foreach (Debitor d in debitorCombo.Items)
{
if (d.Name == "Chuck Norris")
{
debitorCombo.SelectedItem = d;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
SelectedText是获取或设置字符串编辑所选项目的实际文本在下拉列表作为记录在这里.如果你设置:这是不可编辑的:
comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
Run Code Online (Sandbox Code Playgroud)
使用:
comboBox1.SelectedItem = "test1";
Run Code Online (Sandbox Code Playgroud)
要么:
comboBox1.SelectedIndex = comboBox1.Items.IndexOf("test1");
Run Code Online (Sandbox Code Playgroud)
我使用了一种扩展方法:
public static void SelectItemByValue(this ComboBox cbo, string value)
{
for(int i=0; i < cbo.Items.Count; i++)
{
var prop = cbo.Items[i].GetType().GetProperty(cbo.ValueMember);
if (prop!=null && prop.GetValue(cbo.Items[i], null).ToString() == value)
{
cbo.SelectedIndex = i;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后只需使用方法:
ddl.SelectItemByValue(value);
Run Code Online (Sandbox Code Playgroud)
ComboBox1.SelectedIndex= ComboBox1.FindString("Matching String");
Run Code Online (Sandbox Code Playgroud)
在Windows窗体中尝试此操作。
小智 5
假设 test1、test2、test3 属于comboBox1集合,以下语句将起作用。
comboBox1.SelectedIndex = 0;
Run Code Online (Sandbox Code Playgroud)
小智 5
我已经用从数据库填充的 een DataTable 填充了 ComboBox。然后我设置了 DisplayMember 和 ValueMember。我使用此代码来设置所选项目。
foreach (DataRowView Row in ComboBox1.Items)
{
if (Row["ColumnName"].ToString() == "Value") ComboBox1.SelectedItem = Row;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这个解决方案是基于MSDN我做了一些修改。
它找到字符串的确切部分或部分并设置它。
private int lastMatch = 0;
private void textBoxSearch_TextChanged(object sender, EventArgs e)
{
// Set our intial index variable to -1.
int x = 0;
string match = textBoxSearch.Text;
// If the search string is empty set to begining of textBox
if (textBoxSearch.Text.Length != 0)
{
bool found = true;
while (found)
{
if (comboBoxSelect.Items.Count == x)
{
comboBoxSelect.SelectedIndex = lastMatch;
found = false;
}
else
{
comboBoxSelect.SelectedIndex = x;
match = comboBoxSelect.SelectedValue.ToString();
if (match.Contains(textBoxSearch.Text))
{
lastMatch = x;
found = false;
}
x++;
}
}
}
else
comboBoxSelect.SelectedIndex = 0;
}
Run Code Online (Sandbox Code Playgroud)我希望我有所帮助!
归档时间: |
|
查看次数: |
601396 次 |
最近记录: |