Solved

Listview to textbox

Posted on 2006-06-09
13
1,366 Views
Last Modified: 2013-12-25
Hi Experts,

I am tired of punding my head in, so I am turning to the "guru's".  I have a listview that is populated at load from a database; this listview has five columns.  I want to (on click of checkbox) fill a textbox with the contents of column five of the selected record.

Example:

PersonType_ID        PersonNumber         Offense_Indicator          FullName                   person_id
       W                          W-1                          1,2,3                 Joe N. Smith                  99999


 
In the above example I need for the textbox to be filled with the 99999.  How can I make this happen?  Below is my current "LOAD" code for the form:



    Private Sub OffenseIncidnetPeopleEditAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GroupBox1.Text = "Offenses for Agency Report Number: **********"


        Dim conn2 As New System.Data.SqlClient.SqlConnection("Persist Security Info=True;Server=127.0.0.1;Database=Reporting;uid=sa;network=dbmssocn;password=xxxxxx")
        Dim da2 As New System.Data.SqlClient.SqlDataAdapter("Select * from tblPeople", conn2)
        Dim ds2 As New System.Data.DataSet
        Dim loggedOnUser As String
        loggedOnUser = My.User.Name
        Dim cmd2 As New System.Data.SqlClient.SqlCommand("select authorid from tblAuthor where username='" & loggedOnUser & "'", conn2)
        Dim cmd As New System.Data.SqlClient.SqlCommand("SELECT LastName +  ', ' + FirstName + ' ' + MiddleName AS FullName, PersonType_ID, Person_ID, PersonNumber, Offense_Indicator from tblPeople where arn='" & lblArn.Text & "' and Incident_ID='" & lblIncidentID.Text & "' and deleted='0' order by dateadded", conn2)

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

                Dim lvi As New ListViewItem(myReader.Item("PersonType_ID").ToString)
                lvi.SubItems.Add(myReader.Item("personnumber").ToString())
                lvi.SubItems.Add(myReader.Item("Offense_Indicator").ToString())
                lvi.SubItems.Add(myReader.Item("FullName").ToString())
                lvi.SubItems.Add(myReader.Item("person_id").ToString())

                ListView1.Items.Add(lvi)

            Loop

            myReader.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conn2.Close()

        End Try
    End Sub
0
Comment
Question by:NCSO
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 16872505
Hi NCSO,
----------

you can try
TextBox1.Text =  ListView1.SelectedItems(0).SubItems(5).Text

not sure now if the subitems are starting at zero also so it could be
TextBox1.Text =  ListView1.SelectedItems(0).SubItems(4).Text

----------
bruintje
share what you know, learn what you don't
0
 

Author Comment

by:NCSO
ID: 16872723
I have tried this and I get the following error:

InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 16872769
Private Sub ListView1_Click()

    my_SELECTED_ROW = ListView1.SelectedItem.Index'get current row
    LR= ListView1.ColumnHeaders.Count 'get the last col
    tmp = ListView1.ListItems(my_SELECTED_ROW).SubItems(LR - 1)
    Text1.Text = tmp
   
End Sub
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16872807
you probably have not item selected on the first run so without selecting a row
selected items will be empty then you need to pass in a index yourself

TextBox1.Text =  ListView1.Items(0).SubItems(5).Text

0
 

Author Comment

by:NCSO
ID: 16873031
vb_elmer,

That didn't work either.  I forgot to mention, I am using VB2005
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16873056
are you selecting an item in the load fucntion or not?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:NCSO
ID: 16873205
no, at load the listview is filled from a database, I am selecting checkboxes
0
 
LVL 44

Expert Comment

by:bruintje
ID: 16873228
but i don't understand the sequence

1. you fill the listview
2. then click on a checkbox (in the listview?)
3. and the text box should show the subitemtext

or is there a step to select a row first
0
 

Author Comment

by:NCSO
ID: 16873347
Sorry,

1. Form loads
2. onLoad the listview fills
3. click checkbox on listview
4. textbox show the data in column 5
0
 
LVL 44

Accepted Solution

by:
bruintje earned 500 total points
ID: 16873557
it uses the item check event to get the checked item collection

try something like

Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
    TextBox1.Text = ListView1.CheckedItems(0).SubItems("person_id").ToString()
        ' or
    TextBox1.Text = ListView1.CheckedItems(0).SubItems(5).ToString()
End Sub

maybe even better [i'm off now :)]

Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
  TextBox1.Text = ListView1.Items(e.Index).SubItems(5).ToString()
End Sub
0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 16875454
>>  I forgot to mention, I am using VB2005
uaahhh .
0
 
LVL 9

Expert Comment

by:lojk
ID: 16890123
The DataGridView control in vb2005 is excellent and far superior to the old datagrid of .net1.x and may be a better and easier replacement to your listbox.

Then all youd need to do would be to load the dataset set using a dataadaptor instead of a command reader and Bind it to the DataGridView (the DataGridView is easy to set as readonly or additional behavioru for each column action unlike the old DataGrid)  (actuall recommed binding it via a DataView control for easy readonly setting) and catch the currentcellchanged event and check if is column 4 (5-1) and react accordingly...
0
 
LVL 9

Expert Comment

by:lojk
ID: 16890131
oh youd probably want to set RowselectMode = true to make it look a little more like a list box (and even more so if you removed the side and top headers and sett the gridcolumn colour to systemcolor.window)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now