Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CheckedListBox SelectedValue 1

Status
Not open for further replies.

jebenson

Technical User
Feb 4, 2002
2,956
US
Hello all,
Does anybody know how to get the SelectedValue for each checked item in a CheckedListBox, without scrolling through the list? I have this code that returns the SelectedValue:

Code:
For i = 0 To lstAgy.Items.Count - 1
    If lstAgy.GetItemCheckState(i) = CheckState.Checked Then
        lstAgy.SetSelected(i, True)
        Module1.AGY = Module1.AGY & Trim(lstAgy.SelectedValue) & ","
    End If
Next

but it scrolls through the list, which looks very "clunky".

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Unless I've misunderstood something, I don't think you need to use .SetSelected.

Try the following:

Code:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim s As String = ""
    For a As Integer = 0 To CheckedListBox1.Items.Count - 1
      If CheckedListBox1.GetItemCheckState(a) = CheckState.Checked Then
        s += CheckedListBox1.Items(a).ToString + Environment.NewLine
      End If
    Next
    MessageBox.Show(s)

  End Sub



Hope this helps.
 
earthandfire,

you are looping through all the items in the listbox. He do not want this, but to get directly a collection of the checked items only. (i think)
 
VBakias, yes but not re-selecting the items - therefore no scrolling is involved.


Hope this helps.
 
VBakias, by the way, .CopyTo requires a destination array at least as large as the elements being copied into it, so:

Dim s1 As String
Dim ss() As String
ReDim ss(CheckedListBox1.Items.Count - 1)
CheckedListBox1.Items.CopyTo(ss, 0)
s1 = ""
s.Join(Environment.NewLine, ss)
MessageBox.Show(s)


would be an alternative method.


Hope this helps.
 
I know. Just provided the method to use.

Anyway, what is the s? (s.Join(Environment.NewLine, ss)
)
and where do you use the string s1?
 
My mistake, should have been:

Dim s1 As String
Dim ss() As String
ReDim ss(CheckedListBox1.CheckedItems.Count - 1)
CheckedListBox1.CheckedItems.CopyTo(ss, 0)
s1 = ""
s1 = s1.Join(Environment.NewLine, ss)
MessageBox.Show(s1)


I've got both versions in the same sub, so I did some copying and pasting - my original test worked (or at least appeared to because I was showing s with MessageBox.

The above code works properly - sorry for any confusion.
 
earthandfire:

I tried your code and I get this error message:

"Object cannot be stored in an array of this type."

in this line:

CheckedListBox1.CheckedItems.CopyTo(ss, 0)

So apparently the code is trying to store the actual DataRowView object in the String array.

The first block of code you provided just produces a string with "System.Data.DataRowView" for every checked item.

Any other ideas?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
I'm confused. Your original code only referred to a CheckedListBox and you assigned the results to a variable Module.AGY.

Where does the DataRow fit in?
 
Well, if anybody ever need to do this, here is a way:

Code:
Dim i As Integer
Dim s As String

For i = 0 To CheckedListBox1.CheckedItems.Count - 1
    s = s & CheckedListBox1.CheckedItems(i).Item(0).ToString.Trim & ","
Next

'remove trailing comma
s = Mid(Module1.AGY, 1, Len(Module1.AGY) - 1)

MsgBox(Module1.AGY)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
jebenson, I've thoroughly tested my original and the corrected version of my second solution - both seem to work fine.

I still don't see where the DataRowView comes in. You have not referred to it in your last post.
 
earthandfire,

Module1.AGY is a String variable to hold the values from the checked items in the listbox. The listbox is populated from a SQL Server table. As I stated in my earlier response, the first code you provided puts the text "System.Data.DataRowView" into the Module1.AGY String for each checked item. The second bit of code throws the error I stated earlier.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top