ListView only recognizes Items, how about SubItems?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard

    #1

    ListView only recognizes Items, how about SubItems?

    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_Mouse Down(ByVal sender As Object, ByVal e As
    System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down
    'Displays a context menu with the option to "Copy" to the clipboard.

    'Activate context menu if user right-clicked.
    If e.Button = MouseButtons.Ri ght Then
    Dim ClickPoint As Point = New Point(e.X, e.Y)
    Dim LVItem As ListViewItem = ListView1.GetIt emAt(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
  • Imran Koradia

    #2
    Re: ListView only recognizes Items, how about SubItems?

    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.Point ToClient( _
    New Point(Cursor.Po sition.X, Cursor.Position .Y))

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

    hope that helps..
    Imran.

    "Richard" <Richard@discus sions.microsoft .com> wrote in message
    news:E29894BD-9CA9-4154-A088-2A1405893230@mi crosoft.com...[color=blue]
    > 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[/color]
    the[color=blue]
    > clipboard with this code:
    >
    >
    > Private Sub ListView1_Mouse Down(ByVal sender As Object, ByVal e As
    > System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down
    > 'Displays a context menu with the option to "Copy" to the[/color]
    clipboard.[color=blue]
    >
    > 'Activate context menu if user right-clicked.
    > If e.Button = MouseButtons.Ri ght Then
    > Dim ClickPoint As Point = New Point(e.X, e.Y)
    > Dim LVItem As ListViewItem = ListView1.GetIt emAt(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[/color]


    Comment

    • Richard

      #3
      Re: ListView only recognizes Items, how about SubItems?

      Thank you Imran, but this is not a viable solution.
      It seems that ListView1.GetIt emAt() 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:
      [color=blue]
      > 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.Point ToClient( _
      > New Point(Cursor.Po sition.X, Cursor.Position .Y))
      >
      > Dim LVItem As ListViewItem = ListView1.GetIt emAt(p.X, p.Y)
      >
      > hope that helps..
      > Imran.
      >
      > "Richard" <Richard@discus sions.microsoft .com> wrote in message
      > news:E29894BD-9CA9-4154-A088-2A1405893230@mi crosoft.com...[color=green]
      > > 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[/color]
      > the[color=green]
      > > clipboard with this code:
      > >
      > >
      > > Private Sub ListView1_Mouse Down(ByVal sender As Object, ByVal e As
      > > System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down
      > > 'Displays a context menu with the option to "Copy" to the[/color]
      > clipboard.[color=green]
      > >
      > > 'Activate context menu if user right-clicked.
      > > If e.Button = MouseButtons.Ri ght Then
      > > Dim ClickPoint As Point = New Point(e.X, e.Y)
      > > Dim LVItem As ListViewItem = ListView1.GetIt emAt(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[/color]
      >
      >
      >[/color]

      Comment

      • Lloyd Sheen

        #4
        Re: ListView only recognizes Items, how about SubItems?

        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" <Richard@discus sions.microsoft .com> wrote in message
        news:C0B7E1EC-DD17-4B9D-98AE-095B48ADB664@mi crosoft.com...[color=blue]
        > Thank you Imran, but this is not a viable solution.
        > It seems that ListView1.GetIt emAt() 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:
        >[color=green]
        >> 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.Point ToClient( _
        >> New Point(Cursor.Po sition.X, Cursor.Position .Y))
        >>
        >> Dim LVItem As ListViewItem = ListView1.GetIt emAt(p.X, p.Y)
        >>
        >> hope that helps..
        >> Imran.
        >>
        >> "Richard" <Richard@discus sions.microsoft .com> wrote in message
        >> news:E29894BD-9CA9-4154-A088-2A1405893230@mi crosoft.com...[color=darkred]
        >> > 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[/color]
        >> the[color=darkred]
        >> > clipboard with this code:
        >> >
        >> >
        >> > Private Sub ListView1_Mouse Down(ByVal sender As Object, ByVal e As
        >> > System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down
        >> > 'Displays a context menu with the option to "Copy" to the[/color]
        >> clipboard.[color=darkred]
        >> >
        >> > 'Activate context menu if user right-clicked.
        >> > If e.Button = MouseButtons.Ri ght Then
        >> > Dim ClickPoint As Point = New Point(e.X, e.Y)
        >> > Dim LVItem As ListViewItem =
        >> > ListView1.GetIt emAt(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[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Lloyd Sheen

          #5
          Re: ListView only recognizes Items, how about SubItems?

          Property is FullRowSelect

          Lloyd Sheen

          "Lloyd Sheen" <sqlguyremoveal lofthis@tostops pamhotmail.com> wrote in message
          news:oI06d.133$ zFA1.96@news04. bloor.is.net.ca ble.rogers.com. ..[color=blue]
          > 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" <Richard@discus sions.microsoft .com> wrote in message
          > news:C0B7E1EC-DD17-4B9D-98AE-095B48ADB664@mi crosoft.com...[color=green]
          >> Thank you Imran, but this is not a viable solution.
          >> It seems that ListView1.GetIt emAt() 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:
          >>[color=darkred]
          >>> 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.Point ToClient( _
          >>> New Point(Cursor.Po sition.X, Cursor.Position .Y))
          >>>
          >>> Dim LVItem As ListViewItem = ListView1.GetIt emAt(p.X, p.Y)
          >>>
          >>> hope that helps..
          >>> Imran.
          >>>
          >>> "Richard" <Richard@discus sions.microsoft .com> wrote in message
          >>> news:E29894BD-9CA9-4154-A088-2A1405893230@mi crosoft.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_Mouse Down(ByVal sender As Object, ByVal e As
          >>> > System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down
          >>> > 'Displays a context menu with the option to "Copy" to the
          >>> clipboard.
          >>> >
          >>> > 'Activate context menu if user right-clicked.
          >>> > If e.Button = MouseButtons.Ri ght Then
          >>> > Dim ClickPoint As Point = New Point(e.X, e.Y)
          >>> > Dim LVItem As ListViewItem =
          >>> > ListView1.GetIt emAt(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
          >>>
          >>>
          >>>[/color][/color]
          >
          >[/color]


          Comment

          • Imran Koradia

            #6
            Re: ListView only recognizes Items, how about SubItems?

            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" <Richard@discus sions.microsoft .com> wrote in message
            news:C0B7E1EC-DD17-4B9D-98AE-095B48ADB664@mi crosoft.com...[color=blue]
            > Thank you Imran, but this is not a viable solution.
            > It seems that ListView1.GetIt emAt() 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:
            >[color=green]
            >> 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.Point ToClient( _
            >> New Point(Cursor.Po sition.X, Cursor.Position .Y))
            >>
            >> Dim LVItem As ListViewItem = ListView1.GetIt emAt(p.X, p.Y)
            >>
            >> hope that helps..
            >> Imran.
            >>
            >> "Richard" <Richard@discus sions.microsoft .com> wrote in message
            >> news:E29894BD-9CA9-4154-A088-2A1405893230@mi crosoft.com...[color=darkred]
            >> > 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[/color]
            >> the[color=darkred]
            >> > clipboard with this code:
            >> >
            >> >
            >> > Private Sub ListView1_Mouse Down(ByVal sender As Object, ByVal e As
            >> > System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down
            >> > 'Displays a context menu with the option to "Copy" to the[/color]
            >> clipboard.[color=darkred]
            >> >
            >> > 'Activate context menu if user right-clicked.
            >> > If e.Button = MouseButtons.Ri ght Then
            >> > Dim ClickPoint As Point = New Point(e.X, e.Y)
            >> > Dim LVItem As ListViewItem =
            >> > ListView1.GetIt emAt(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[/color]
            >>
            >>
            >>[/color][/color]


            Comment

            • Richard

              #7
              Re: ListView only recognizes Items, how about SubItems?

              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:
              [color=blue]
              > Property is FullRowSelect
              >
              > Lloyd Sheen
              >
              > "Lloyd Sheen" <sqlguyremoveal lofthis@tostops pamhotmail.com> wrote in message
              > news:oI06d.133$ zFA1.96@news04. bloor.is.net.ca ble.rogers.com. ..[color=green]
              > > 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" <Richard@discus sions.microsoft .com> wrote in message
              > > news:C0B7E1EC-DD17-4B9D-98AE-095B48ADB664@mi crosoft.com...[color=darkred]
              > >> Thank you Imran, but this is not a viable solution.
              > >> It seems that ListView1.GetIt emAt() 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.Point ToClient( _
              > >>> New Point(Cursor.Po sition.X, Cursor.Position .Y))
              > >>>
              > >>> Dim LVItem As ListViewItem = ListView1.GetIt emAt(p.X, p.Y)
              > >>>
              > >>> hope that helps..
              > >>> Imran.
              > >>>
              > >>> "Richard" <Richard@discus sions.microsoft .com> wrote in message
              > >>> news:E29894BD-9CA9-4154-A088-2A1405893230@mi crosoft.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_Mouse Down(ByVal sender As Object, ByVal e As
              > >>> > System.Windows. Forms.MouseEven tArgs) Handles ListView1.Mouse Down
              > >>> > 'Displays a context menu with the option to "Copy" to the
              > >>> clipboard.
              > >>> >
              > >>> > 'Activate context menu if user right-clicked.
              > >>> > If e.Button = MouseButtons.Ri ght Then
              > >>> > Dim ClickPoint As Point = New Point(e.X, e.Y)
              > >>> > Dim LVItem As ListViewItem =
              > >>> > ListView1.GetIt emAt(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
              > >>>
              > >>>
              > >>>[/color]
              > >
              > >[/color]
              >
              >
              >[/color]

              Comment

              Working...