Solved

Problem with listview multi selection

Posted on 2003-11-10
7
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 86

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 86

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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

726 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