Hi,
Anyone of you know hot to get item when user click on item in the listview. I want the data in listview display in the text box, can anyone give me some advice on this? The following code is my coding at Form_Load():-

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Set the List to Detail View
    ListView1.View = View.Details
    ListView1.CheckBoxes = True

    'To Activate an Item you must doubleclick the item
    'This will fire the 
    ListView_OrderEntry.Activation = ItemActivation.TwoClick

    'Add Columns
    ListView1.Columns.Add("C1", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C2", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C3", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C4", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C5", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C6", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C7", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C8", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C9", 100, HorizontalAlignment.Center)
    ListView1.Columns.Add("C10", 100, HorizontalAlignment.Center)
End Sub

Hi,

You can use SelectedIndexChange event of ListView class.

Loren Soth

Code to fill the listview control in vb.net dynamically

Dim rdGetData As SqlClient.SqlDataReader
Try
    If ListView1.Items.Count > 0 Then
        ListView1.Items.Clear()
    End If
    SQLCmd.CommandType = CommandType.Text
    SQLCmd.CommandText = "SELECT * from table"
    rdGetData = SQLCmd.ExecuteReader

    Dim intCount As Decimal = 0
    While rdGetData.Read
        ListView1.Items.Add(Trim("FieldName"))           'col no. 1
        ListView1.Items(CInt(intCount)).SubItems.Add(Trim(rdGetContactsInfo("FieldName")))  'col no. 2
        ListView1.Items(CInt(intCount)).SubItems.Add(Trim(rdGetContactsInfo("FieldName")))  'col no. 3
        intCount = intCount + 1
    End While
    rdGetData.Close()
    rdGetData = Nothing

Catch Exp As Exception
    intNumError = Err.Number()
    MsgBox("[ " & CStr(intNumError) + " ] " + Err.Description, MsgBoxStyle.Critical, " (Program Error)")
End Try 

Code to get selected item in the textbox control into the button's click event or according to your requirment.

TextBox1.Text = ListView1.SelectedItems(0).Text 
TextBox2.Text = ListView1.SelectedItems(0).SubItems(2).Text

This will copy the selected item's first value and 2nd value into the textboxes

hi,
You can use the following code to display the selected text in the textbox.

Private Sub ListView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles           ListView1.ItemDrag
    TextBox1.Text = e.Item.ToString
End Sub

Regards

Exelio

hi,
You can use the following code to display the selected text in the textbox.

Private Sub ListView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
TextBox1.Text = e.Item.ToString
End Sub


Regards

Exelio

Perhaps you can use e.Item.ToString for an ItemDrag event, however I think he is using the ItemActivate event, in which case, this will not work. Why VB does this is beyond me. ListViews have been a pain to learn in general.

thank you

'here i've got a listview1 ,label1 .
'as simple as this one. it displays the corresponding item from the listview1 straight to the 'label1 ..

Private Sub listview1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles listview1.ItemSelectionChanged

Label1.Text = e.Item.Text

End Sub


'or if you want the next column-item to be displayed, let say label2 from the same listview, then just the code

Label2.Text = e.Item.SubItems(1).Text

'(0) for the first column ,(1) for the 2nd, and so on.

note: this was coded in Visual Studio 2008, so i never tried the same code in VB.Net but i think it works the same.

You should use the mouse click event and the code below

Dim itm as ListViewItem = Me.prvLstVw.GetItemAt(e.X, e.Y)

If itm is nothing Then
Exit Sub
End If

You should use
textbox1.text = ListView1.SelectedItems.Item(0).Text

maybe this help you

private sub ListView_Click()

textbox1.text = Listview1.SelectedItems(0).Subitems(0).Text 'For C1
textbox2.text = Listview1.SelectedItems(0).Subitems(1).Text 'For C2
textbox3.text = Listview1.SelectedItems(0).Subitems(2).Text 'For C3
textbox4.text = Listview1.SelectedItems(0).Subitems(3).Text 'For C4
textbox5.text = Listview1.SelectedItems(0).Subitems(4).Text 'For C5
textbox6.text = Listview1.SelectedItems(0).Subitems(5).Text 'For C6
textbox7.text = Listview1.SelectedItems(0).Subitems(6).Text 'For C7
textbox8.text = Listview1.SelectedItems(0).Subitems(7).Text 'For C8
textbox9.text = Listview1.SelectedItems(0).Subitems(8).Text 'For C9
textbox10.text = Listview1.SelectedItems(0).Subitems(9).Text 'For C10

End Sub

"ListView1.Items.Add("kb").SubItems.Add("ghv")"; this code can only add an item at column 1 and 2, how do i add an item in the third forth etc... column? thanks

i want to do the folloing things

i have 1 combobox and 1 textbox i am entering the no in textbox and select the table from combobox when i am select the table it shows the data of this no and that table in the listview


plese give me help for this

commented: dont reply to the dead threads -1

@aterus29, change your code into this

    'Add first item
    arr(0) = "product_1"
    arr(1) = "100"
    arr(2) = "10"
    arr(3) = "10"
    itm = New ListViewItem(arr)
    ListView1.Items.Add(itm)

    'Add second item
    arr(0) = "product_2"
    arr(1) = "200"
    arr(2) = "20"
    arr(3) = "10"
    itm = New ListViewItem(arr)
    ListView1.Items.Add(itm)

sirs, do you have a version of this code for listview web control. it doesnt work on web based listview.. thanks//

--Sirs,, it tried this code on web app but not working.. pls help me for the web version..
    Dim rdGetData As SqlClient.SqlDataReader
        Try
            If ListView1.Items.Count > 0 Then
                ListView1.Items.Clear()
            End If
            SQLCmd.CommandType = CommandType.Text
            SQLCmd.CommandText = "SELECT * from table"
            rdGetData = SQLCmd.ExecuteReader

            Dim intCount As Decimal = 0
            While rdGetData.Read
                ListView1.Items.Add(Trim("FieldName")) 'col no. 1
                ListView1.Items(CInt(intCount)).SubItems.Add(Trim(rdGetContactsInfo("FieldName"))) 'col no. 2
                ListView1.Items(CInt(intCount)).SubItems.Add(Trim(rdGetContactsInfo("FieldName"))) 'col no. 3
                intCount = intCount + 1
            End While
            rdGetData.Close()
            rdGetData = Nothing

        Catch Exp As Exception
            intNumError = Err.Number()
            MsgBox("[ " & CStr(intNumError) + " ] " + Err.Description, MsgBoxStyle.Critical, " (Program Error)")
        End Try


        TextBox1.Text = ListView1.SelectedItems(0).Text
        TextBox2.Text = ListView1.SelectedItems(0).SubItems(2).Text

add items in list view

        arr[0] = "product_1";
        arr[1] = "100";
        arr[2] = "10";
        itm = new ListViewItem(arr);
        listView1.Items.Add(itm);

get items from list view

        productName = listView1.SelectedItems[0].SubItems[0].Text;
        price = listView1.SelectedItems[0].SubItems[1].Text;
        quantity = listView1.SelectedItems[0].SubItems[2].Text;

http://vb.net-informations.com/gui/vb.net-listview.htm

maley

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.