none
retain blue background on listview selected item when focus leaves listview RRS feed

  • Question

  •  

    When I select an item in a listview, the selected item's background turns blue.

    When I click on a button on the same form, the background color becomes white, as it was before it was selected.

    Is there a way to retain that blue background color so that when I click on another control on the same form, that dark blue background will persist, so the user knows that is still the selected item?

     

    Here is what I have so far; This does persist Color.Navy to each of the selected items but the font turns from white to Navy as well, so I can't see the font (ie listitem text) anymore, it's one dark line.

    Public Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles  ListView1.SelectedIndexChanged

          If Not m_blnInChange Then

               m_blnInChange = True

              OnSelectedIndexChanged(Me, e)

              Dim i As ListViewItem

              For Each i In Me.ListView1.Items

                   i.BackColor = Color.White

              Next

              For Each i In Me.ListView1.SelectedItems

                  i.BackColor = Color.Navy

                  'Put some code in here to store the items that are selected

              Next

                  m_blnInChange = False

         End If

    End Sub

     

    Thank you,

    Greg

    Tuesday, January 29, 2008 12:03 AM

Answers

  • Hi Greg,

     

    The ListView class has provided a property "HideSelection", which gets or sets a value indicating whether the selected item in the control remains highlighted when the control loses focus.

     

    So you can set the HideSelection property of the ListView control to false to get what you want.

     

    Hope this helps.

     

    Linda

    Thursday, January 31, 2008 6:03 AM
  • Hi Greg,

     

    Thank you for your reply!

     

    To remain the lighlight color when a ListView losts focus, you can derive a new class from the ListView class and override the WndProc method. In the override WndProc method, ignore the WM_KILLFOCUS message.

     

    The following is a sample.

     

    Public Class ListViewExt
        Inherits ListView

     

        Public Sub New()
            Me.View = View.Details
        End Sub


        Dim WM_KILLFOCUS = &H8


        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg <> WM_KILLFOCUS Then
                MyBase.WndProc(m)
            End If
        End Sub
    End Class

     

    Use the derived ListView control on your form instead of the standard ListView control.

     

    Hope this helps.

    Linda

    Friday, February 1, 2008 8:48 AM

All replies

  • Hi Greg,

     

    The ListView class has provided a property "HideSelection", which gets or sets a value indicating whether the selected item in the control remains highlighted when the control loses focus.

     

    So you can set the HideSelection property of the ListView control to false to get what you want.

     

    Hope this helps.

     

    Linda

    Thursday, January 31, 2008 6:03 AM
  • Thank you Linda. That was very helpful.

    I wish there was a way to change that light grey highlighted but lost focus color to remain the same color blue as when it was selected. But this is still better than no selection indicator at all !

    Thank you once again Linda, You have been so helpful on many questions I have had.

    Greg

     

    Thursday, January 31, 2008 1:51 PM
  • Hi Greg,

     

    Thank you for your reply!

     

    To remain the lighlight color when a ListView losts focus, you can derive a new class from the ListView class and override the WndProc method. In the override WndProc method, ignore the WM_KILLFOCUS message.

     

    The following is a sample.

     

    Public Class ListViewExt
        Inherits ListView

     

        Public Sub New()
            Me.View = View.Details
        End Sub


        Dim WM_KILLFOCUS = &H8


        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg <> WM_KILLFOCUS Then
                MyBase.WndProc(m)
            End If
        End Sub
    End Class

     

    Use the derived ListView control on your form instead of the standard ListView control.

     

    Hope this helps.

    Linda

    Friday, February 1, 2008 8:48 AM
  • Linda

     

    Thanks for the info so far it has been extemely helpful.  Once I have created the Class ListViewExt, how do I ensure that the instance I create has a particular panel object as its parent?  I normally achieve this by dragging the required component onto the panel in the designer which I don't believe I can do if I generate the instance in code.

     

    Thanks

     

    Chris

     

    Friday, June 27, 2008 4:26 PM
  • Hi Chris,

     

    > I normally achieve this by dragging the required component onto the panel in the designer which I don't believe I can do if I generate the instance in code.

     

    Add the component to the Controls collection of the Panel at run time. For example:

    ListViewEx listviewex = new ListViewEx();

    panel1.Controls.Add(listviewex);

     

    Hope this helps.

     

    Linda

    Friday, July 11, 2008 8:20 AM