ron nash

Hi,

I have a List view control with a few items listed. My question is "How can I prevent the selected item from having the blue background".

I can mask the issue by removing focus from the selected item as soon as it is clicked, however, there must be away of disabling this feature.



Re: Visual Basic Express Edition list view selected item blue background

nobugz

Check the MSDN library example for the ListView.DrawItem event. Just omit the FillRectangle() method call.





Re: Visual Basic Express Edition list view selected item blue background

ron nash

Nobugz,

Can you please help with the following:

I have set the listview ' ownerdraw ' property to true which allows me to code the listview.drawitem method to set/remove the background etc.

I have also set the view property to smallicon. With the ownerdraw property set to true the listview will not display the images that are in the coded image list. If I set to false they show, but with the blue background on the selected item that I need to remove

Here is the code for my test example.

Private Sub setlistitems()

' Create ImageList objects.

Dim imageListSmall As New ImageList()

 

' Initialize the ImageList objects with bitmaps.

imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))

ListAccounts.SmallImageList = imageListSmall

' Adds a new group that has a left-aligned header

ListAccounts.Groups.Add(New ListViewGroup("Clients", HorizontalAlignment.Left))

' Adds the first item to the first group

ListAccounts.Items.Add("Add Client", 0).Group = ListAccounts.Groups(0)

End Sub





Re: Visual Basic Express Edition list view selected item blue background

ron nash

Can anyone help with the above issue please





Re: Visual Basic Express Edition list view selected item blue background

Dave299

Ron

Dumb question maybe - are you actually drawing the images in your DrawItem event If so could you post the code.





Re: Visual Basic Express Edition list view selected item blue background

ron nash

Cheers Dave  "Dumb Question"

 

No i am not drawing the image in the DrawItem event, should I be if yes perhaps you could show me how this can be done.

I am using a imagelist and setting the listview items image by its index number.

Code that sets my listview items and images

Private Sub setlistitems()

' Create two ImageList objects.

Dim imageListSmall As New ImageList()

' Initialize the ImageList objects with bitmaps.

Dim newimage0 As Image = IFA.My.Resources.Resources.UpdatAcc

Dim newimage1 As Image = IFA.My.Resources.Resources.Search_48_n_p

Dim newimage2 As Image = IFA.My.Resources.Resources.applications_321

imageListSmall.Images.Add(newimage0)

imageListSmall.Images.Add(newimage1)

imageListSmall.Images.Add(newimage2)

ListAdmin.SmallImageList = imageListSmall

ListAccounts.SmallImageList = imageListSmall

 

' Adds a new group that has a left-aligned header

ListAdmin.Groups.Add(New ListViewGroup("Admin", HorizontalAlignment.Left))

ListAccounts.Groups.Add(New ListViewGroup("Clients", HorizontalAlignment.Left))

' Adds the first item to the first group

ListAdmin.Items.Add("Edit Mortgages", 2).Group = ListAdmin.Groups(0)

ListAdmin.Items.Add("Edit Insurance", 2).Group = ListAdmin.Groups(0)

'\\ ListAccounts items

ListAccounts.Items.Add("Add Client", 0).Group = ListAccounts.Groups(0)

ListAccounts.Items.Add("Update Client", 0).Group = ListAccounts.Groups(0)

ListAccounts.Items.Add("Client Search", 1).Group = ListAccounts.Groups(0)

End Sub

The following code is just used to remove the blue background when the listview item is first sellected. The blue background is only displayed when the item is first clicked as focus is then removed from the listview which removes the background. I have also coded to underline the selected item in the list.

Private Sub ListAccounts_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles ListAccounts.DrawItem

If Not Me.ListAccounts.View = View.Details Then

e.DrawText()

End If

End Sub





Re: Visual Basic Express Edition list view selected item blue background

ron nash

Sorry if this is yet another Dumb question Dave

I have found the way to add the image within the Drawitem event.

i would perfer to not do it this way as It requires more coding I need to work out the x,y for each list item to align the image. Much easier the other way.

Could you please confirm that, if by setting the ownerdraw property to true, imagelists cannot be used

Private Sub ListAccounts_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles ListAccounts.DrawItem

Dim newimage0 As Image = IFA.My.Resources.Resources.UpdatAcc

If Not Me.ListAccounts.View = View.Details Then

e.Graphics.DrawImage(newimage0, 10, 1, 15, 15)

e.DrawText()

End If

End Sub





Re: Visual Basic Express Edition list view selected item blue background

Dave299

Ron

I didn't mean your question was dumb - I was referring to mine, expecting you to say "of course I'm drawing the image".

However, I've only used OwnerDraw in relation to ListBoxes but the principles look very similar.

The DrawListViewItemEventArgs contains all the info you need to postion the image and the associated text.

The following will draw the image at the left hand edge of the item - it seems a bit convoluted, so there may be a better way of doing it.

e.Graphics.DrawImage(ListAccounts.SmallImageList.Images(e.Item.ImageIndex), e.Bounds.X, e.Bounds.Y)

You can then draw the text using something like this (again it looks a bit convoluted)

e.Graphics.DrawString(e.Item.Text, New Font("Arial", 12, FontStyle.Bold), Brushes.Red, e.Bounds.X + ListAccounts.SmallImageList.ImageSize.Width + 20, e.Bounds.Y)

Dave





Re: Visual Basic Express Edition list view selected item blue background

ron nash

Cheers Dave,

With only little modification, works great. Infact it's simpifies all my coding for my multiple listviews.