Solved

VB.net CheckedListBox

Posted on 2014-11-25
10
56 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

805 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