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.
Last edited by Keithuk; Oct 27th, 2016 at 07:25 AM.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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
I'm using Microsoft Windows Common Control 5.0 (SP2) for the ListView.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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
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.
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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
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
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.
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
Last edited by Anees; Oct 15th, 2016 at 03:48 AM.
Please rate my post if it is useful.
Do not forget to mark the thread as resolved if you got what you are looking for. Create new threads instead of personal messages.It may help others too.
Sorry for the late reply, I've been busy with a lot of other things.
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.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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
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.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
Thank you guys for you help, thread [RESOLVED] now.
Keith
I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.
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.
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.