Solved

Problem with listview multi selection

Posted on 2003-11-10
7
571 Views
Last Modified: 2012-05-04
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.
0
Comment
Question by:biao81
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 20 total points
ID: 9720328
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9720337
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
 
LVL 1

Expert Comment

by:JonothanTompson
ID: 9720363
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:biao81
ID: 9720374
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
 

Expert Comment

by:Gryff
ID: 9721180
Idle_mind,
I don't have the .backcolor method on my listview, you using common controls 6?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9722429
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
 

Expert Comment

by:Gryff
ID: 9722540
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now