2008-10-06

Call DrawItem from another sub

Hi,

In this post I am going to tell you how to call DrawItem from another function, sub or procedure.

What is DrawItem
DrawItem is a event which occurs when a visual aspect of an owner-drawn control changes. The DrawItem event is fired only if the DrawMode property is set to OwnerDrawFixed or OwnerDrawVariable. In this article I am going to use this event to change the the ForeColor of Listbox items.

Simple Example

VB.NET Version:
Public Class Form1

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load

ListBox1.DrawMode = DrawMode.OwnerDrawFixed
ListBox1.Items.Add("Red")
ListBox1.Items.Add("Green")
ListBox1.Items.Add("Blue")
End Sub

Private Sub ListBox1_DrawItem( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs _
) Handles ListBox1.DrawItem

Dim myFont As Font
If e.Index > -1 Then
e.DrawBackground()
myFont = New Font(ListBox1.Font.Name, ListBox1.Font.Size, ListBox1.Font.Style)
If ListBox1.Items(e.Index).ToString = "Red" Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, myFont, New SolidBrush(Color.Red), e.Bounds)
ElseIf ListBox1.Items(e.Index).ToString = "Green" Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, myFont, New SolidBrush(Color.Green), e.Bounds)
ElseIf ListBox1.Items(e.Index).ToString = "Blue" Then
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, myFont, New SolidBrush(Color.Blue), e.Bounds)
End If
End If
End Sub

End Class
C# Version:
public class Form1 {

private void Form1_Load(object sender, System.EventArgs e) {
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
ListBox1.Items.Add("Red");
ListBox1.Items.Add("Green");
ListBox1.Items.Add("Blue");
}

private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
Font myFont;

if (e.Index > -1) {

e.DrawBackground();
myFont = new Font(ListBox1.Font.Name, ListBox1.Font.Size, ListBox1.Font.Style);

if (ListBox1.Items(e.Index).ToString == "Red") {
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, myFont, new SolidBrush(Color.Red), e.Bounds);
}
else if (ListBox1.Items(e.Index).ToString == "Green") {
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, myFont, new SolidBrush(Color.Green), e.Bounds);
}
else if (ListBox1.Items(e.Index).ToString == "Blue") {
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString, myFont, new SolidBrush(Color.Blue), e.Bounds);
}

}
}

}
Example is quite straight forward. In the Load event of the Form I am adding three items to the Listbox control "Red", "Green" and "Blue" respectively. In the DrawItem event I am checking the text of the listbox item being added and doing the coloring stuff.

Real World Scenario
The example given above is very simple. In real world applications the scenarios may not be as simple as this one. I am checking for Red, Green and Blue colors in DrawItem event itself, but what of you want to create a procedure that will add an item to the listbox as well as will specifying the color of that listbox item.

Real World Example
Lets consider a scenario wherein I want to add items to a ListBox control. While adding items to ListBox, I want to specify the ForeColor of the list item. This cannot be done in DrawItem event. Just go through the code snippet given below. I will explain it shortly.

VB.NET Version:
Public Class Form1

Class ListItem
Friend Text As String
Friend ForeColor As Color

Public Sub New(ByVal text As String, ByVal textColor As Color)
Me.Text = text
Me.ForeColor = textColor
End Sub

Public Overrides Function ToString() As String
Return Text
End Function
End Class

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load

ListBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub

Private Sub AddItem(ByVal text As String, ByVal foreColor As Color)
ListBox1.Items.Add(New ListItem(text, foreColor))
End Sub

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click

AddItem("One", Color.Red)
AddItem("Two", Color.Green)
AddItem("Three", Color.Blue)
End Sub

Private Sub ListBox1_DrawItem( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs _
) Handles ListBox1.DrawItem

Dim myFont As Font
Dim cListItem As ListItem

If e.Index > -1 Then
e.DrawBackground()
myFont = New Font(ListBox1.Font.Name, ListBox1.Font.Size, ListBox1.Font.Style)
cListItem = DirectCast(ListBox1.Items(e.Index), ListItem)
e.Graphics.DrawString(cListItem.ToString, myFont, New SolidBrush(cListItem.ForeColor), e.Bounds)
End If
End Sub

End Class
C# Version:
public class Form1 {

class ListItem {
internal string Text;
internal Color ForeColor;

public ListItem(string text, Color textColor) {
this.Text = text;
this.ForeColor = textColor;
}

public override string ToString() {
return Text;
}
}

private void Form1_Load(object sender, System.EventArgs e) {
ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
}

private void AddItem(string text, Color foreColor) {
ListBox1.Items.Add(new ListItem(text, foreColor));
}

private void Button1_Click(object sender, System.EventArgs e) {
AddItem("One", Color.Red);
AddItem("Two", Color.Green);
AddItem("Three", Color.Blue);
}

private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
Font myFont;
ListItem cListItem;

if (e.Index > -1) {
e.DrawBackground();
myFont = new Font(ListBox1.Font.Name, ListBox1.Font.Size, ListBox1.Font.Style);
cListItem = (ListItem)ListBox1.Items(e.Index);
e.Graphics.DrawString(cListItem.ToString, myFont, new SolidBrush(cListItem.ForeColor), e.Bounds);
}
}

}

In the above example I have created a custom class called 'ListItem' to represent the item to be added in the Listbox control. This class contains two members which are quite explanatory. They represent Text and ForeColor of list box item. Next, I have created a constructor having two arguments whose values are assigned to the class members. In the next code segment I am overriding the ToString function of the class. ToString function is the default function of any class. When items are added to the listbox, this function gets called. I am using this function to return the text to be displayed in the listbox control.

In the form load event, I am setting the drawMode property of Listbox. DrawMode specifies how the elements of a control are drawn i. e. it gets or sets a value indicating whether user code or the operating system will handle drawing of elements in the list. Its default value is set to 'Normal'. When the DrawMode property is set to Normal, all the elements in a control are drawn by the operating system. As in this example we are going to handle to drawing stuff ourselves in DrawItem event, I set the value of DrawMode to OwnerDrawFixed.

Next, I have created a procedure 'AddItem' which accepts two arguments; text and foreColor. Using this two arguments I am creating a new object of ListItem class and adding it to the Listbox. In the button click event, I am calling the AddItem procedure and passing values to it.

The next code segment is the most important part of this article i.e. DrawItem event. Every time an item is added to the ListBox, DrawItem event is fired.
This event is also fired even when there are no items in the ListBox. So I have added an IF condition to prevent our code from failure. The next statement is e.DrawBackground(). This method basically draws the item selection background. In the next statement I am creating a font object from Listbox control that will be used while drawing the actual string in the ListBox. In the next statement I am getting the object ofListItem class that we added from AddItem method. The next statement is the heart of the DrawItem event which actually draws the ListBox item text.

That's All.

No comments: