)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to display image on TreeMenuItem in WinForms TreeNavigator?

Platform: WinForms |
Control: TreeNavigator |
Published Date: February 19, 2016 |
Last Revised Date: February 4, 2020

Display image on TreeMenuItem

In TreeMenuItem, the image can be displayed by creating custom control which is inherited from TreeMenuItem. The menu item image can be shown by following the steps.

1. Need to create class which is inherited from TreeMenuItem.

2. Need to create custom property to assign the ItemImage.

3. Need to overrides the OnPaint event and draw the image in required location.

C#

//To specify the TreeMenuItem image
this.treeMenuItem1.ItemImage = this.imageListAdv1.Images[0];
this.treeMenuItem2.ItemImage = this.imageListAdv1.Images[1];
this.treeMenuItem3.ItemImage = this.imageListAdv1.Images[2];
this.treeMenuItem4.ItemImage = this.imageListAdv1.Images[3];
 
/// <summary>
/// Creating custom class
/// </summary>
public class TreeMenuItemEx : TreeMenuItem
{
    public TreeMenuItemEx()
    {
        //Initialization
    }
 
    /// <summary>
    /// Initialize the item image.
    /// </summary>
    private Image m_ItemImage = null;
 
    /// <summary>
    /// Gets or sets the item image.
    /// </summary>
    public Image ItemImage
    {
        get
        {
            return m_ItemImage;
        }
        set
        {
            m_ItemImage = value;
        }
    }
 
    /// <summary>
    /// Overrides the OnPaint.
    /// </summary>
    /// <param name="e">The PaintEventArgs contains the event data.</param>
    protected override void OnPaint(PaintEventArgs e)
    {
        if (this.ItemImage != null)
        {
            SizeF textSize = e.Graphics.MeasureString(this.Text, this.Font);
            int textwidth = (int)Math.Ceiling(textSize.Width);
            //Draw the Menu item image
            e.Graphics.DrawImage(this.ItemImage, new Rectangle(this.Width - 70, 10, 30, 30));
        }
        base.OnPaint(e);
    }
}

VB

'To specify the TreeMenuItem image
Me.treeMenuItem1.ItemImage = Me.imageListAdv1.Images(0)
Me.treeMenuItem2.ItemImage = Me.imageListAdv1.Images(1)
Me.treeMenuItem3.ItemImage = Me.imageListAdv1.Images(2)
Me.treeMenuItem4.ItemImage = Me.imageListAdv1.Images(3)
 
''' <summary>
''' Creating custom class
''' </summary>
Public Class TreeMenuItemEx
    Inherits TreeMenuItem
    Public Sub New()
        'Initialization
    End Sub
 
    ''' <summary>
    ''' Initialize the item image.
    ''' </summary>
    Private m_ItemImage As Image = Nothing
 
    ''' <summary>
    ''' Gets or sets the item image.
    ''' </summary>
    Public Property ItemImage() As Image
        Get
            Return m_ItemImage
        End Get
        Set(ByVal value As Image)
            m_ItemImage = value
        End Set
    End Property
 
    ''' <summary>
    ''' Overrides the OnPaint.
    ''' </summary>
    ''' <param name="e">The PaintEventArgs contains the event data.</param>
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        If Me.ItemImage IsNot Nothing Then
            Dim textSize As SizeF = e.Graphics.MeasureString(Me.Text, Me.Font)
            Dim textwidth As Integer = CInt(Fix(Math.Ceiling(textSize.Width)))
            'Draw the Menu item image
             e.Graphics.DrawImage(Me.ItemImage, New Rectangle(Me.Width - 70, 10, 30, 30))
        End If
        MyBase.OnPaint(e)
    End Sub
End Class

Screenshot

 Show TreeMenuItem with image

Figure 1. TreeMenuItem shows with Image.

Samples:

C#: TreeMenu_Image_C#

VB: TreeMenu_Image_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon