Problem with listview multi selection

I would like to ask 2 question regarding listview.

Firstly, let's say i have a listview name lvwProduct, how do i programatically make a row in listiview being selected and highligted as well, possibly set the color of the highlight as well if feasible.

Secondly, after i set the property of listview to enable multi row selection, I would like to get the key with lvwProduct.selectedItem.key, but it always return me the last item in my selection. I would like to delete all the selected item, but i just can't get all the selected item's key. It supposed to be an array rith? what is the syntax?

thank you.
biao81Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Here you go...

' Programmatically select an item... first item is selected here
Set ListView1.SelectedItem = ListView1.ListItems(1)

' Determine which items are selected when multiselect is enabled...
If ListView1.SelectedItem Is Nothing Then Exit Sub
   For i = 1 To ListView1.ListItems.Count
      If ListView1.ListItems(i).Selected = True Then
         Debug.Print ListView1.ListItems(i).Key
      End If
   Next i
End Sub

Regards,

Idle_Mind
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Also, you can change the color of an item like this...

ListView1.ListItems(1).BackColor = QBColor(Rnd * 15)
ListView1.ListItems(1).ForeColor = QBColor(Rnd * 10)

You probably don't want a random color though.  =)
0
JonothanTompsonCommented:
To select a row in a listview in code you do this by
Set lvwProduct.SelectedItem = lvwProduct.ListItems(2) '2 being the item in which you wish to select

Sorry can find out how to do the second question
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

biao81Author Commented:
Idle_Mind, i ur code provided here should be working, anyway, i haven't tested it, will test it out within these two days and reward the marks to u if it works, cos i'm busy with other thing.

thnx
0
GryffCommented:
Idle_mind,
I don't have the .backcolor method on my listview, you using common controls 6?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
I am using Common Controls 6.0 (SP6).

I erred slightly though.  I have BackColor and ForeColor on my ListView, but only ForeColor on a ListItem.
0
GryffCommented:
I have something that may help you with colouring a line, however be aware it s not perfect, you may be able to improve it significantly. It is something I barstardised from http://www.mvps.org/vbnet/index.html?code/comctl/lvledger.htm  so most of the credit is due to that website.

The bit that chooses which line is reproduced immediately below, followed by the code as a whole. You will need to adjust the controls and whatnot to reflect your project, and you need a picturebox control. I would make the picture box hidden as it is just used to work stuff(tm) out. If you are repeatedly changeing lines then you may encounter some memory problems as I never managed to debug this section properly. If anyone tracks down where I made the mistake I would be grateful to you pointing it out.

In this code you should be able to see the bit that 'does' I was using it to highlight certain lines based on the value intphase.
        For Each Itmx In Fluidy.LstChar.ListItems
            If Itmx.SubItems(2) = IntPhase Then
                IntNum = Itmx.Index - 1
                Fluidy.Picture1.Line (0, (iBarHeight * _
                    IntNum))-(lBarWidth, (iBarHeight * _
                    (IntNum + 1))), Bar1Color, BF
            End If
        Next Itmx


Public Enum HiColour
  vbledgerWhite = &HF9FEFF
  vbLedgerGreen = &HD0FFCC
  vbLedgerYellow = &HE1FAFF
  vbLedgerRed = &HE1E1FF
  vbLedgerGrey = &HE0E0E0
  vbLedgerBeige = &HD9F2F7
  vbLedgerSoftWhite = &HF7F7F7
  vbledgerPureWhite = &HFFFFFF
End Enum

Public Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Public Sub SetListHiLite(lv As ListView, _
                              Bar1Color As HiColour)

   Dim iBarHeight  As Long  '/* height of 1 line in the listview
   Dim lBarWidth   As Long  '/* width of listview
   Dim diff        As Long  '/* used in calculations of row height
   Dim twipsy      As Long  '/* variable holding Screen.TwipsPerPicture1elY
   Dim Itmx        As ListItem
   Dim IntNum      As Integer
   
   iBarHeight = 0
   lBarWidth = 0
   diff = 0
   
   On Local Error GoTo SetListViewColor_Error
   
   twipsy = Screen.TwipsPerPixelY
   If lv.View = lvwReport Then
   
     '/* set up the listview properties
      With lv
        .Picture = Nothing  '/* clear picture
        .Refresh
        .Visible = 1
        .PictureAlignment = 0 ' not lvwTile
        lBarWidth = .Width
      End With  ' lv
       
     '/* set up the picture box properties
      With Fluidy.Picture1
         .AutoRedraw = False       '/* clear/reset picture
         .Picture = Nothing
         .BackColor = vbWhite
         .Height = 1
         .AutoRedraw = True        '/* assure image draws
         .BorderStyle = vbBSNone   '/* other attributes
         .ScaleMode = vbTwips
         '.Top = Form1.Top - 10000  '/* move it way off screen
         .Width = Screen.Width
         .Visible = False
         .Font = lv.Font           '/* assure Picture1 font matched listview font
         
        '/* match picture box font properties
        '/* with those of listview
         With .Font
            .Bold = lv.Font.Bold
            .Charset = lv.Font.Charset
            .Italic = lv.Font.Italic
            .Name = lv.Font.Name
            .Strikethrough = lv.Font.Strikethrough
            .Underline = lv.Font.Underline
            .Weight = lv.Font.Weight
            .Size = lv.Font.Size
         End With  'Picture1.Font
         
        '/* here we calculate the height of each
        '/* bar in the listview. Several things
        '/*  can affect this height - the use
        '/* of item icons, the size of those icons,
        '/* the use of checkboxes and so on through
        '/* all the permutations.
        '/*
       
       '/* used by all sizing routines
         iBarHeight = .TextHeight("W")

              '/* 2. text with checkboxes: add to textheight the
              '/*    difference between 18 Pixels and iBarHeight
              '/*    all calculated initially in Pixels,
              '/*    then converted to twips
               If (iBarHeight \ twipsy) > 16 Then
                  iBarHeight = iBarHeight + twipsy
               Else
                  diff = 16 - (iBarHeight \ twipsy)
                  iBarHeight = iBarHeight + (diff * twipsy) + (twipsy * 1)
               End If
               
'         End Select
     
         .Height = iBarHeight * IntCharNum  'Set this to be end of selected characters
         .Width = lBarWidth
         
                ' * number is Line you want to highlight
        For Each Itmx In Fluidy.LstChar.ListItems
            If Itmx.SubItems(2) = IntPhase Then
                IntNum = Itmx.Index - 1
                Fluidy.Picture1.Line (0, (iBarHeight * _
                    IntNum))-(lBarWidth, (iBarHeight * _
                    (IntNum + 1))), Bar1Color, BF
            End If
        Next Itmx

         .AutoSize = True
         .Refresh
         
      End With  'Picture1
     
     '/* set the lv picture to the
     '/* Picture1 image
     
      lv.Refresh
      lv.Picture = Fluidy.Picture1.Image
     
   Else
   
      lv.Picture = Nothing
       
   End If  'lv.View = lvwReport

SetListViewColor_Exit:
On Local Error GoTo 0
Exit Sub
   
SetListViewColor_Error:

  '/* clear the listview's picture and exit
   With lv
      .Picture = Nothing
      .Refresh
   End With
   
   Resume SetListViewColor_Exit
   
End Sub


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.