Windows Develop Bookmark and Share   
 index > Windows Forms General > Label mousehover to show image
 

Label mousehover to show image

Hi,

I have a label with some text - Item number. When I move the mouse over this text how will I be able to show an image for the item number. 


Scorgi  Tuesday, October 06, 2009 8:22 PM
How you want to display the image??
I mean in some kind of picture box or some other way??
you can use MouseHover event for that..

Let me know if this helps.

Paras
paras kumar  Tuesday, October 06, 2009 9:48 PM
You can develop your own control by inheriting from Label.

Here is a sample code that you can achieve your goal by using ProductText and ProductImage properties.

    public class MyPictureLabel:System.Windows.Forms.Label
    {
        public MyPictureLabel():base()
        {
            this.AutoSize = false;
            this.Text = ProductText;
            this.BackColor = Color.White;
            
        }


        private Image _ResizedImage = null;
        private Image _ProductImage = null;
        public Image ProductImage
        {
            get { return _ProductImage; }
            set { _ProductImage = value; }
        }

        private string _ProductText = "";

        public string ProductText
        {
            get { return _ProductText; }
            set { _ProductText = value; this.Text = value; }
        }
        protected override void OnMouseHover(EventArgs e)
        {
            base.OnMouseHover(e);
            if (_ResizedImage == null || _ResizedImage.Width!=this.Width || _ResizedImage.Height!=this.Height)
            {
                this._ResizedImage = ProductImage.GetThumbnailImage(this.Width, this.Height, null, IntPtr.Zero);
            }
            if (ProductImage != null)
            {
                this.Text = "";
                this.Image = _ResizedImage;
            }
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            this.Image = null;
            this.Text = ProductText;
            base.OnMouseLeave(e);
        }
    }
  • Proposed As Answer byTamer OzMVP8 hours 48 minutes ago
  •  
Tamer Oz  Wednesday, October 07, 2009 3:41 AM

You can use google to search for other answers

Custom Search

More Threads

• ?? Right-Click on ListBox Item ??
• read/write/edit file on button click
• MonthCalender DateChanged Event problem
• DXUT
• Staus bar not visible in MDI Frame
• CPU usage and Virtual size issue
• Checkbox in datagrid?
• C# KeyDown and Num Lock
• Topmost from when called from sub Main
• dataset : disconnected can i apply to semi-real time.