Has anyone tried to setup a listview so that the rows alternate color ? (eg. like greenbar computer paper)
Printable View
Has anyone tried to setup a listview so that the rows alternate color ? (eg. like greenbar computer paper)
Not with a list view, but I've done it with grid controls.....
here's a sample:
http://www.visualbasicdotnet.com/vbdn/
***!?
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?
pvb,
Thanks for the link to the example.... just what I needed.
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...
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 !
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:
http://www.visualbasicdotnet.com/vbd...edComboBox.jpg
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:
Private Sub FillListBoxWithColors() Dim itemCount As Integer myCombo.DrawMode = DrawMode.OwnerDrawFixed myCombo.ItemHeight = 18 myCombo.BeginUpdate() myCombo.Items.Clear() For itemCount = 1 To 20 myCombo.Items.Add("Item" & itemCount.ToString()) Next myCombo.EndUpdate() End Sub
You have to inject some code in the DrawItem event of the combobox that makes the magic happen:
VB Code:
Private Sub myCombo_DrawItem(ByVal sender As Object, _ ByVal e As DrawItemEventArgs) _ Handles myCombo.DrawItem Dim rect As Rectangle Dim b As SolidBrush Dim b2 As Brush 'get X-Y coordinates of item being drawn rect = e.Bounds 'Fill the background with proper color If (e.State And DrawItemState.Selected) Then e.Graphics.FillRectangle(SystemBrushes.Highlight, rect) Else e.Graphics.FillRectangle(SystemBrushes.Window, rect) End If 'figure out what color to paint the background of the item. If e.Index Mod 2 = 0 Then b = New SolidBrush(Color.AliceBlue) Else b = New SolidBrush(Color.Beige) End If 'Create a 2 pixel margin around the item being drawn. rect.Inflate(-2, -2) 'Fill the new rectange with the current background color. e.Graphics.FillRectangle(b, rect) 'Color of the text b2 = Brushes.Black 'Now draw the item e.Graphics.DrawString(myCombo.Items(e.Index), e.Font, b2, rect.X, rect.Y) 'Dispose of the SolidBrush instance. b.Dispose() 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.
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. :D
/Leyan
If someone has the alt color code for Listview - VB.NEt - please post it as the link in here still goes nowhere.
Thanks.
heh, moved stuff around and broke some links. Here's the same project, ListView color banding
i've got this
VB Code:
Dim cn As New SqlConnection() Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load cn.ConnectionString = "user id=sa;password=password;initial catalog=northwind" cn.Open() Dim cm As New SqlCommand("select * from territories", cn) Dim dr As SqlDataReader = cm.ExecuteReader Dim i As Integer = 0 While dr.Read Dim li As ListViewItem = ListView1.Items.Add(dr(0)) li.SubItems.Add(dr(1)) li.SubItems.Add(dr(2)) If i Mod 2 = 0 Then li.BackColor = Color.Brown ' Monkey End If i += 1 End While End Sub