Notifications
Clear all

[Closed] ToolStripMenuItem.BackColor not working?

Hi guys,

Has anyone else come across this possible bug with dotNet and ToolStripMenuItem.BackColor not working.

See below for the function that I am using to create the contextMenu and how I am applying the backColor to the MenuItem.
If I try to change any of the other methods it works but this doesn’t


fn uI_ContextMenu cmItmArray:Undefined cmAccessArray:Undefined cmImgArray:Undefined cmImgSize:10 cmBgClr:Undefined=(
	if cmItmArray!=Undefined then
	(
		dNet_ContextMenu=dotNetObject"System.Windows.Forms.ContextMenuStrip"
		for a=1 to cmItmArray.Count do
		(
			if classOf cmItmArray[a]== Array Then
			(
				temp_Item_Array=#()
				temp_Sub_Item=#()
				temp_Obj=(dNet_ContextMenu.Items.Add(cmItmArray[a][1]))
				append temp_Item_Array temp_Obj
				for b=1 to cmItmArray[a][2].Count do
				(
					temp_Sub_Obj=(dNet_ContextMenu.Items.Get_Item (a-1)).DropDownItems.Add cmItmArray[a][2][b]
					append temp_Sub_Item temp_Sub_Obj
					if cmBgClr!=Undefined do
					(
	-- 							temp_Sub_Obj.Checked=True
						temp_Sub_Obj.BackColor=(dotNetClass "System.Drawing.Color").fromARGB cmBgClr.r cmBgClr.g cmBgClr.b
					)
				)
				append temp_Item_Array temp_Sub_Item
				append cmAccessArray temp_Item_Array
				if cmImgArray!=Undefined do
				(
					cmAccessArray[a][1].Image=cmImgArray[a][1]
					for b=1 to cmImgArray[a][2].Count do
					(
						cmAccessArray[a][2][b].Image=cmImgArray[a][2][b]
					)
				)
			)
			Else
			(
				temp_Obj=(dNet_ContextMenu.Items.Add(cmItmArray[a]))
				append cmAccessArray temp_Obj
				if cmImgArray!=Undefined do(cmItmArray[a].Image=cmImgArray[a])
			)
		)
		if cmBgClr!=Undefined do
		(
			dNet_ContextMenu.BackColor=(dotNetClass "System.Drawing.Color").Gray
		)
		dNet_ContextMenu.ImageScalingSize=dotNetObject"System.Drawing.Size"cmImgSize cmImgSize
		dNet_ContextMenu.width=cmImgSize
		return dNet_ContextMenu
	)else(Print "No objects have been passed to the context Menu")
)

Any suggestions would be much appreciated!

Cheers,

Shea.

2 Replies

you can use ToolStripMenuItem Backcolor only in custom drawing mode. In normal paint mode the item’s backcolor retrieved from the parent control (menu).

here is a sample how to use Paint event to draw an Item:


try(form.Close()) catch()
(	
	global form = dotnetobject "Form"
	form.Text = "RC for Context Menu"
	menu = dotnetobject "ContextMenuStrip"

	i = menu.items.add "File"
	i.BackColor = i.BackColor.Orange
	i.ForeColor = i.ForeColor.Brown

	fn onPaint s e = 
	(
		if not s.Selected then
		(
			bb = dotnetobject "System.Drawing.SolidBrush" s.Backcolor
			fb = dotnetobject "System.Drawing.SolidBrush" s.Forecolor
		)
		else
		(
			bb = dotnetobject "System.Drawing.SolidBrush" s.Backcolor.Green
			fb = dotnetobject "System.Drawing.SolidBrush" s.Forecolor.Yellow
		)
		
		r = e.ClipRectangle
		e.Graphics.FillRectangle bb r
		sf = dotnetobject "System.Drawing.StringFormat"
		sf.LineAlignment = (dotnetclass "System.Drawing.StringAlignment").Center
		rect = dotnetobject "System.Drawing.RectangleF" (r.x+34) r.y (r.width-34) r.height
		e.Graphics.DrawString s.Text s.Font fb rect sf
	)
	dotnet.addEventHandler i "Paint" onPaint

	menu.items.add "Edit"
	menu.items.add "Exit"

	form.ContextMenuStrip = menu
		
	MaxHandleWrapper = dotnetobject "MaxCustomControls.Win32HandleWrapper" (dotnetobject "System.IntPtr" (windows.getmaxhwnd()))
	form.Show MaxHandleWrapper
)