Combobox.Findstringexact()返回-1作为索引 [英] Combobox.Findstringexact() returning -1 as index

查看:196
本文介绍了Combobox.Findstringexact()返回-1作为索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在我的代码中,即使组合框具有该元素,combobox.findstringexact也会返回-1。请告诉我代码中的问题。这个愚蠢的问题正在浪费我宝贵的时间。我知道我在这里遗漏了一些东西。谢谢

Hello guys,

in my code combobox.findstringexact is returning -1 even when the combobox has that element. Please let me know whats the problem in my code. This silly problem is wasting my precious time. I know i am missing something here. Thanks

comboBoxLanguage.DataSource = Enum.GetValues(typeof(ATXDataManager.Constants.Language));
comboBoxLanguage.SelectedIndex = comboBoxLanguage.FindStringExact(objAdminData.LANGUAGE.Value.ToString());

推荐答案

如果你看在 FindStrignExact [ ^ ]当字符串返回-1时找不到 - 所以这可能是预期的行为。



在这种情况下,问题可能是组合框尚未加载:你正在设置DataSource在尝试搜索字符串之前立即进行,因此很可能是ComboBox没有加载数据:它不会立即执行,而是将负载推迟到需要时。



为什么要直接访问Enum.GetValues返回的集合来搜索组合框集合?
If you look at the definition of FindStrignExact[^] it returns -1 when the string is not found - so this is probably expected behavior.

In this case, the problem is likely to be that the combo box has not loaded yet: you are setting the DataSource immediate before trying to search for the string so the likelyhood is that the ComboBox has not loaded the data: it doesn't do it immediately, but defers the load until it is needed.

Why search the combobox collection at all, when you could just access the collection returned by the Enum.GetValues directly instead?


这篇关于Combobox.Findstringexact()返回-1作为索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