Well, that is of course always a possibility. Now please consider the MSDN
article "How to Create a Microsoft .NET Compact Framework-based Image Button"
(
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/ImageButton.asp), which is the basis of my approach.
I've done the following:
1) Created two 'ImageButtons' with the above mentioned code
2) Made one of the 'ImageButtons'decrease the selected index of a ListBox
listBox1
3) Made the other 'ImageButtons' increase the selected index of listBox1
4) Made two normal buttons do the same for another ListBox listBox2
This causes the described behaviour. The 'ImageButtons' are much slower and
the click events are quite frequently not even fired (or handeled) when
clicking the 'Imagebuttons'. What bug or inefficiency could be the reason for
this?
The source code for this is as follows with one Windows Form Form1 and
"ImageButton.cs" from the MSDN article:
//----------------------Form1.cs--------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace ImageButtonExample
{
public partial class Form1 : Form
{
private ImageButton ImageButton_Up;
private ImageButton ImageButton_Down;
private Button NormalButton_Up;
private Button NormalButton_Down;
private ListBox listBox1;
private ListBox listBox2;
private int iNumberOfItems;
public Form1()
{
this.Size= new Size(500,300);
//ImageButton_Up
ImageButton_Up = new ImageButtonClass();
ImageButton_Up.Image = new
Bitmap(ImageButtonExample.Properties.Resources.Up);
ImageButton_Up.Location = new Point(30, 50);
ImageButton_Up.Size = new Size(44, 34);
ImageButton_Up.Click += new EventHandler(ImageButton_Up_Click);
this.Controls.Add(ImageButton_Up);
//ImageButton_Down
ImageButton_Down = new ImageButtonClass();
ImageButton_Down.Image = new
Bitmap(ImageButtonExample..Properties.Resources.Down);
ImageButton_Down.Location = new Point(30, 90);
ImageButton_Down.Size = new Size(44, 34);
ImageButton_Down.Click += new
EventHandler(ImageButton_Down_Click);
this.Controls.Add(ImageButton_Down);
//NormalButton_Up
NormalButton_Up = new Button();
NormalButton_Up.Location = new Point(220, 50);
NormalButton_Up.Text = "Up";
NormalButton_Up.Click += new EventHandler(NormalButton_Up_Click);
this.Controls.Add(NormalButton_Up);
//NormalButton_Down
NormalButton_Down = new Button();
NormalButton_Down.Location = new Point(220, 80);
NormalButton_Down.Text = "Down";
NormalButton_Down.Click += new
EventHandler(NormalButton_Down_Click);
this.Controls.Add(NormalButton_Down);
//listBox1
listBox1 = new ListBox();
listBox1.Size = new Size(100, 100);
listBox1.Location = new Point(100, 50);
listBox1.Items.Add("item1");
listBox1.Items.Add("item2");
listBox1.Items.Add("item3");
listBox1.Items.Add("item4");
listBox1.Items.Add("item5");
listBox1.SelectedIndex = 0;
this.Controls.Add(listBox1);
//listBox2
listBox2 = new ListBox();
listBox2.Size = new Size(100, 100);
listBox2.Location = new Point(300, 50);
listBox2.Items.Add("item1");
listBox2.Items.Add("item2");
listBox2.Items.Add("item3");
listBox2.Items.Add("item4");
listBox2.Items.Add("item5");
listBox2.SelectedIndex = 0;
this.Controls.Add(listBox2);
iNumberOfItems = 5;
}
void ImageButton_Up_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > 0)
{
listBox1.SelectedIndex -= 1;
if (listBox1.SelectedIndex == 0)
{
}
}
}
void ImageButton_Down_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex < (iNumberOfItems - 1))
{
listBox1.SelectedIndex += 1;
if (listBox1.SelectedIndex == (iNumberOfItems - 1))
{
}
}
}
private void NormalButton_Up_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex > 0)
{
listBox2.SelectedIndex -= 1;
if (listBox2.SelectedIndex == 0)
{
}
}
}
private void NormalButton_Down_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex < (iNumberOfItems - 1))
{
listBox2.SelectedIndex += 1;
if (listBox2.SelectedIndex == (iNumberOfItems - 1))
{
}
}
}
}
}
//----------------ImageButton.cs from MSDN article--------------
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace ImageButtonExample
{
public class ImageButton : Control
{
private Image image;
private bool bPushed;
private Bitmap m_bmpOffscreen;
public Image Image
{
get
{
return image;
}
set
{
image = value;
}
}
public ImageButton()
{
bPushed = false;
//default minimal size
this.Size = new Size(21, 21);
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e )
{
Graphics gxOff; //Offscreen graphics
Rectangle imgRect; //image rectangle
Brush backBrush; //brush for filling a backcolor
if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap(ClientSize.Width,
ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(this.BackColor);
if (!bPushed)
backBrush = new SolidBrush(Parent.BackColor);
else //change the background when it's pressed
backBrush = new SolidBrush(Color.LightGray);
gxOff.FillRectangle(backBrush, this.ClientRectangle);
if (image != null)
{
//Center the image relativelly to the control
int imageLeft = (this.Width - image.Width) / 2;
int imageTop = (this.Height - image.Height) / 2;
if (!bPushed)
{
imgRect = new Rectangle(imageLeft, imageTop,
image.Width, image.Height);
}
else //The button was pressed
{
//Shift the image by one pixel
imgRect = new Rectangle(imageLeft + 1 , imageTop
+1, image.Width, image.Height);
}
//Set transparent key
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(BackgroundImageColor(image),
BackgroundImageColor(image));
//Draw image
gxOff.DrawImage(image, imgRect, 0, 0, image.Width,
image.Height, GraphicsUnit.Pixel, imageAttr);
}
if (bPushed) //The button was pressed
{
//Prepare rectangle
Rectangle rc = this.ClientRectangle;
rc.Width--;
rc.Height--;
//Draw rectangle
gxOff.DrawRectangle(new Pen(Color.Black), rc);
}
//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e);
}
protected override void
OnPaintBackground(System.Windows.Forms.PaintEventArgs e )
{
//Do nothing
}
protected override void OnMouseDown (
System.Windows.Forms.MouseEventArgs e )
{
bPushed = true;
this.Invalidate();
}
protected override void OnMouseUp ( System.Windows.Forms.MouseEventArgs
e )
{
bPushed = false;
this.Invalidate();
}
private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap(image);
return bmp.GetPixel(0, 0);
}
}
}