Link to home
Start Free TrialLog in
Avatar of NCSO
NCSO

asked on

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
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

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
ASKER CERTIFIED SOLUTION
Avatar of jake072
jake072
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
missed that :) was reading
>>I need textbox1.text to read as "1,2" if both are clicked
Didn't mean to steal your thunder :)

Just noticed it myself...

Jake
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
Avatar of Sancler
Sancler

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
Avatar of NCSO

ASKER

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
Avatar of NCSO

ASKER

Jake,  it worked perfectly.  I used the code on control "leave".  Thanks to both of you for your help