Solved

VB.net CheckedListBox

Posted on 2014-11-25
10
63 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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