• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

how to make textbox equal the checked value of the listview

Hi Experts,

How can I make textbox1.text equal the checked value(s) of Listview1?  The ListView contains (2) columns;

Offense ID                Offense Description
     1                                DWLSR
     2                             Grand Theft

I need textbox1.text to read as "1,2" if both are clicked.

Below is the code that I currently have, thanks in advance for you cooperation.

        Dim conn2 As New System.Data.SqlClient.SqlConnection("Persist Security Info=True;Server=xxx.xxx.xxx.xxx;Database=Reporting;uid=uid;network=dbmssocn;password=password")
        Dim da2 As New System.Data.SqlClient.SqlDataAdapter("Select * from tblOffense", conn2)
        Dim ds2 As New System.Data.DataSet
        Dim cmd As New System.Data.SqlClient.SqlCommand("Select offense_number, offensedesc from tblOffense where incident_ID='" & IncidentPeopleAdd.Incident_ID.Text & "' and ARN='" & IncidentPeopleAdd.ARN.Text & "'", conn2)


        Try
            conn2.Open()
            Dim myReader As SqlDataReader = cmd.ExecuteReader()
            Do While myReader.Read()

                Dim lvi As New ListViewItem(myReader.Item("offense_number").ToString)
                lvi.SubItems.Add(myReader.Item("offensedesc").ToString())
                ListView1.Items.Add(lvi)



            Loop
            myReader.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn2.Close()
        End Try
0
NCSO
Asked:
NCSO
  • 3
  • 2
  • 2
  • +1
1 Solution
 
bruintjeCommented:
Hi NCSO,
----------

tested this as follows

1. put a listview on the form
2. a textbox
3. and a button

then i added a few items and subitems

for your question i added this to the buttonclick event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lvItem As ListViewItem
  If ListView1.SelectedItems.Count > 0 Then
    TextBox1.Text = ""
    For Each lvItem In ListView1.SelectedItems
      TextBox1.Text = TextBox1.Text & lvItem.Text
    Next
  End If
End Sub

----------
bruintje
share what you know, learn what you don't
0
 
jake072Commented:
Don't you want the CHECKED items?

        If Not (ListView1.CheckedItems Is Nothing) AndAlso ListView1.CheckedItems.Count > 0 Then
            For i As Integer = 0 To ListView1.CheckedItems.Count - 1
                TextBox1.Text &= CStr(ListView1.CheckedItems.Item(i).Index + 1) & CStr(IIf(i < ListView1.CheckedItems.Count - 1, ",", "")) ' Add 1 for zero based index...  Also, the "IIf" will only put a ', ' if there are more items to add...
            Next
        End If

Hope that helps,

Jake
0
 
bruintjeCommented:
missed that :) was reading
>>I need textbox1.text to read as "1,2" if both are clicked
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jake072Commented:
Didn't mean to steal your thunder :)

Just noticed it myself...

Jake
0
 
bruintjeCommented:
no problem keeps me awake :)

just changed it to checked items which works basically the same as selected items
lovely stuff this .net framework

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lvItem As ListViewItem
  If ListView1.CheckedItems.Count > 0 Then
    TextBox1.Text = ""
    For Each lvItem In ListView1.CheckedItems
      TextBox1.Text = TextBox1.Text & lvItem.Text
    Next
  End If
End Sub
0
 
SanclerCommented:
jake

I think NCSO wants the item text - which in this case happens to be 1 and 2 - rather than the index.

bruntje

I think s/he wants commas between the items

but between you - ;-) - I think the right code's there.  

What I'm less sure about, however, is where to put it.  I'm not sure s/he wants to have to click a button.  But I've just tried both .ItemCheck and .MouseUp and discovered that the textbox.text doesn't change until the mouse is moved as well.  It doesn't actually have to move OFF the checkbox, but it seems to have to move somewhat, even if ever so slightly.  Can either of you think of some other event to use?

Roger
0
 
NCSOAuthor Commented:
Thanks for the posts, I am wanting the textbox to be populated on "itemChecked".  I will try the code above and post the results here, Thanx
0
 
NCSOAuthor Commented:
Jake,  it worked perfectly.  I used the code on control "leave".  Thanks to both of you for your help
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now