检索组合框的项目的索引 [英] retrieve index of the items of a combobox

查看:73
本文介绍了检索组合框的项目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索组合框中包含的项目的索引。



例如



comboBox_dept中填充了数据库中的项目one,two,three





i需要找到他们的索引pro-语法上的。



i试过这个



i want to retrieve Index of the items contained in a combobox.

for eg

comboBox_dept is populated with items from database as "one","two","three"


i need to find their indexes pro-grammatically.

i tried this

int  index = comboBox_dept.FindString(one);
       comboBox_dept.SelectedIndex = index;



它总是返回-1。




it always return -1.

foreach (Object item in comboBox_dept.Items)
               {
                  int index1 = comboBox_dept.Items.IndexOf(item);
               MessageBox.Show(index1.ToString() + item);
               }





它只返回第一项



任何帮助或提示将不胜感激。



it only return 1st item only

any help or hint would be appreciated.

推荐答案

为什么不循环:



for(int i = 0; i< comboBox_dept.Items.Count; ++ i)

{

string s = comboBox_dept.Items [i] .ToString() ;

//现在s是字符串,我是索引

}
Why don't you do a loop:

for (int i = 0; i < comboBox_dept.Items.Count;++i)
{
string s = comboBox_dept.Items[i].ToString();
// Now s is the string and i is the index
}


目前尚不清楚如何添加项目组合框?你在做什么 comboBox_dept.Items.Add(sqldatareader);



添加将单个对象添加到项目列表中,无论对象的类型如何。如果你想添加一个范围,那么使用 AddRange



你的最后一个代码片段似乎表明你的列表只包含一个项目。你是否在运行时看到组合中的多个项目。



如果你正在使用数据绑定,我想这应该从原始源获得索引。
It is not clear how items are added to the combobox? Are you doing something like comboBox_dept.Items.Add(sqldatareader);?

Add will add a single object to the list of items whatever the type of the object. If you want to add a range, then uses AddRange.

Your last code snippet seems to indicate that your list contains only one item. Are you seeing multiple items in the combo at run time.

If you are using databinding, I guess that should get the index from the original source.


试用此代码:



Try this code:

 Public Function GetTextDataGridView(ByVal DataGR As DataGridView, ByVal Rowget As Integer) As String

    GetTextDataGridView = vbNullString
    Try
        Dim i As Integer
        i = DataGR.CurrentRow.Index
        GetTextDataGridView = DataGR.Item(Rowget, i).Value
    Catch ex As Exception

    End Try

End Function




$ _ $ b in Form_Load()





in Form_Load()

Dim item As Object = GetTextDataGridView(frmListMembers.dgvListMember, 3)
Dim Index1 As Integer = cboSex.Items.IndexOf(item)
cboSex.SelectedIndex = Index1


这篇关于检索组合框的项目的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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