Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.net CheckedListBox

Posted on 2014-11-25
10
Medium Priority
?
64 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 1500 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
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.

 

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

661 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