Icon on caption

Hello
I'm looking for way to add an icon in a header of ContextMenuStripEx .. 
is there any way to do it?

Thanx
Alessandro.



7 Replies 1 reply marked as answer

DV Duraimurugan Vedagiri Syncfusion Team July 15, 2020 09:51 AM UTC

Hi Alessandro ,

Thanks for contacting syncfusion support.

You can achieve your requirement "How to add an icon in header of ContextMenuStripEx" by render the customized ContextMenuStripEx class and it is inherit from ContextMenuStrip. Please refer the below code snippet and sample for reference.

public class ContextMenuStripEx : ContextMenuStrip 
{ 
    public override Rectangle DisplayRectangle 
    { 
        get 
        { 
            Rectangle rc = base.DisplayRectangle; 
            rc.Y += TitleHeight; 
            return rc; 
        } 
    } 
 
    public override Size GetPreferredSize(Size proposedSize) 
    { 
        Size szResult = base.GetPreferredSize(proposedSize); 
        szResult.Height += TitleHeight; 
        return szResult; 
    } 
 
    internal int TitleHeight 
    { 
        get { return 25; } 
    } 
 
    protected override void OnPaint(PaintEventArgs e) 
    { 
        base.OnPaint(e); 
        Image image = Image.FromFile(@"..\..\Images\ExitIcon.png"); 
        e.Graphics.FillRectangle(new SolidBrush(Control.DefaultBackColor), 1, 1, this.Width - 2, TitleHeight); 
        Rectangle imgRect = new Rectangle(2, 2, 16, 16); 
        e.Graphics.DrawImage(image, imgRect); 
        Rectangle textRect = new Rectangle(imgRect.Width + 2, 2, this.Width, TitleHeight); 
        e.Graphics.DrawString(Text, this.Font, new SolidBrush(Color.Black), textRect); 
    } 
} 



Sample : https://www.syncfusion.com/downloads/support/forum/156024/ze/ContextMenuStripEx872438820.zip  

Regards,
Duraimurugan V

Marked as answer

AB Alessandro Breschi July 20, 2020 04:20 PM UTC

Hello, Duraimurugan
Thanx for replay... this is perfect...

I have another request is possible to reverse(  changing the ForeColor of selected item?).
In this case white ?
Best regards-
Alessandro.


VR Vijayalakshmi Roopkumar Syncfusion Team July 21, 2020 07:55 AM UTC

Hi Alessandro, 
 
Thank you for your update. 
 
Query : I have another request is possible to reverse(  changing the ForeColor of selected item?). 
In this case white ? 
 
We have analyzed your query regarding how to change the foreground of the selected item of ToolStripMenuItem. To achieve this behavior, you need to invoke the ItemClicked event of ContextMenuStrip and handle the forecolor property of ToolStripMenuItem by casting the e.ClickedItem. Please find the code and sample for the same below: 
 
Code :[C#] 
 
//Invoke Itemclicked event 
private void ContextMenuStripEx_ItemClicked(object sender, ToolStripItemClickedEventArgs e) 
{ 
ToolStripMenuItem menuItem; 
menuItem = e.ClickedItem as ToolStripMenuItem; 
//set forecolor for the selected MenuItem 
menuItem.ForeColor = Color.Red; 
 
} 
 
 
 
 
Please try this solution and changed the forecolor as per your desire.  
 
Regards, 
Vijayalakshmi VR 



AB Alessandro Breschi July 21, 2020 11:03 AM UTC

Thanks for the reply, 
but the suggested event is fired when the user selects the item,
I hope change the fore color whe i moving the menu.
I use this event to do action .
In addition the link for example is invalid.
Best Regard.
Alessandro.


VR Vijayalakshmi Roopkumar Syncfusion Team July 21, 2020 01:41 PM UTC

Hi Alessandro,

Sorry for the inconvenience

Here we have attached the correct sample for ContextMenuStrip Forecolor:

Sample: https://www.syncfusion.com/downloads/support/forum/156024/ze/ContextMenuStripExselectedforecolor271102355

Please try this solution and let us know if it is helpful.

Regards,
Vijayalakshmi VR. 



AB Alessandro Breschi July 22, 2020 02:42 PM UTC

hello, Vijayalakshmi
the attach example work .. but the functionality is no good .
The event suggested close the menu.
I found a solution adding every the menu are create two event handler at ToolStripItem to intercept MouseEnter and MouseLeave .

I don't like this solution very much,
I would like to be able to work on a contextmenu event and not continuously add handlers
to objects that will be immediately destroyed
Have you a better solution?

Best Regards.
Alessandro.


Code Conncept.
Dim i As Integer
         Dim MenuItem As ToolStripItem

             MenuItem = ContextMenuStripEx1.Items.Add("hello");

            AddHandler MenuItem.MouseEnter, AddressOf ItemEnter
            AddHandler MenuItem.MouseLeave, AddressOf ItemLeave


             MenuItem = ContextMenuStripEx1.Items.Add("ciao");

            AddHandler MenuItem.MouseEnter, AddressOf ItemEnter
            AddHandler MenuItem.MouseLeave, AddressOf ItemLeave


 
Private Sub ItemEnter(sender As Object, e As EventArgs)
        CType(sender, ToolStripItem).ForeColor = Color.White
    End Sub
    Private Sub ItemLeave(sender As Object, e As EventArgs)
        CType(sender, ToolStripItem).ForeColor = Color.Black
    End Sub



BR Backia Raj Kanagaraj Syncfusion Team July 23, 2020 10:05 AM UTC

Hi Alessandro, 
  
Thank you for your update. 
  
From your update, we have understood that you don't want to close the menu , when the item get clicked. In our previous update, we have suggested to set the forecolor of ToolStripMenuItem in ItemClicked event which does close the Menu when it lost activation. To avoid this behavior, you can set the AutoClose property to false, which will restrict the closing of menu after selecting the item. We have modified the sample for the same and it can be downloaded from following location: 
  
  
  
You can refer the functionality of AutoClose behavior in the following location: 
  
  
  
Please try this solution and let us know if is helpful. 
 
Regards, 
Backia Raj Kanagaraj

Loader.
Up arrow icon