Solved

Listview to textbox

Posted on 2006-06-09
13
1,375 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
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!

 
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
 

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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

756 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