Solved

VB.net CheckedListBox

Posted on 2014-11-25
10
50 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
I get error:

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Dont understand.......
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:triphen
Comment Utility
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
Comment Utility
OK I think I got it...


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

Author Comment

by:triphen
Comment Utility
 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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 …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

16 Experts available now in Live!

Get 1:1 Help Now