הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, November 19, 2011 7:27 AM
Hola,
I know how to have alternate row colors with ListView. But how about with ListBox? I've run a search. I read in some web sites where they say you can't. Is that true? Meanwhile, I've done something like
For i As Integer = 0 To ListBox1.Items.Count - 1 Step 1
If i Mod 2 = 0 Then
ListBox1. BackColor = Color.FromArgb(150, 250, 255)
Else
ListBox1. BackColor = Color.White
End If
Next i
with ListBox's DrawItem event. And it doesn't work. If you can't have alternate row colors with ListBox, that's a big disappointment with Visual Basic for me.
Muchas gracias,
Onion
Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
All replies (8)
Saturday, November 19, 2011 11:22 AM ✅Answered | 2 votes
Here is an example:
this is a list box where the back color of every other row is light gray
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For x = 1 To 20
ListBox1.Items.Add("Crazypennie " & CStr(x))
Next
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
ListBox1.ItemHeight += 5
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
If e.Index Mod 2 = 0 Then
e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds)
End If
If ListBox1.SelectedIndex = e.Index Then
e.Graphics.FillRectangle(Brushes.Blue, e.Bounds)
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, Me.Font, Brushes.White, 0, e.Bounds.Y + 2)
Else
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, Me.Font, Brushes.Black, 0, e.Bounds.Y + 2)
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox1.Refresh()
End Sub
End Class
Saturday, November 19, 2011 9:03 AM | 1 vote
Yes it is true if you want to do this with Windows Forms,
That is why they created WPF.
You can also search for 3th party listboxes.
Success
Cor
Saturday, November 19, 2011 10:00 AM | 1 vote
The Items are undefined objects. Thus, they can be anything you wish. You have control of any drawing through the DrawMode property. You can pretty much do anything you wish.
Saturday, November 19, 2011 10:04 AM
Oh, no...
...
Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
Saturday, November 19, 2011 9:33 PM | 1 vote
If you can't have alternate row colors with ListBox, that's a big disappointment with Visual Basic for me.
Keep in mind that this is not an issue with Visual Basic, rather it is with the MS team that designed the WonForms controls. And you wouldn't be the first person to complain that MS skimped out on development of these controls. However, they were never intended to be highly customizable like, say, 3rd party control suites such as DevExpress or Telerik. My belief is regarding the intention of the design team was that they provided an "adequate" set of controls that could be used as-is for most scenarios but could also be customized through inheritance or through the rich set of events that are exposed should the need arise. Additionally there is the UserControl base class that allows you to make your own controls. That's a journey in itself ;)
It's pretty clear that MS is not avidly in the business of developing control suites, so as a developer wishing to push controls to the limits you may want to consider either paying for control suites or looking for free code of others who have customized WinForms controls. These can be found in places like CodePlex, CodeProject, or many other repositories for code examples and articles.
Saturday, November 19, 2011 9:45 PM
Thanks, Crazypennie.
It doesn't work for me, though.
Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
Saturday, November 19, 2011 10:53 PM
Thanks, Crazypennie.
It doesn't work for me, though.
Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64
Can you be more specific please? What didn't work for you? I tried the code and it made the listbox have alternating colors for the items collection.
And for the record when considering what I wrote above about MS's attitude towrd controls, CrazyPennie's solution is a pretty quick and easy one. It should be a testament to the flexibility they built into the WinForms control suite... at least for easy to medium-difficulty modifications like this.
Saturday, November 19, 2011 11:06 PM
After I removed
For x = 1 To 20
ListBox1.Items.Add("Crazypennie " & CStr(x))
Next
from the first set, it works.
Thanks.
Onion is a recent refuge from Mac OS X and hates Mac App Store. System: Windows 7 x64