By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,190 Members | 2,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,190 IT Pros & Developers. It's quick & easy.

ListView only recognizes Items, how about SubItems?

P: n/a
Hi. I use a ListView to display data in tabular form.

Each ListView row corresponds to a data record.
The ListView Item of the record is the record key or code.
Each SubItem in that row represents a field from the record.

I implemented a context menu so the user can "Copy" the cell content to the
clipboard with this code:
Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
'Displays a context menu with the option to "Copy" to the clipboard.

'Activate context menu if user right-clicked.
If e.Button = MouseButtons.Right Then
Dim ClickPoint As Point = New Point(e.X, e.Y)
Dim LVItem As ListViewItem = ListView1.GetItemAt(ClickPoint.X,
ClickPoint.Y)

'Copy data only if item contains some data.
If Not LVItem Is Nothing Then
'Code to copy to ciipboard...
End If
End If
End Sub

The problem is that GetItemAt() returns Nothing if the click happened on a
SubItem, that is, any column but the leftmost.
How can I allow the users to copy the data from the other columns to the
clipboard?

Thanks in advance

Richard
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I've seen people having problems with this. In theory, you should get the
ListView Item even when you click on the subitems (as per documentation of
the GetItemAt method). You can try one of these:

1. Try putting your code in the mouseup event instead of the mouse down
event.

2. If you absolutely want it in the mousedown event, you could do something
like this:

Dim p As Point = ListView1.PointToClient( _
New Point(Cursor.Position.X, Cursor.Position.Y))

Dim LVItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)

hope that helps..
Imran.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
Hi. I use a ListView to display data in tabular form.

Each ListView row corresponds to a data record.
The ListView Item of the record is the record key or code.
Each SubItem in that row represents a field from the record.

I implemented a context menu so the user can "Copy" the cell content to the clipboard with this code:
Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
'Displays a context menu with the option to "Copy" to the clipboard.
'Activate context menu if user right-clicked.
If e.Button = MouseButtons.Right Then
Dim ClickPoint As Point = New Point(e.X, e.Y)
Dim LVItem As ListViewItem = ListView1.GetItemAt(ClickPoint.X,
ClickPoint.Y)

'Copy data only if item contains some data.
If Not LVItem Is Nothing Then
'Code to copy to ciipboard...
End If
End If
End Sub

The problem is that GetItemAt() returns Nothing if the click happened on a
SubItem, that is, any column but the leftmost.
How can I allow the users to copy the data from the other columns to the
clipboard?

Thanks in advance

Richard

Jul 21 '05 #2

P: n/a
Thank you Imran, but this is not a viable solution.
It seems that ListView1.GetItemAt() method only returns something when
clicked on the Item (the 1st column of the ListView). When a subitem (any
other column) is clicked, It returns Nothing.

Thank you,

Richard

"Imran Koradia" wrote:
I've seen people having problems with this. In theory, you should get the
ListView Item even when you click on the subitems (as per documentation of
the GetItemAt method). You can try one of these:

1. Try putting your code in the mouseup event instead of the mouse down
event.

2. If you absolutely want it in the mousedown event, you could do something
like this:

Dim p As Point = ListView1.PointToClient( _
New Point(Cursor.Position.X, Cursor.Position.Y))

Dim LVItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)

hope that helps..
Imran.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
Hi. I use a ListView to display data in tabular form.

Each ListView row corresponds to a data record.
The ListView Item of the record is the record key or code.
Each SubItem in that row represents a field from the record.

I implemented a context menu so the user can "Copy" the cell content to

the
clipboard with this code:
Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
'Displays a context menu with the option to "Copy" to the

clipboard.

'Activate context menu if user right-clicked.
If e.Button = MouseButtons.Right Then
Dim ClickPoint As Point = New Point(e.X, e.Y)
Dim LVItem As ListViewItem = ListView1.GetItemAt(ClickPoint.X,
ClickPoint.Y)

'Copy data only if item contains some data.
If Not LVItem Is Nothing Then
'Code to copy to ciipboard...
End If
End If
End Sub

The problem is that GetItemAt() returns Nothing if the click happened on a
SubItem, that is, any column but the leftmost.
How can I allow the users to copy the data from the other columns to the
clipboard?

Thanks in advance

Richard


Jul 21 '05 #3

