none
Retaining the selection color(default blue Color) for the selected items in ListView when focus is lost RRS feed

  • Question

  • I am having a Listview in a form. I have added items(images) to the Listview. When i select the item(Image) in the listview , the item(image) is selected with Default selection Color(Blue). In the same form i have other form added to its container.When i tried to click on the controls of other form, the default selection color(blue) is changing to Grey color. For the lisview i am using property HideSelection = false. How i could maintain the default selection color(blue) even in focus leave of listview? Can any one please suggest me a solution or give me a piece of code.

    Thanks in Advance,

    Kumar

    vendredi 3 septembre 2010 13:22

Réponses

  • Here is some code from my own software that does basically the same thing you are asking for. I have a ListView that shows icons. The ListView is set to Tile mode, and the tile size is slightly larger than the icons so that you can see the selection color around the icon edges.

     Private Sub lvIcons_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles lvIcons.DrawItem
      e.DrawBackground()
    
      If e.Item.Selected Then
       e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
      End If
    
      'Draw centered image.
      Dim image As Image = e.Item.ImageList.Images(e.Item.ImageIndex)
      e.Graphics.DrawImage(image, CSng(e.Bounds.Left + (e.Bounds.Width - image.Width) / 2), CSng(e.Bounds.Top + (e.Bounds.Height - image.Height) / 2))
    
      'Draw focus rectangle.
      e.DrawFocusRectangle()
     End Sub
    
    
    Hope this helps.
    mercredi 15 septembre 2010 03:31
  • Hi mskumar.mcp,

    You could draw the selected item programmatically. Please take a look at the document below:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem.aspx

    Let me know if this helps or not.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    mardi 7 septembre 2010 14:50
    Modérateur

Toutes les réponses

  • Hi mskumar.mcp,

    You could draw the selected item programmatically. Please take a look at the document below:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem.aspx

    Let me know if this helps or not.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    mardi 7 septembre 2010 14:50
    Modérateur
  • Hi Aland Li,

                Thanks for your reply.

                 I have tried the given solution. It could not solve my problem. Actually with this solution the listview items are filled with Maroon color in rectangular form. But the actual problem is as follows

    I have added bitmap images to the listview. When the user clicks on that image, the image will be selected with blue color selection. When the focus is lost, immediately the image selection color changed to gray. So , how can i maintain the selection color of image in the listview?

     

    Thanks,

    mskumar.mcp

    lundi 13 septembre 2010 08:12
  • Hi mskumar.mcp,

    I mean you need to handle the DrawItem event to draw something to achieve your goal, but not completely follow the code snippet I shown above.

    In your case, you need to draw the background at first and then draw the image and text.

    Regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    lundi 13 septembre 2010 09:00
    Modérateur
  • Hi Aland Li,

               I tried above scenario, In that i am able to change fore color and back color of lisview item. But still image selection color i am not able to change in focus lost.

    Regards,

    mskumar.mcp

    lundi 13 septembre 2010 10:06
  • I am having a Listview in a form. I have added items(images) to the Listview. When i select the item(Image) in the listview , the item(image) is selected with Default selection Color(Blue). In the same form i have other form added to its container.When i tried to click on the controls of other form, the default selection color(blue) is changing to Grey color. For the lisview i am using property HideSelection = false. How i could maintain the default selection color(blue) even in focus leave of listview? Can any one please suggest me a solution or give me a piece of code.

    Thanks in Advance,

    mskumar.mcp

    • Fusionné Jing0Moderator jeudi 16 septembre 2010 10:34 Merge it to keep the same topic in one thread
    mardi 14 septembre 2010 11:29
  • I am having a Listview in a form. I have added items(images) to the Listview. When i select the item(Image) in the listview , the item(image) is selected with Default selection Color(Blue). In the same form i have other form added to its container.When i tried to click on the controls of other form, the default selection color(blue) is changing to Grey color. For the lisview i am using property HideSelection = false. How i could maintain the default selection color(blue) even in focus leave of listview? Can any one please suggest me a solution or give me a piece of code.

    Thanks in Advance,

    Kumar

    • Fusionné Jing0Moderator jeudi 16 septembre 2010 10:34 Merge it to keep the same topic in one thread
    mardi 14 septembre 2010 11:31
  • Can you post the code you are using so we can have a look?
    mardi 14 septembre 2010 14:09
  • Hello Kumar,

    please do not double post .. you already have an opened thread with the same question:

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/aee95fe6-2d91-4637-b5e7-4ebd3283991e/

     

    @moderator: please merge these threads 

     

    thanks, 
    Vladimir

    mardi 14 septembre 2010 15:04
  • Here is some code from my own software that does basically the same thing you are asking for. I have a ListView that shows icons. The ListView is set to Tile mode, and the tile size is slightly larger than the icons so that you can see the selection color around the icon edges.

     Private Sub lvIcons_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles lvIcons.DrawItem
      e.DrawBackground()
    
      If e.Item.Selected Then
       e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds)
      End If
    
      'Draw centered image.
      Dim image As Image = e.Item.ImageList.Images(e.Item.ImageIndex)
      e.Graphics.DrawImage(image, CSng(e.Bounds.Left + (e.Bounds.Width - image.Width) / 2), CSng(e.Bounds.Top + (e.Bounds.Height - image.Height) / 2))
    
      'Draw focus rectangle.
      e.DrawFocusRectangle()
     End Sub
    
    
    Hope this helps.
    mercredi 15 septembre 2010 03:31