[RESOLVED] How to get data from a ListView?
Hi guys.
How to get data from a ListView?
I make a few apps using a ListView which has 1,000 of data which there is no point is printing them all out.
If I right click on an item how can I I get the data to the clipboard?
Code:
Private Sub lvwEngine_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Set lvwEngine.SelectedItem = lvwEngine.ListItems(1)
'vbRightButton 1 Left button is pressed
'vbLeftButton 2 Right button is pressed
If vbLeftButton Then
Clipboard.Clear
lvwEngine.SelectedItem
Clipboard.GetText
End If
End Sub
This doesn't appear to get anything.
Re: How to get data from a ListView?
Maybe something like:
Code:
Private Sub ListView1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Item As MSComctlLib.ListItem
Dim Text As String
Dim I As Long
If Button And vbRightButton Then
With ListView1
Set Item = .SelectedItem
If Not Item Is Nothing Then
Clipboard.Clear
Text = Item.Text
For I = 1 To .ColumnHeaders.Count - 1
Text = Text & vbTab & Item.SubItems(I)
Next
Clipboard.SetText Text, vbCFText
End If
End With
End If
End Sub
Re: How to get data from a ListView?
Thanks for the reply.
I tried your code but it says:
Dim Item As MSComctlLib.ListItem
It says User-defined type not defined.
I'm using Microsoft Windows Common Control 5.0 (SP2) for the ListView.
1 Attachment(s)
Re: How to get data from a ListView?
Hello Keithuk,
This is what you have to do.
Code:
Private Sub lvwEngine_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbRightButton Then
Clipboard.Clear
Clipboard.SetText lvwEngine.SelectedItem.Text
Debug.Print "Right button is pressed"
Debug.Print "Data from clipboard : " & Clipboard.GetText
ElseIf Button = vbLeftButton Then
Debug.Print "Left button is pressed"
End If
End Sub
Run the project I have attached with the immediate window open.
And i have a small advice for you, Pay attention for indentation. It will help you to make your code more readable.
Please mark the thread as resolved and rate my post if you think it is useful
Re: How to get data from a ListView?
Quote:
Originally Posted by
Keithuk
Thanks for the reply.
I tried your code but it says:
Dim Item As MSComctlLib.ListItem
It says User-defined type not defined.
I'm using Microsoft Windows Common Control 5.0 (SP2) for the ListView.
Just drop the 'Mscomctllib'. It's referring to the 6.0 ListView. Or change it to Comctllib.
Re: How to get data from a ListView?
Quote:
Originally Posted by
fafalone
Just drop the 'Mscomctllib'. It's referring to the 6.0 ListView. Or change it to Comctllib.
The main reason I use Microsoft Windows Common Control 5.0 (SP2) is because the progress bar shows a perfect line where as 6.0 the line is broken up with the bars which I don't like. It strange 5.0 is perfect and 6.0 they cocked it up.
Anees - I tried you code and it works in a fashion it shows the text from the 1 column you have but I have 9 columns showing text from a database.
I looked at the code for extracting the text to each column.
Dim itmX As ListItem
Dim Rs As Recordset
itmX.SubItems(1) = (Rs!EC)
itmX.SubItems(2) = (Rs!KW)
itmX.SubItems(3) = (Rs!HP)
itmX.SubItems(4) = (Rs!Ltr)
itmX.SubItems(5) = (Rs!Cyl)
itmX.SubItems(6) = (Rs![Mounting Time])
itmX.SubItems(7) = (Rs!Model)
itmX.SubItems(8) = (Rs!Remarks)
I tried
Clipboard.SetText lvwEngine.ListItems(1) & " " & lvwEngine.ListItems(2) & " " & lvwEngine.ListItems(3) & " " & lvwEngine.ListItems(4)
but it shows the text from column 1 4 times
SubItems only shows 12225341.2
Re: How to get data from a ListView?
Try this
Code:
Private Sub lvwEngine_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim j As Integer
Dim c As String
If Button = vbRightButton Then
Clipboard.Clear
c = ListView1.SelectedItem.Text & vbNewLine
For j = 1 To ListView1.ColumnHeaders.Count - 1
c = c & ListView1.SelectedItem.SubItems(j) & vbNewLine
Next
Clipboard.SetText c
End If
End Sub
Quote:
The main reason I use Microsoft Windows Common Control 5.0 (SP2) is because the progress bar shows a perfect line where as 6.0 the line is broken up with the bars which I don't like
You can change this by
Code:
ProgressBar1.Scrolling = ccScrollingSmooth
Re: How to get data from a ListView?
Quote:
Originally Posted by
Keithuk
The main reason I use Microsoft Windows Common Control 5.0 (SP2) is because the progress bar shows a perfect line where as 6.0 the line is broken up with the bars which I don't like. It strange 5.0 is perfect and 6.0 they cocked it up.
They didn't, not really.
The main difference is that the "5.0" OCX wraps comctl32.dll and the "6.0" one does not. Instead it has its own implementation built in and that was based on comctl32.dll as it was in 1997 or so. The main idea there was to add extensions to what comctl32.dll provided, but it was never meant to live forever.
That means as Windows changes comctl32.dll the "5.0" OCX picks up those changes.
If VB had not been killed off in order to force a move to .Net we'd probably have a "12.0" version or something by now. We'd probably all be enjoying our flying cars living in universal prosperity within colonies all over the solar system as well. Instead we got .Net and the dreams were dashed.
1 Attachment(s)
Re: How to get data from a ListView?
Try this.
Code:
Private Sub lvwEngine_Mouseup(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Item As ListItem
Dim Text As String
Dim i As Integer
If Button = vbRightButton Then
With lvwEngine
Set Item = .SelectedItem
If Not Item Is Nothing Then
Text = Item.Text
For i = 1 To .ColumnHeaders.Count - 1
Text = Text & Item.SubItems(i)
Next
On Error GoTo HandleErrors:
Clipboard.Clear
Clipboard.SetText Text
Debug.Print "Right button is pressed"
Debug.Print "Data from clipboard : " & Clipboard.GetText
Text1.Text = Clipboard.GetText
End If
End With
End If
Exit Sub
HandleErrors:
Select Case MsgBox(Error(Err.Number), vbCritical + vbRetryCancel, "Error Number" + Str(Err.Number))
Case vbCancel
Exit Sub
Case vbRetry
Resume
Case vbIgnore
Resume Next
End Select
End Sub
Re: How to get data from a ListView?
Sorry for the late reply, I've been busy with a lot of other things.
Quote:
Originally Posted by
4x2y
Try this
Code:
Private Sub lvwEngine_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim j As Integer
Dim c As String
If Button = vbRightButton Then
Clipboard.Clear
c = ListView1.SelectedItem.Text & vbNewLine
For j = 1 To ListView1.ColumnHeaders.Count - 1
c = c & ListView1.SelectedItem.SubItems(j) & vbNewLine
Next
Clipboard.SetText c
MsgBox c
End If
End Sub
Thanks 4x2y for your solution which works.
Your solution just shows the text from each ListView1.SelectedItem.Text. I needed to add extra text in there so I know what its showing.
Code:
im J As Integer
Dim C As String
If Button = vbRightButton Then
Clipboard.Clear
C = lvwEngine.SelectedItem.Text '& vbNewLine
For J = 1 To lvwEngine.ColumnHeaders.Count - 1
'C = C & " " & lvwEngine.SelectedItem.SubItems(J) '& vbNewLine
Next
'MsgBox C
C = "Mounting Time: " & lvwEngine.SelectedItem.SubItems(6) & " Model: " & lvwEngine.SelectedItem.SubItems(7) & " Engine Code: " & lvwEngine.SelectedItem.SubItems(1) & " " & lvwEngine.SelectedItem.SubItems(2) & "Kw " & lvwEngine.SelectedItem.SubItems(3) & "PS " & lvwEngine.SelectedItem.SubItems(4) & " Ltr " & lvwEngine.SelectedItem.SubItems(5) & " Cyls " & lvwEngine.SelectedItem.SubItems(8)
Clipboard.SetText C
'MsgBox C
MsgBox "Data copied to Clipboard.", 64, "Copied to Clipboard."
End If
Your code shows:
Audi 1Z 66 90 1.9 4 01/95-07/97 Audi A4 Greece ("GR."): Turbocharged Direct Injection (TDI)
Mine shows as I want it to show:
Mounting Time: 01/95-07/97 Model: Audi A4 Engine Code: 1Z 66Kw 90PS 1.9 Ltr 4 Cyls Greece ("GR."): Turbocharged Direct Injection (TDI)
There are 8 fields in the database. The first field in the database is the 4 manufactures, Audi, Seat, Skoda, VW which I don't really need to show but I added an extra part in just to see if it works and it doesn't.
Each of the lvwEngine.SelectedItem.SubItems text shows:
Engine Code: Index number 1
Kw: Index number 2
PS: Index number 3
Ltr: Index number 4
Cyls: Index number 5
Mounting Time: Index number 6
Model: Index number 7
Remarks: Index number 8
If there are 8 fields listed which one is the manufacture? I added a 9 in there but it came back with a 380 Invalid property value which I would have expected.
I've put a breakpoint in your code and cycled through you code and it goes through 8 times.
I can't figure out which Index number shows the manufacture as it shows Audi in your clipboard info.
Re: How to get data from a ListView?
I have used For...Next loop because i thought you want to put all columns with their order into the clipboard, but you are free to pick only the columns you want with any extra description.
Code:
Private Sub lvwEngine_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim c As String
If Button = vbRightButton Then
Clipboard.Clear
c = "Mounting Time: " & ListView1.SelectedItem.Text & " "
c = c & "Model: " & ListView1.SelectedItem.SubItems(3) & " " ' change index to meet the actual index of column you want
c = c & "Audi A4 Engine Code: " & ListView1.SelectedItem.SubItems(2) & " "
c = c & "Greece (""GR.""): " & ListView1.SelectedItem.SubItems(6) & " "
' etc...
Clipboard.SetText c
End If
End Sub
Re: How to get data from a ListView?
Yes I know how your code works but as I said yesterday:
Your code show in the clipboard:
Audi 1Z 66 90 1.9 4 01/95-07/97 Audi A4 Greece ("GR."): Turbocharged Direct Injection (TDI)
Manufacturer, Engine Code, Kw, PS, Ltr, Cyls, Mounting Time, Remarks
Mine shows for the same line:
Mounting Time: 01/95-07/97 Model: Audi A4 Engine Code: 1Z 66Kw 90PS 1.9 Ltr 4 Cyls Greece ("GR."): Turbocharged Direct Injection (TDI)
What I was asking how come yours shows Audi and mine doesn't even though I know which lvwEngine.SelectedItem.SubItems shows which
Engine Code: Index number 1
Kw: Index number 2
PS: Index number 3
Ltr: Index number 4
Cyls: Index number 5
Mounting Time: Index number 6
Model: Index number 7
Remarks: Index number 8
I've cycled through the code with a break point and yours goes through 8 times showing 8 different fields. I can't find the Index number showing Manufacturer: Audi.
I have another app with 4 columns that works on the same principle and I need the text in the first column.
Re: How to get data from a ListView?
The first column is the .Text of the item... it has no index...
-tg
Re: How to get data from a ListView?
Quote:
Originally Posted by
Keithuk
What I was asking how come yours shows Audi and mine doesn't
As techgnome already said "The first column is the .Text of the item... it has no index... "
Therefore i added this line before the loop C = lvwEngine.SelectedItem.Text
So your code in post #10 should be
Code:
C = "Manufacturer: " & lvwEngine.SelectedItem.Text & _
" Mounting Time: " & lvwEngine.SelectedItem.SubItems(6) & _
" Model: " & lvwEngine.SelectedItem.SubItems(7) & _
" Engine Code: " & lvwEngine.SelectedItem.SubItems(1) & " " & _
lvwEngine.SelectedItem.SubItems(2) & _
"Kw " & lvwEngine.SelectedItem.SubItems(3) & _
"PS " & lvwEngine.SelectedItem.SubItems(4) & _
" Ltr " & lvwEngine.SelectedItem.SubItems(5) & _
" Cyls " & lvwEngine.SelectedItem.SubItems(8)
Re: How to get data from a ListView?
Its strange it doesn't have an index when all the rest do
Code:
C = "Manufacturer: " & lvwEngine.SelectedItem.Text & " Mounting Time: " & lvwEngine.SelectedItem.SubItems(6) & " Model: " & lvwEngine.SelectedItem.SubItems(7) & " Engine Code: " & lvwEngine.SelectedItem.SubItems(1) & " " & lvwEngine.SelectedItem.SubItems(2) & "Kw " & lvwEngine.SelectedItem.SubItems(3) & "PS " & lvwEngine.SelectedItem.SubItems(4) & " Ltr " & lvwEngine.SelectedItem.SubItems(5) & " Cyls " & lvwEngine.SelectedItem.SubItems(8)
That worked.
Thank you guys for you help, thread [RESOLVED] now. ;)
Re: [RESOLVED] How to get data from a ListView?
Why? The others are sub items... the first col is the text. It's never pretended to be anything else. It's not strange at all, because it's not the same thing.
-tg
Re: [RESOLVED] How to get data from a ListView?
Quote:
Originally Posted by
techgnome
Why? The others are sub items... the first col is the text. It's never pretended to be anything else. It's not strange at all, because it's not the same thing.
-tg
VB.NET considers both the same :D
ListView1.SelectedItems(0).SubItems(0).Text equals ListView1.SelectedItems(0).Text