Hi, i've got this code to do a Custom List View.

When i feel the item they doesn't apair on the list, but if a click over
they aparis.

Then arrow key doesn't work.



Can someone help me to know the problem?



Thank's Agnoletto Christian



/*

THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF

ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO

THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A

PARTICULAR PURPOSE.


This is sample code and is freely distributable.

*/

using System;

using System.Windows.Forms;

using System.Collections;

using System.Drawing;

using System.Data;

using System.Reflection;

using System.Drawing.Imaging;

namespace ofta_mobile {

enum ClienteIcon {

privato,

business

}

/// <summary>

/// Class used to store display score info

/// </summary>

class ClienteItem{

public ClienteIcon icon;

public string codice;

public string ragione_sociale;

public string indirizzo;

public string provincia;

public ClienteItem(ClienteIcon Icon, string Codice, string
Ragione_sociale,string Indirizzo,string Provincia)

{

this.icon = Icon;

this.codice = Codice;

this.ragione_sociale = Ragione_sociale;

this.indirizzo= Indirizzo;

this.provincia= Provincia;

}

}

/// <summary>

/// Provide a 2 line listview

/// </summary>

class MyListView : OwnerDrawnList {

const int Column1Left = 5;

const int Column2Left = 30;

public ImageList ImageList1;

public MyListView() {

// We need a total of 5 rows, so the height is 180/5=36

Graphics g = this.CreateGraphics();

Font font = new Font(this.Font.Name, 10, FontStyle.Bold);

// Calc line height to be height of letter A plus 10%

int fontHeight = (int)(g.MeasureString("A", font).Height*1.1);

this.ItemHeight = fontHeight*3;

g.Dispose();

}

public void SetImage(ImageList iml )

{

ImageList1 = iml;

}

// Check if the user presses the action key

protected override void OnKeyDown(KeyEventArgs e) {

MessageBox.Show(e.KeyCode.ToString());

switch(e.KeyCode) {

case Keys.Return:

MessageBox.Show("You selected item " + this.SelectedIndex.ToString(),

"Item selected",

MessageBoxButtons.OK,

MessageBoxIcon.Asterisk,

MessageBoxDefaultButton.Button1);

break;

}

base.OnKeyDown(e);

}



/// <summary>

/// Custom OnPaint. This paints the listview items

/// </summary>

/// <param name="e"></param>

protected override void OnPaint(PaintEventArgs e) {


// Declare vars

Font font;

Color fontColor;

ClienteItem lvi;

string riga;

//string bmpName;

// bmpName="";


// Get graphics object from bitmap.

Graphics gOffScreen = Graphics.FromImage(this.OffScreen);


// Set background color

gOffScreen.FillRectangle(new SolidBrush(this.BackColor),
this.ClientRectangle);

gOffScreen.DrawRectangle(new Pen(SystemColors.ActiveBorder),
this.ClientRectangle);


// Set the y pos of the current item

int itemTop = 0;

// Draw the visible items.

for(int n = this.VScrollBar.Value; n <= this.VScrollBar.Value + DrawCount;
n++) {

// Draw the selected item to appear highlighted

if(n == this.SelectedIndex) {

gOffScreen.FillRectangle(new SolidBrush(SystemColors.Highlight),

0,

itemTop,

// If the scroll bar is visible, subtract the scrollbar width

this.ClientSize.Width - (this.VScrollBar.Visible ? this.VScrollBar.Width :
0),

this.ItemHeight);

fontColor = CalcTextColor(SystemColors.Highlight);

}

else

fontColor = this.ForeColor;

// Draw a gray separator for each item

gOffScreen.DrawLine(new Pen(Color.DarkGray), 1, itemTop+this.ItemHeight,
this.ClientSize.Width - (this.VScrollBar.Visible ? this.VScrollBar.Width :
2), itemTop+this.ItemHeight);

if (this.Items.Count > n)

{

lvi = (ClienteItem)this.Items[n];

int indice=0;

switch(lvi.icon.ToString())

{

case "privato":

indice=2;

break;

case "business":

indice=3;

break;

default:

indice=1;

MessageBox.Show(lvi.icon.ToString());

break;

};

Image bmp = ImageList1.Images[indice];

Rectangle imgRect = new Rectangle(Column1Left, itemTop+ ItemHeight/ 3,
bmp.Width, bmp.Height);

// Draw the image

gOffScreen.DrawImage(bmp, imgRect, 0,0, bmp.Width, bmp.Height,
GraphicsUnit.Pixel, new ImageAttributes());

font = new Font(this.Font.Name, 10, FontStyle.Regular);

gOffScreen.DrawString(lvi.codice, font, new SolidBrush(fontColor),
Column1Left, itemTop);

font.Dispose();

font = new Font(this.Font.Name, 10, FontStyle.Bold);

gOffScreen.DrawString(lvi.ragione_sociale, font, new SolidBrush(fontColor),
Column2Left, itemTop );


font.Dispose();

font = new Font(this.Font.Name, 10, FontStyle.Italic);

riga = lvi.indirizzo+ " ("+lvi.provincia+")";

gOffScreen.DrawString(riga, font, new SolidBrush(fontColor), Column2Left,
itemTop + (ItemHeight/3));

font.Dispose();

};

itemTop += this.ItemHeight;

}

// Now draw the visible list box

e.Graphics.DrawImage(this.OffScreen, 0, 0);

gOffScreen.Dispose();

}

// Draws the external border around the control.

protected override void OnPaintBackground(PaintEventArgs e) {

// e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0,
this.ClientSize.Width - 1, this.ClientSize.Height - 1);

}

protected override void OnMouseDown(MouseEventArgs e)

{

SelectedIndex=(e.Y/this.ItemHeight)+this.VScrollBar.Value;

this.Refresh();

}

}

