Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1156
  • Last Modified:

Get Text of Item in CheckedListBox

What happened to List(index)????
Anyway, I need to retrieve the text of a checked items in a checkedlistbox.
I have search all over and tried a lot of different things, but nothing works.
I listed a couple of them in the code section.
I also listed my fill procedure for the listbox.
HOW do I get the text value of this checkedlistbox???
Thanks,

I have tried
If Me.lstChkSessions.GetItemChecked(i) = True Then
            s = Me.lstChkSessions.SelectedItems(i)
            ft.TabRacing.TabPages(i).Text = s
        End If
 
For Each item As Object In Me.lstChkSessions.SelectedItems
            If Me.lstChkSessions.GetItemChecked(i) = True Then
                If TypeOf item Is DataRowView Then
                    s = DirectCast(item, DataRowView)(i)  '("strCircuit")
                End If
            End If
 
        Next item
 
My fill...
Public Sub FillCheckedList(ByVal cnt As CheckedListBox)
        Dim ds As New DataSet
 
        If DatabaseToUse() = BizObjs.DBType.Access Then
            'Access Database
            Dim cFields As New cFieldTableNames
            cFields.GetStrTableName = msTblName
            cFields.ComboTable = msTblName
            SelectStr = cFields.ReturnComboString()
 
 
            Dim dc As New OleDb.OleDbConnection(ConnectionString)
            Dim da As New OleDb.OleDbDataAdapter(mSelectStr, dc)
          
 
            Try
                da.Fill(ds)
                dc.Close()
 
                cnt.DataSource = ds.Tables(0)
                cnt.DisplayMember = cFields.SendToCorrectFieldProcedure(cFields.ReturnDisplayField + 1)
                cnt.ValueMember = cFields.SendToCorrectFieldProcedure(1)
 
            Catch ex As Exception
 
                MessageBox.Show(ex.Message)
                Throw ex
 
            Finally
                dc.Close()
                da.Dispose()
            End Try
        End If
 
    End Sub

Open in new window

0
Sheritlw
Asked:
Sheritlw
  • 5
  • 5
2 Solutions
 
kennethfineCommented:
Hi,

Study this page. It is in C# but the attributes you want are identical. They have a demo that does exactly what you are trying to do:

http://www.geekpedia.com/tutorial40_ListBox-and-CheckedListBox.html 
0
 
abelCommented:
It seems to me that you are a bit confused about the type Object of a selected or checked list item. But calling .ToString() on that object will give you the text of that item.

And... you confused SelectedItems with CheckedItems... here you go

For Each itm As Object In lstChkSessions.CheckedItems
    Dim itemText As String = itm.ToString()
    ' do something with itemText... or not
Next
That 's all there is to it.
0
 
SheritlwAuthor Commented:
I can't click on or copy and paste links... just tried again:-(
My IE always crashes.
Please provide an example in VB.Net
Thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
abelCommented:
Btw, in case you are wondering, the underlying item type is String (you can see that when debugging and doing item.GetType()). If you are used to rich item objects from the ASP.NET worlds, you can be quite disappointed to find out that it is so trimmed down here...
0
 
abelCommented:
> Please provide an example in VB.Net
did you miss my post? I (other p then of the first reply) gave a small example there.
> I can't click on or copy and paste links..> My IE always crashes.

interesting. IE is about browsing the web and browsing the web is about clicking links... I abandoned IE a while ago in favor of Opera and Firefox... maybe time to upgrade? ;-)


0
 
SheritlwAuthor Commented:
I just tried
For Each itm As Object In lstChkSessions.CheckedItems
            Dim itemText As String = itm.ToString()
            MessageBox.Show(itemText)
            ' do something with itemText... or not
        Next

ItemText returns = System.Data.DataRowView
0
 
abelCommented:
Sorry, then I showed you the wrong code. What is the object that is the CheckedListBox? Because you should replace lstChkSessions with the object that is your CheckedListBox...

btw: was I correct in assuming VB.NET and WinForms?
0
 
SheritlwAuthor Commented:
My checkedlistbox is named lstChkSessions so that is correct.
What else should I try to get the text?

Thanks
0
 
SheritlwAuthor Commented:
Well I finally figured it out.

This is what I had to do

For Each i As Object In Me.lstChkSessions.CheckedItems

            s = Me.lstChkSessions.GetItemText(i).ToString
            ft.TabRacing.TabPages(c).Text = s
            c = c + 1

        Next i

abel you were close so I will award you the points.

Thanks
0
 
abelCommented:
Glad you found it. I'm puzzled by your code in that it cannot possibly work with a CheckedListBox control. And since you showed DataRowView, I assume that something else is used... OR ... you managed to get DataRowView items inside the listbox.
0
 
SheritlwAuthor Commented:
I think it's because of the way I fill it, but it works!!
Thanks for your help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now