The code was taken from MSDN
using System;
using System.Drawing;
using System.Drawing. Drawing2D;
using System.Globaliz ation;
using System.Windows. Forms;
namespace WindowsApplicat ion1
{
public class Form1: Form
{
private ListView listView1 = new ListView();
private ContextMenu contextMenu1 = new ContextMenu();
public Form1()
{
// Initialize the ListView control.
listView1.BackC olor = Color.Black;
listView1.ForeC olor = Color.White;
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.FullR owSelect = true;
// Add columns to the ListView control.
listView1.Colum ns.Add("Name", 72, HorizontalAlign ment.Center);
listView1.Colum ns.Add("First", 72, HorizontalAlign ment.Center);
listView1.Colum ns.Add("Second" , 72, HorizontalAlign ment.Center);
listView1.Colum ns.Add("Third", 72, HorizontalAlign ment.Center);
// Create items and add them to the ListView control.
ListViewItem listViewItem1 = new ListViewItem(ne w string[] { "One", "20",
"30", "-40" }, -1);
ListViewItem listViewItem2 = new ListViewItem(ne w string[] { "Two",
"-250", "145", "37" }, -1);
ListViewItem listViewItem3 = new ListViewItem(ne w string[] { "Three",
"200", "800", "-1,001" }, -1);
ListViewItem listViewItem4 = new ListViewItem(ne w string[] { "Four", "not
available", "-2", "100" }, -1);
listView1.Items .AddRange(new ListViewItem[] { listViewItem1,
listViewItem2, listViewItem3, listViewItem4 });
// Initialize the shortcut menu and
// assign it to the ListView control.
contextMenu1.Me nuItems.Add("Li st", new EventHandler(me nuItemList_Clic k));
contextMenu1.Me nuItems.Add("De tails", new
EventHandler(me nuItemDetails_C lick));
listView1.Conte xtMenu = contextMenu1;
// Configure the ListView control for owner-draw and add
// handlers for the owner-draw events.
listView1.Owner Draw = true;
listView1.DrawI tem += new
DrawListViewIte mEventHandler(l istView1_DrawIt em);
listView1.DrawS ubItem += new
DrawListViewSub ItemEventHandle r(listView1_Dra wSubItem);
listView1.DrawC olumnHeader += new
DrawListViewCol umnHeaderEventH andler(listView 1_DrawColumnHea der);
// Add a handler for the MouseMove event to compensate for an
// extra DrawItem event that occurs the first time the mouse
// moves over each row.
listView1.Mouse Move += new MouseEventHandl er(listView1_Mo useMove);
// Add a handler for the MouseUp event so an item can be
// selected by clicking anywhere along its width.
listView1.Mouse Up += new MouseEventHandl er(listView1_Mo useUp);
// Initialize the form and add the ListView control to it.
this.ClientSize = new Size(292, 79);
this.FormBorder Style = FormBorderStyle .FixedSingle;
this.MaximizeBo x = false;
this.Text = "ListView OwnerDraw Example";
this.Controls.A dd(listView1);
}
// Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing)
{
contextMenu1.Di spose();
}
base.Dispose(di sposing);
}
[STAThread]
static void Main()
{
Application.Ena bleVisualStyles ();
Application.Run (new ListViewOwnerDr aw());
}
// Sets the ListView control to the List view.
private void menuItemList_Cl ick(object sender, EventArgs e)
{
listView1.View = View.List;
listView1.Inval idate();
}
// Sets the ListView control to the Details view.
private void menuItemDetails _Click(object sender, EventArgs e)
{
listView1.View = View.Details;
// Reset the tag on each item to re-enable the workaround in
// the MouseMove event handler.
foreach (ListViewItem item in listView1.Items )
{
item.Tag = null;
}
}
// Selects and focuses an item when it is clicked anywhere along
// its width. The click must normally be on the parent item text.
private void listView1_Mouse Up(object sender, MouseEventArgs e)
{
ListViewItem clickedItem = listView1.GetIt emAt(5, e.Y);
if (clickedItem != null)
{
clickedItem.Sel ected = true;
clickedItem.Foc used = true;
}
}
// Draws the backgrounds for entire ListView items.
private void listView1_DrawI tem(object sender, DrawListViewIte mEventArgs
e)
{
if ((e.State & ListViewItemSta tes.Selected) != 0)
{
// Draw the background and focus rectangle for a selected item.
e.Graphics.Fill Rectangle(Brush es.Maroon, e.Bounds);
e.DrawFocusRect angle();
}
else
{
// Draw the background for an unselected item.
using (LinearGradient Brush brush =
new LinearGradientB rush(e.Bounds, Color.Orange,
Color.Maroon, LinearGradientM ode.Horizontal) )
{
e.Graphics.Fill Rectangle(brush , e.Bounds);
}
}
// Draw the item text for views other than the Details view.
if (listView1.View != View.Details)
{
e.DrawText();
}
}
// Draws subitem text and applies content-based formatting.
private void listView1_DrawS ubItem(object sender,
DrawListViewSub ItemEventArgs e)
{
TextFormatFlags flags = TextFormatFlags .Left;
using (StringFormat sf = new StringFormat())
{
// Store the column text alignment, letting it default
// to Left if it has not been set to Center or Right.
switch (e.Header.TextA lign)
{
case HorizontalAlign ment.Center:
sf.Alignment = StringAlignment .Center;
flags = TextFormatFlags .HorizontalCent er;
break;
case HorizontalAlign ment.Right:
sf.Alignment = StringAlignment .Far;
flags = TextFormatFlags .Right;
break;
}
// Draw the text and background for a subitem with a
// negative value.
double subItemValue;
if (e.ColumnIndex 0 && Double.TryParse (
e.SubItem.Text, NumberStyles.Cu rrency,
NumberFormatInf o.CurrentInfo, out subItemValue) &&
subItemValue < 0)
{
// Unless the item is selected, draw the standard
// background to make it stand out from the gradient.
if ((e.ItemState & ListViewItemSta tes.Selected) == 0)
{
e.DrawBackgroun d();
}
// Draw the subitem text in red to highlight it.
e.Graphics.Draw String(e.SubIte m.Text,
listView1.Font, Brushes.Red, e.Bounds, sf);
return;
}
// Draw normal text for a subitem with a nonnegative
// or nonnumerical value.
e.DrawText(flag s);
}
}
// Draws column headers.
private void listView1_DrawC olumnHeader(obj ect sender,
DrawListViewCol umnHeaderEventA rgs e)
{
using (StringFormat sf = new StringFormat())
{
// Store the column text alignment, letting it default
// to Left if it has not been set to Center or Right.
switch (e.Header.TextA lign)
{
case HorizontalAlign ment.Center:
sf.Alignment = StringAlignment .Center;
break;
case HorizontalAlign ment.Right:
sf.Alignment = StringAlignment .Far;
break;
}
// Draw the standard header background.
e.DrawBackgroun d();
// Draw the header text.
using (Font headerFont =
new Font("Helvetica ", 10, FontStyle.Bold) )
{
e.Graphics.Draw String(e.Header .Text, headerFont,
Brushes.Black, e.Bounds, sf);
}
}
return;
}
// Forces each row to repaint itself the first time the mouse moves over
// it, compensating for an extra DrawItem event sent by the wrapped
// Win32 control.
private void listView1_Mouse Move(object sender, MouseEventArgs e)
{
ListViewItem item = listView1.GetIt emAt(e.X, e.Y);
if (item != null && item.Tag == null)
{
listView1.Inval idate(item.Boun ds);
item.Tag = "tagged";
}
}
}
}
"Jani Järvinen [MVP]" <ja***@removeth is.dystopia.fiw rote in message
news:eu******** ******@TK2MSFTN GP03.phx.gbl...
Hello,
can you post your code that shows the error and the complete error
message?
>
There must be something else going on in your code if you have the
references/using statements already in place.
--
Regards,
Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethi s.dystopia.fi
http://www.saunalahti.fi/janij/