帮助从CHECKEDBOXLIST代码中检索值 [英] Helping for retrieving values from CHECKEDBOXLIST code

查看:101
本文介绍了帮助从CHECKEDBOXLIST代码中检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是用于在CHECKEDBOXLIST中存储值索引的代码:

This's code for storing the indexes of values in CHECKEDBOXLIST :

Dim checkedItemsList As Integer() = New Integer(CheckedListBox1.CheckedIndices.Count - 1) {}
        Dim sb As New StringBuilder()
        For i As Integer = 0 To CheckedListBox1.CheckedIndices.Count - 1
            checkedItemsList(i) = CheckedListBox1.CheckedIndices(i)
            sb.Append(checkedItemsList(i))
            sb.Append(";")
        Next
        CheckedListBox1.Tag = sb


        Dim cmd As New SqlCommand("Insert into tb values('" & sb.ToString() & "')", connDB)
        Dim iCount As Integer = cmd.ExecuteNonQuery()
        connDB.Close()





它将CHECKEDBOXLIST的值保存在一列中:



it's save the values of CHECKEDBOXLIST in one column as:

0;1;2;3;





检索代码的问题无法正常运行:





the problem in retrieving code doesn't run correctly :

Dim intersts As String = ""
       Dim cmd As New SqlCommand("Select Interests from tb", connDB)

       Dim dr As SqlDataReader
       dr = cmd.ExecuteReader()
       If dr.Read() Then
           intersts = dr("Interests").ToString()
       End If
       connDB.Close()



       For Each i As ListItem In CheckedListBox1.Items
           For Each s As String In intersts.Split(";"c)
               If i.Value = s Then
                   i.Selected = True
                   Exit For
               End If
           Next
       Next





编译器显示错误消息:未定义类型'ListItem'。



任何人都有一个代码用于从列中检索值并显示它们



the compiler show me error message : "Type 'ListItem' is not defined."

anyone has a code for retrieving the values from the column and display them

推荐答案

似乎你在那里做错了什么



首先你选择索引后来你使用了错误的方法



你需要使用与索引相同的方法



遍历所选索引并使用此方法setItemChecked [ ^ ]检查checkedlistbox中的项目
seems you are doing something wrong there

at first you are pick the index in the later you are using the wrong approach

you need to use the same approach as you get the index

loop through the selected index and use this method setItemChecked [^] to check the items in the checkedlistbox


您好。



尝试列表 Box 项目而不是ListItem。



然后处理i-Variable的新属性...



如果你做很多Splitstring-Operations使用.Trim是shure,String可以拆分而不会出错......



Hi.

Try ListBoxItem instead of ListItem.

And then take care of the new Properties of the i-Variable...

And if you do alot of Splitstring-Operations use a .Trim to be shure the String can Split without errors...

For Each i As ListBoxItem In CheckedListBox1.Items
           For Each s As String In intersts.Split(";"c)
               If i.Value = s.Trim Then
                   i.Selected = True 
                   Exit For
               End If
           Next
Next





A现在无法测试,所以我不记得i.Value和 i.Selected 可以使用。



也许 i.IsSelected ???但我认为IDE会帮助你...



c.u.和快乐的编码



Joshi



A can´t Test it now, so I don´t memorize if i.Value and i.Selected can be used.

Maybe i.IsSelected ??? but I think the IDE will help You with that...

c.u. and Happy Coding

Joshi


这篇关于帮助从CHECKEDBOXLIST代码中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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