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.SqlC onnection( "Persist Security Info=True;Server=xxx.xxx.x xx.xxx;Dat abase=Repo rting;uid= uid;networ k=dbmssocn ;password= password")
Dim da2 As New System.Data.SqlClient.SqlD ataAdapter ("Select * from tblOffense", conn2)
Dim ds2 As New System.Data.DataSet
Dim cmd As New System.Data.SqlClient.SqlC ommand("Se lect 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").T oString)
lvi.SubItems.Add(myReader. Item("offe nsedesc"). ToString() )
ListView1.Items.Add(lvi)
Loop
myReader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message )
Finally
conn2.Close()
End Try
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.SqlC
Dim da2 As New System.Data.SqlClient.SqlD
Dim ds2 As New System.Data.DataSet
Dim cmd As New System.Data.SqlClient.SqlC
Try
conn2.Open()
Dim myReader As SqlDataReader = cmd.ExecuteReader()
Do While myReader.Read()
Dim lvi As New ListViewItem(myReader.Item
lvi.SubItems.Add(myReader.
ListView1.Items.Add(lvi)
Loop
myReader.Close()
Catch ex As Exception
MessageBox.Show(ex.Message
Finally
conn2.Close()
End Try
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
missed that :) was reading
>>I need textbox1.text to read as "1,2" if both are clicked
>>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
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.Cou nt > 0 Then
TextBox1.Text = ""
For Each lvItem In ListView1.CheckedItems
TextBox1.Text = TextBox1.Text & lvItem.Text
Next
End If
End Sub
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.Cou
TextBox1.Text = ""
For Each lvItem In ListView1.CheckedItems
TextBox1.Text = TextBox1.Text & lvItem.Text
Next
End If
End Sub
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
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
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
ASKER
Jake, it worked perfectly. I used the code on control "leave". Thanks to both of you for your help
----------
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.Co
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