P: n/a
There is a property (don't have VS where I am) that will determine when the
GUI will respond to a mouse click to select an item. If you don't set this
the listview item will only be selected if you click the first column. I
would bet that the GetItemAt will respond in the same manner. Set this
property to true and I think you will get what you are looking for.

Lloyd Sheen

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
Thank you Imran, but this is not a viable solution.
It seems that ListView1.GetItemAt() method only returns something when
clicked on the Item (the 1st column of the ListView). When a subitem (any
other column) is clicked, It returns Nothing.

Thank you,

Richard

"Imran Koradia" wrote:
I've seen people having problems with this. In theory, you should get the
ListView Item even when you click on the subitems (as per documentation
of
the GetItemAt method). You can try one of these:

1. Try putting your code in the mouseup event instead of the mouse down
event.

2. If you absolutely want it in the mousedown event, you could do
something
like this:

Dim p As Point = ListView1.PointToClient( _
New Point(Cursor.Position.X, Cursor.Position.Y))

Dim LVItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)

hope that helps..
Imran.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
> Hi. I use a ListView to display data in tabular form.
>
> Each ListView row corresponds to a data record.
> The ListView Item of the record is the record key or code.
> Each SubItem in that row represents a field from the record.
>
> I implemented a context menu so the user can "Copy" the cell content to

the
> clipboard with this code:
>
>
> Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
> 'Displays a context menu with the option to "Copy" to the

clipboard.
>
> 'Activate context menu if user right-clicked.
> If e.Button = MouseButtons.Right Then
> Dim ClickPoint As Point = New Point(e.X, e.Y)
> Dim LVItem As ListViewItem =
> ListView1.GetItemAt(ClickPoint.X,
> ClickPoint.Y)
>
> 'Copy data only if item contains some data.
> If Not LVItem Is Nothing Then
> 'Code to copy to ciipboard...
> End If
> End If
> End Sub
>
> The problem is that GetItemAt() returns Nothing if the click happened
> on a
> SubItem, that is, any column but the leftmost.
> How can I allow the users to copy the data from the other columns to
> the
> clipboard?
>
> Thanks in advance
>
> Richard


Jul 21 '05 #4

P: n/a
Property is FullRowSelect

Lloyd Sheen

"Lloyd Sheen" <sq*******************@tostopspamhotmail.com> wrote in message
news:oI***************@news04.bloor.is.net.cable.r ogers.com...
There is a property (don't have VS where I am) that will determine when
the GUI will respond to a mouse click to select an item. If you don't set
this the listview item will only be selected if you click the first
column. I would bet that the GetItemAt will respond in the same manner.
Set this property to true and I think you will get what you are looking
for.

Lloyd Sheen

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
Thank you Imran, but this is not a viable solution.
It seems that ListView1.GetItemAt() method only returns something when
clicked on the Item (the 1st column of the ListView). When a subitem (any
other column) is clicked, It returns Nothing.

Thank you,

Richard

"Imran Koradia" wrote:
I've seen people having problems with this. In theory, you should get
the
ListView Item even when you click on the subitems (as per documentation
of
the GetItemAt method). You can try one of these:

1. Try putting your code in the mouseup event instead of the mouse down
event.

2. If you absolutely want it in the mousedown event, you could do
something
like this:

Dim p As Point = ListView1.PointToClient( _
New Point(Cursor.Position.X, Cursor.Position.Y))

Dim LVItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)

hope that helps..
Imran.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
> Hi. I use a ListView to display data in tabular form.
>
> Each ListView row corresponds to a data record.
> The ListView Item of the record is the record key or code.
> Each SubItem in that row represents a field from the record.
>
> I implemented a context menu so the user can "Copy" the cell content
> to
the
> clipboard with this code:
>
>
> Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
> 'Displays a context menu with the option to "Copy" to the
clipboard.
>
> 'Activate context menu if user right-clicked.
> If e.Button = MouseButtons.Right Then
> Dim ClickPoint As Point = New Point(e.X, e.Y)
> Dim LVItem As ListViewItem =
> ListView1.GetItemAt(ClickPoint.X,
> ClickPoint.Y)
>
> 'Copy data only if item contains some data.
> If Not LVItem Is Nothing Then
> 'Code to copy to ciipboard...
> End If
> End If
> End Sub
>
> The problem is that GetItemAt() returns Nothing if the click happened
> on a
> SubItem, that is, any column but the leftmost.
> How can I allow the users to copy the data from the other columns to
> the
> clipboard?
>
> Thanks in advance
>
> Richard


Jul 21 '05 #5

P: n/a
Richard,

I believe Lloyd's answer should solve your problem. I looked in my code and
I've been using the same code you posted without any problems. I guess I
overlooked the fact that FullRowSelect was True in my case - I just assumed
you would have that set to True since that's how we are accustomed to using
listviews. You should set this property to True (from the designer or at
runtime - whichever) which will ensure that the entire row is highlighted
and then you should be able to get the listview item by clicking anywhere on
that row.

