473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DrawItem event of ListBox and CheckedListBox control

I can not make the DrawItem event to work on the CheckedListBox control. I
suggestion will be highly appreciated.

I have the following code. The code works for ListBox and the second item
(Two) is displayed in red in the ListBox but the CheckedLIstBox displays
everything in black (where it should be displayed as red).

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
CheckedListBox1.DrawMode = DrawMode.OwnerDrawFixed
AddHandler CheckedListBox1.DrawItem, AddressOf
Me.CheckedListBox1_DrawItem

ListBox1.DrawMode = DrawMode.OwnerDrawFixed
AddHandler ListBox1.DrawItem, AddressOf Me.ListBox1_DrawItem
End Sub

Sub CheckedListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
Dim textBrush As Brush = SystemBrushes.ControlText
Dim drawFont As Font = e.Font
If (CheckedListBox1.Items.Item(e.Index).ToString() = "Two") Then
textBrush = Brushes.Red
drawFont = New Font(drawFont.FontFamily, drawFont.Size,
FontStyle.Bold)
ElseIf (e.State And DrawItemState.Selected) > 0 Then
textBrush = SystemBrushes.HighlightText
End If

e.Graphics.DrawString(CheckedListBox1.Items.Item(e .Index).ToString(),
drawFont, textBrush, e.Bounds.X, e.Bounds.Y)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
CheckedListBox1.Items.Add("One")
CheckedListBox1.Items.Add("Two")
CheckedListBox1.Items.Add("Three")
CheckedListBox1.Items.Add("Four")
ListBox1.Items.Add("One")
ListBox1.Items.Add("Two")
ListBox1.Items.Add("Three")
ListBox1.Items.Add("Four")
End Sub

Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs)
e.DrawBackground()
Dim textBrush As Brush = SystemBrushes.ControlText
Dim drawFont As Font = e.Font
If (ListBox1.Items.Item(e.Index).ToString() = "Two") Then
textBrush = Brushes.Red
drawFont = New Font(drawFont.FontFamily, drawFont.Size,
FontStyle.Bold)
ElseIf (e.State And DrawItemState.Selected) > 0 Then
textBrush = SystemBrushes.HighlightText
End If
e.Graphics.DrawString(ListBox1.Items.Item(e.Index) .ToString(),
drawFont, textBrush, e.Bounds.X, e.Bounds.Y)
End Sub
Jul 21 '05 #1
0 1991

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim H | last post by:
I'm trying to use an owner draw ListBox by subscribing to the DrawItem event to draw my custom control as an item in the ListBox. This event is triggered when I drag the scroll button but NOT when...
1
by: Hrvoje Voda | last post by:
I have a users name in checkedlistBox and usersID in listBox. How to connect those to lists to get a userID from listBox depending on userName from checkedlistBox? Hrcko
1
by: e | last post by:
I have an ownerdrawn listbox that paints items in various colors dependant upon whether or not it has focus, selected, etc. The code that physically paints the items is in the drawItem event, and...
0
by: pothik05 | last post by:
I can not make the DrawItem event to work on the CheckedListBox control. I suggestion will be highly appreciated. I have the following code. The code works for ListBox and the second item (Two)...
1
by: Huahe | last post by:
Hi, I have an checkedlistbox with a lot of items, so there is a vertical scrollbar in the checkedlistbox. I want to make the checkedlistbox invisible when the mouseleave event goes off. The...
0
by: Paul_Madden via DotNetMonster.com | last post by:
I am handling the Listbox DrawItem event to enable the listbox strings to be displayed in different colours for easy reading. Have set ScrollAlwaysVisible and HorizontalScrollbar to true. I...
2
by: Paul_Madden via DotNetMonster.com | last post by:
I am handling the Listbox DrawItem event to enable the listbox strings to be displayed in different colours for easy reading. Have set ScrollAlwaysVisible and HorizontalScrollbar to true. I...
5
by: mabond | last post by:
Hi Can't believe I've not been able to find the answer to this in the on-line help. I have a CheckedListBox which, via a timer control, is populated with the names of files in a network...
4
by: Kevin | last post by:
I'm using an ActiveX Listbox control in my Windows Forms program. I can load the listbox and have it work fine on one of my forms. But when I add the line: Me.MDIParent = frmMain the form...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.