How to display image on TreeMenuItem in WinForms TreeNavigator?
Display image on TreeMenuItem
In WinForms Tree Navigator, 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
Figure 1. TreeMenuItem shows with Image.
Samples:
Conclusion
I hope you enjoyed learning about how to display image on TreeMenuItem in WinForms TreeNavigator.
You can refer to our WinForms TreeNavigator’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms TreeNavigator documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms TreeNavigator and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!