检索组合框的项目的索引

编程入门 行业动态 更新时间:2024-10-28 20:19:33
本文介绍了检索组合框的项目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想检索组合框中包含的项目的索引。 例如 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

更多推荐

检索组合框的项目的索引

本文发布于:2023-11-05 15:29:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   索引   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!