listview checked item

S

spowel4

My form has a listview populated with the 50 states. When the user
checks the checkbox within the listview for a particular state, I need
to retrieve which state was checked (i.e. if AZ is checked I need to
get the text AZ) and search through an array for matching text. When
matching text is found I need to populate another listview with some
information.

So if the array is two-dimensional and I'm looking at elements
(0,0 ... 0,7) for matching text, the matching text will always be at
(0,6) and when a match is found I need to send the text found in (0,1)
to another listview. I am not sure what listview event to work with
and what property I need to use (checkeditem, etc...). It does need
to be a checked item though and not a selected item.
 
C

Cor Ligthert[MVP]

spowel4.

A little question, did you try to make some code and if you did, than what
did not work?

You can this do the best by writting a little test and then show us what
does not work.

We want to help here, not write the code for you.

Be aware that an two dimmensional array is not the easiest thing to use in a
listbox. A hashtable or a datatable will mostly makes this more simple.

Cor
 
S

spowel4

A little question, did you try to make some code and if you did, than what
did not work?

You can this do the best by writting a little test and then show us what
does not work.

We want to help here, not write the code for you.

Be aware that an two dimmensional array is not the easiest thing to use in a
listbox. A hashtable or a datatable will mostly makes this more simple.

Cor

No, I wouldn't want you to write the code for me; I apologize if
that's how my post came across. I just need some pointers/advice
about how to proceed.

Here is the code I have at this point, I've been using debug.print to
try to see what's going on. Currently I'm using the ItemCheck event
to try to capture the text of the checked item but as soon as I check
the checkbox of an item I get an error "object reference not set to an
instance of an object". If I change this line:
dim checkedItem as listviewitem

to this line:
dim checkedItem as new listviewitem

that gets rid of that error message but then the text always returns
as empty.
I've tried the ItemChecked event but that event seems to be firing as
soon as the form loads, so basically I'm lost here.

Private Sub ListViewStates_ItemCheck(ByVal sender As Object, ByVal e
As System.Windows.Forms.ItemCheckEventArgs) Handles
ListViewStates.ItemCheck

'Populate the ShipTos listview
cnt = 0
cnt1 = 0
reader1 = File.OpenText(Application.StartupPath &
"\arshipto.cd")
While Not reader1.EndOfStream
fileContents = reader1.ReadLine()
'Populate the ShipTos listview with the shipto id field of
shipToInfo array
If checkedItem.Checked = True Then
Debug.Print(checkedItem.Text)
End If
ListViewShipTos.Items.Add(shipToInfo(cnt,
cnt1).Trim(Chr(34)))
cnt1 = cnt1 + 1
End While
reader1.Close()
End Sub
 
C

Cor Ligthert[MVP]

spowe,

To do something with checked items will mean in my idea forever creating a
buffer and than looping through the checked items in the control and perform
for all of those the actions you want.

It makes in my idea no sence to use a checked item to start with that a
procedure, then you can for the same use the selected item.

Cor
 
D

Dean Slindee

Set the listview property so that only one item can be selected.
No need to use checked items, here is the call to the function below:
For Each itm In lvwSearch.SelectedItems

strCustomerID = ListviewItemGetColumnValue(lvwSearch, itm.Index,
"CustomerID")




Next itm


Here is the called function that gets the value in a listview selected
item's cell:

Public Shared Function ListviewItemGetColumnValue(ByVal lvw As ListView, _

ByVal idx As Integer, _

ByVal str As String) As String

Dim col As Integer

Dim hdr As ColumnHeader

Dim itm As ListViewItem

Try

'find column header matching str

col = 1

For Each hdr In lvw.Columns

If hdr.Text = str Then

Exit For

Else

col += 1

End If

Next

'find selected item



itm = lvw.Items(idx)



Dim strValue As String

If col = 1 Then

strValue = CType(itm.Text, String)

Else

strValue = CType(itm.SubItems(col - 1).Text, String)

End If

Return strValue

Catch exc As Exception

Call ExceptionHandler("clsListview", exc)

Return String.Empty

Finally

End Try

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top