Results 1 to 12 of 12

Thread: Alternate row color in listview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113

    Alternate row color in listview

    Has anyone tried to setup a listview so that the rows alternate color ? (eg. like greenbar computer paper)

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Not with a list view, but I've done it with grid controls.....
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Last edited by pvb; Jul 22nd, 2004 at 07:26 PM.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    ***!?
    I went to http://www.visualbasicdotnet.com/vbdotnet/
    And thought, "hmmmm...."
    and tried to goto
    http://www.visualbasicdotnet.com
    But it kept sending me to http://www.visualbasicdotnet.com/vbdotnet/
    EH?! Is that right?
    A one page web site that goes nowhere?
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113
    pvb,

    Thanks for the link to the example.... just what I needed.

  6. #6
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    yeah, um, you ever have those projects where ya start it then get caught up with something else more fun and tell yourself, "ya, I'll get to that tomorrow..."? well that site is one of my "ya, I'll get to that tomorrow..." sites

    For anyone who likes to know stuff, i have really great host http://www.activeservers.com. They host as many urls as i want under the condition that they all resolve to the same directory structure and ip (behind the scenes they just hook up all of my domain names to a single ip in dns). So all of my sites are subdirectories under the one site i pay for(way cool and way cheap), hence whenever you just type in the base url http://www.visualbasicdotnet.com, you will always be redirected to a subdirectory http://www.visualbasicdotnet.com/vbdotnet/ where the pages for that site exist. I have a single default.asp page at the root of the single ip address my host gives me, that checks to see what url someone typed in, then redirects to the appropriate subdirectory with that pages content.

    Anywho, I'll update that site tomorrow...

  7. #7
    Addicted Member
    Join Date
    Jun 2002
    Location
    Brisbane Australia
    Posts
    150

    Question Alternate colour in Combo ?

    Thats great for the list box. Does anyone know if you can do the same for a combo box ? There dosn't appear to be a comboboxitem !

  8. #8
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    So, after fiddling around with a listbox example from what I'm gonna say is the best VB.NET book to date(Francesco Balena's Programming Visual Basic .NET) here's one way of working the color banding on a combobox:



    I started a new windows application and added a single combobox to the default form. I named the combobox myCombo. This guy kicks it off, i call it from the Form_Load event:
    VB Code:
    1. Private Sub FillListBoxWithColors()
    2.     Dim itemCount As Integer
    3.     myCombo.DrawMode = DrawMode.OwnerDrawFixed
    4.     myCombo.ItemHeight = 18
    5.     myCombo.BeginUpdate()
    6.     myCombo.Items.Clear()
    7.     For itemCount = 1 To 20
    8.         myCombo.Items.Add("Item" & itemCount.ToString())
    9.     Next
    10.     myCombo.EndUpdate()
    11. End Sub

    You have to inject some code in the DrawItem event of the combobox that makes the magic happen:
    VB Code:
    1. Private Sub myCombo_DrawItem(ByVal sender As Object, _
    2.                              ByVal e As DrawItemEventArgs) _
    3.                              Handles myCombo.DrawItem
    4.  
    5.     Dim rect As Rectangle
    6.     Dim b As SolidBrush
    7.     Dim b2 As Brush
    8.  
    9.     'get X-Y coordinates of item being drawn
    10.     rect = e.Bounds
    11.  
    12.     'Fill the background with proper color
    13.     If (e.State And DrawItemState.Selected) Then
    14.         e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
    15.     Else
    16.         e.Graphics.FillRectangle(SystemBrushes.Window, rect)
    17.     End If
    18.  
    19.     'figure out what color to paint the background of the item.
    20.     If e.Index Mod 2 = 0 Then
    21.         b = New SolidBrush(Color.AliceBlue)
    22.     Else
    23.         b = New SolidBrush(Color.Beige)
    24.     End If
    25.  
    26.     'Create a 2 pixel margin around the item being drawn.
    27.     rect.Inflate(-2, -2)
    28.  
    29.     'Fill the new rectange with the current background color.
    30.     e.Graphics.FillRectangle(b, rect)
    31.  
    32.     'Color of the text
    33.     b2 = Brushes.Black
    34.  
    35.     'Now draw the item
    36.     e.Graphics.DrawString(myCombo.Items(e.Index), e.Font, b2, rect.X, rect.Y)
    37.  
    38.     'Dispose of the SolidBrush instance.
    39.     b.Dispose()
    40.  
    41. End Sub

    ...and that's it. And to be honest, that's some of the coolest stuff i've messed around with. Not just this example, but all the different things you can do with a pretty small amount of code and complexity with ANY control is mind boggling(comin from a vb 6 background anyway). And again, Balena's book is way awesome, i have a ton of vb.net books and his is by far the most thorough and indepth.

  9. #9
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    I also went through the 1600 pages of that book, and for the first time in my life studying, I managed to get all the way through. Has to be a good author to make me do that I tell ya.

    /Leyan

  10. #10
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188
    If someone has the alt color code for Listview - VB.NEt - please post it as the link in here still goes nowhere.

    Thanks.

  11. #11
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    heh, moved stuff around and broke some links. Here's the same project, ListView color banding

  12. #12
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i've got this
    VB Code:
    1. Dim cn As New SqlConnection()
    2.    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.       cn.ConnectionString = "user id=sa;password=password;initial catalog=northwind"
    4.       cn.Open()
    5.  
    6.       Dim cm As New SqlCommand("select * from territories", cn)
    7.       Dim dr As SqlDataReader = cm.ExecuteReader
    8.       Dim i As Integer = 0
    9.       While dr.Read
    10.          Dim li As ListViewItem = ListView1.Items.Add(dr(0))
    11.          li.SubItems.Add(dr(1))
    12.          li.SubItems.Add(dr(2))
    13.          If i Mod 2 = 0 Then
    14.             li.BackColor = Color.Brown ' Monkey
    15.          End If
    16.          i += 1
    17.       End While
    18.    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width