|
-
Nov 25th, 2002, 01:32 PM
#1
Thread Starter
Lively Member
Alternate row color in listview
Has anyone tried to setup a listview so that the rows alternate color ? (eg. like greenbar computer paper)
-
Nov 25th, 2002, 01:35 PM
#2
Not with a list view, but I've done it with grid controls.....
-
Nov 25th, 2002, 01:39 PM
#3
Hyperactive Member
Last edited by pvb; Jul 22nd, 2004 at 07:26 PM.
-
Nov 25th, 2002, 01:42 PM
#4
***!?
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?
-
Nov 25th, 2002, 01:56 PM
#5
Thread Starter
Lively Member
pvb,
Thanks for the link to the example.... just what I needed.
-
Nov 25th, 2002, 02:18 PM
#6
Hyperactive Member
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...
-
Nov 25th, 2002, 06:04 PM
#7
Addicted Member
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 !
-
Nov 26th, 2002, 12:13 AM
#8
Hyperactive Member
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:
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.
-
Nov 26th, 2002, 02:57 AM
#9
Registered User
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
-
Jul 22nd, 2004, 11:34 AM
#10
Addicted Member
If someone has the alt color code for Listview - VB.NEt - please post it as the link in here still goes nowhere.
Thanks.
-
Jul 22nd, 2004, 07:28 PM
#11
Hyperactive Member
heh, moved stuff around and broke some links. Here's the same project, ListView color banding
-
Jul 22nd, 2004, 08:22 PM
#12
Fanatic Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|