Solved

VB.net CheckedListBox

Posted on 2014-11-25
10
54 Views
Last Modified: 2015-04-21
Hello Experts,

Using VB.net in VS2010. I am trying to make my label's text = the text of the checked items in the checkedlistbox separated by commas,

Ideas?
0
Comment
Question by:triphen
  • 6
  • 4
10 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40465754
One approach:

    Private Sub CheckedListBox_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
        Dim cbl As CheckedListBox = DirectCast(sender, CheckedListBox)
        Dim temp As String = String.Empty

        For i As Integer = 0 To cbl.CheckedItems.Count - 1
            If cbl.CheckedItems(i) <> cbl.Items(e.Index) Then
                temp += cbl.CheckedItems(i) + ","c
            End If
        Next

        If e.NewValue Then
            temp += cbl.Items(e.Index)
        Else
            temp = temp.Substring(0, Math.Max(temp.Length - 1, 0))
        End If

        Label1.Text = temp
    End Sub

Open in new window

0
 

Author Comment

by:triphen
ID: 40465769
I get error:

Operator '+' is not defined for string "" and type 'DataRowView'
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40465772
You'll need to adjust the code to dump out the appropriate column's value from the DRV. If memory serves, there should be a Field property which takes a column name or index. You'd need to adjust lines 7 and 12 to account for this.
0
 

Author Comment

by:triphen
ID: 40465781
Dont understand.......
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40465792
My example was based off of having strings in the CLB; you've got DataRowViews. My code works because I'm concatenating strings to strings. It won't work for you because you're effectively trying to concatenate strings and DRVs. Therefore, you have to extract out the column from the DRV that you care about, turn it into a string, and then concatenate that.

e.g.

temp += DirectCast(cbl.CheckedItems(i), DataRowView)("column name to extract").ToString() + ","c

Open in new window


Do the same for line 12.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:triphen
ID: 40465803
I changed line 7 to:

temp += DirectCast(cbl.CheckedItems(i), DataRowView)("StoreNumber").ToString() + ","c

Cant figure out how exactly I need to modify line 12 :/
0
 

Author Comment

by:triphen
ID: 40465811
OK I think I got it...


 temp += DirectCast(cbl.Items(e.Index), DataRowView)("StoreNumber").ToString()
0
 

Author Comment

by:triphen
ID: 40465816
 Dim cbl As CheckedListBox = DirectCast(sender, CheckedListBox)
        Dim temp As String = String.Empty

        For i As Integer = 0 To cbl.CheckedItems.Count - 1
            If cbl.CheckedItems(i).ToString <> cbl.Items(e.Index).ToString Then
                temp += DirectCast(cbl.CheckedItems(i), DataRowView)("StoreNumber").ToString() + ","c
            End If
        Next

        If e.NewValue Then
            temp += DirectCast(cbl.Items(e.Index), DataRowView)("StoreNumber").ToString()
        Else
            temp = temp.Substring(0, Math.Max(temp.Length - 1, 0))
        End If

        Label1.Text = temp

Open in new window



This work, but label1's text keeps getting over written. I need it to be comma separated.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40465848
It should be compiling a fresh list of everything that is checked at the time the CLB is checked. Yes, it is overwriting the label's value, but only with values that are (and have remained) checked.
0
 

Author Comment

by:triphen
ID: 40465859
Yes, it is overwriting the label's value, but only with values that are (and have remained) checked.

But that is not what is happening....the labels text is being overwritten with a single value and the previous value is "lost", even the the CLB still has those items checked.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now