// Base custom control for OwnerDrawnList

class OwnerDrawnList : Control {

int scrollWidth;

int itemHeight = -1;

int selectedIndex = -1;

Bitmap offScreen;

VScrollBar vs;

ArrayList items;

public OwnerDrawnList() {

this.vs = new VScrollBar();

scrollWidth = this.vs.Width;

this.vs.Parent = this;

this.vs.Visible = false;

this.vs.SmallChange = 1;

this.vs.ValueChanged += new EventHandler(this.ScrollValueChanged);

// this.vs.KeyDown += new KeyEventHandler(this.OnKeyDown);

// Items to draw

this.items = new ArrayList();

}

public ArrayList Items {

get { return this.items;}

}

protected Bitmap OffScreen {

get {return this.offScreen;}

}

protected VScrollBar VScrollBar {

get {return this.vs;}

}

public event EventHandler SelectedIndexChanged;

// Raise the SelectedIndexChanged event

protected virtual void OnSelectedIndexChanged(EventArgs e) {

if(this.SelectedIndexChanged != null)

this.SelectedIndexChanged(this, e);

}

// Get or set index of selected item.

public int SelectedIndex {

get {return this.selectedIndex;}

set {

this.selectedIndex = value;

if (this.SelectedIndexChanged != null)

this.SelectedIndexChanged(this, EventArgs.Empty);

}

}

protected void ScrollValueChanged(object o, EventArgs e) {

this.Refresh();

}

protected virtual int ItemHeight {

get {return this.itemHeight;}

set {this.itemHeight = value;}

}

// If the requested index is before the first visible index then set the

// first item to be the requested index. If it is after the last visible

// index, then set the last visible index to be the requested index.

public void EnsureVisible(int index) {

if(index < this.vs.Value) {

this.vs.Value = index;

this.Refresh();

}

else if(index >= this.vs.Value + this.DrawCount) {

this.vs.Value = index - this.DrawCount + 1;

this.Refresh();

}

}

// Selected item moves when you use the keyboard up/down keys.

//protected void OnKeyDown(object o, KeyEventArgs e)

// {

// MessageBox.Show(e.KeyCode.ToString());

// }



protected override void OnKeyDown( KeyEventArgs e)

{

/* MessageBox.Show(e.KeyCode.ToString());

switch(e.KeyCode) {

case Keys.Down:

if(this.SelectedIndex < this.vs.Maximum) {

EnsureVisible(++this.SelectedIndex);

this.Refresh();

}

break;

case Keys.Up:

if(this.SelectedIndex > this.vs.Minimum) {

EnsureVisible(--this.SelectedIndex);

this.Refresh();

}

break;

}

base.OnKeyDown(e);

*/}

// Calculate how many items we can draw given the height of the control.

protected int DrawCount {

get {

if(this.vs.Value + this.vs.LargeChange > this.vs.Maximum)

return this.vs.Maximum - this.vs.Value + 1;

else

return this.vs.LargeChange;

}

}

/* protected virtual void OnMouseDown(EventArgs e)

{

MessageBox.Show("MouseDown");

}*/

protected override void OnResize(EventArgs e) {

int viewableItemCount = this.ClientSize.Height / this.ItemHeight;

this.vs.Bounds = new Rectangle(this.ClientSize.Width - scrollWidth,

0,

scrollWidth,

this.ClientSize.Height);



// Determine if scrollbars are needed

if(this.items.Count > viewableItemCount) {

this.vs.Visible = true;

this.vs.LargeChange = viewableItemCount;

this.offScreen = new Bitmap(this.ClientSize.Width - scrollWidth,
this.ClientSize.Height);

}

else {

this.vs.Visible = false;

this.vs.LargeChange = this.items.Count;

this.offScreen = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);

}

this.vs.Maximum = this.items.Count - 1;


}

// Determine what the text color should be

// for the selected item drawn as highlighted


protected Color CalcTextColor(Color backgroundColor) {

if(backgroundColor.Equals(Color.Empty))

return Color.Black;

int sum = backgroundColor.R + backgroundColor.G + backgroundColor.B;

if(sum > 256)

return Color.Black;

else

return Color.White;

}


}

}