954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ListView in VB.Net

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

khwo
Newbie Poster
12 posts since Jun 2006
Reputation Points: 10
Solved Threads: 0
 

Hi,

You can use SelectedIndexChange event of ListView class.

Loren Soth

Lord Soth
Posting Whiz in Training
233 posts since Mar 2006
Reputation Points: 28
Solved Threads: 4
 

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

Tanvir
Newbie Poster
3 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 

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

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


Regards

Exelio

Exelio
Junior Poster in Training
57 posts since Aug 2006
Reputation Points: 10
Solved Threads: 0
 
hi, You can use the following code to display the selected text in the textbox. PrivateSub ListView1_ItemDrag(ByVal sender AsObject, 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.

giNMaN86
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

thank you

ola awny
Newbie Poster
1 post since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

'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.

pergsify
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

sfdasdfa

noelcahapon
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

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

kevin m cain
Newbie Poster
1 post since May 2011
Reputation Points: 10
Solved Threads: 0
 

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

Gumagur
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

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
maccinelyro
Newbie Poster
8 posts since Feb 2011
Reputation Points: 10
Solved Threads: 1
 

"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

aterus29
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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

Attachments untitled.bmp (978.8KB)
laxmanpathare
Newbie Poster
3 posts since Feb 2012
Reputation Points: 9
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You