Imran.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
Thank you Imran, but this is not a viable solution.
It seems that ListView1.GetItemAt() method only returns something when
clicked on the Item (the 1st column of the ListView). When a subitem (any
other column) is clicked, It returns Nothing.

Thank you,

Richard

"Imran Koradia" wrote:
I've seen people having problems with this. In theory, you should get the
ListView Item even when you click on the subitems (as per documentation
of
the GetItemAt method). You can try one of these:

1. Try putting your code in the mouseup event instead of the mouse down
event.

2. If you absolutely want it in the mousedown event, you could do
something
like this:

Dim p As Point = ListView1.PointToClient( _
New Point(Cursor.Position.X, Cursor.Position.Y))

Dim LVItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)

hope that helps..
Imran.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
> Hi. I use a ListView to display data in tabular form.
>
> Each ListView row corresponds to a data record.
> The ListView Item of the record is the record key or code.
> Each SubItem in that row represents a field from the record.
>
> I implemented a context menu so the user can "Copy" the cell content to

the
> clipboard with this code:
>
>
> Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
> 'Displays a context menu with the option to "Copy" to the

clipboard.
>
> 'Activate context menu if user right-clicked.
> If e.Button = MouseButtons.Right Then
> Dim ClickPoint As Point = New Point(e.X, e.Y)
> Dim LVItem As ListViewItem =
> ListView1.GetItemAt(ClickPoint.X,
> ClickPoint.Y)
>
> 'Copy data only if item contains some data.
> If Not LVItem Is Nothing Then
> 'Code to copy to ciipboard...
> End If
> End If
> End Sub
>
> The problem is that GetItemAt() returns Nothing if the click happened
> on a
> SubItem, that is, any column but the leftmost.
> How can I allow the users to copy the data from the other columns to
> the
> clipboard?
>
> Thanks in advance
>
> Richard


Jul 21 '05 #6

P: n/a
Thank you Lloyd, you are right, with FullRowSelect = True the Item becomes
the whole line and it recognizes the click. In order to find the subitem that
was clicked, I suppose I will have to calculate the width of each column in
pixels. Is that correct?

Thank you

Richard

"Lloyd Sheen" wrote:
Property is FullRowSelect

Lloyd Sheen

"Lloyd Sheen" <sq*******************@tostopspamhotmail.com> wrote in message
news:oI***************@news04.bloor.is.net.cable.r ogers.com...
There is a property (don't have VS where I am) that will determine when
the GUI will respond to a mouse click to select an item. If you don't set
this the listview item will only be selected if you click the first
column. I would bet that the GetItemAt will respond in the same manner.
Set this property to true and I think you will get what you are looking
for.

Lloyd Sheen

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:C0**********************************@microsof t.com...
Thank you Imran, but this is not a viable solution.
It seems that ListView1.GetItemAt() method only returns something when
clicked on the Item (the 1st column of the ListView). When a subitem (any
other column) is clicked, It returns Nothing.

Thank you,

Richard

"Imran Koradia" wrote:

I've seen people having problems with this. In theory, you should get
the
ListView Item even when you click on the subitems (as per documentation
of
the GetItemAt method). You can try one of these:

1. Try putting your code in the mouseup event instead of the mouse down
event.

2. If you absolutely want it in the mousedown event, you could do
something
like this:

Dim p As Point = ListView1.PointToClient( _
New Point(Cursor.Position.X, Cursor.Position.Y))

Dim LVItem As ListViewItem = ListView1.GetItemAt(p.X, p.Y)

hope that helps..
Imran.

"Richard" <Ri*****@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
> Hi. I use a ListView to display data in tabular form.
>
> Each ListView row corresponds to a data record.
> The ListView Item of the record is the record key or code.
> Each SubItem in that row represents a field from the record.
>
> I implemented a context menu so the user can "Copy" the cell content
> to
the
> clipboard with this code:
>
>
> Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
> 'Displays a context menu with the option to "Copy" to the
clipboard.
>
> 'Activate context menu if user right-clicked.
> If e.Button = MouseButtons.Right Then
> Dim ClickPoint As Point = New Point(e.X, e.Y)
> Dim LVItem As ListViewItem =
> ListView1.GetItemAt(ClickPoint.X,
> ClickPoint.Y)
>
> 'Copy data only if item contains some data.
> If Not LVItem Is Nothing Then
> 'Code to copy to ciipboard...
> End If
> End If
> End Sub
>
> The problem is that GetItemAt() returns Nothing if the click happened
> on a
> SubItem, that is, any column but the leftmost.
> How can I allow the users to copy the data from the other columns to
> the
> clipboard?
>
> Thanks in advance
>
> Richard



Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.