dotnet/winforms

ContextMenuStrip has erroneous behavior if ToolStripMenuItem item Available=false

Open

#7,635 创建于 2022年8月22日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)C# (922 fork)batch import
help wanted

仓库指标

Star
 (4,100 star)
PR 合并指标
 (平均合并 14天 22小时) (30 天内合并 56 个 PR)

描述

.NET version

Net 7.0 Preview1

Did it work in .NET Framework?

No

Did it work in any of the earlier releases of .NET Core or .NET 5+?

no

Issue description

If you add a ToolStripMenuItem to a ContextMenuStrip with the flag 'available' = false already set, it brakes the scrolling behavior.

ErrorDemo

If you set the flag after all items are added it works fine.

Steps to reproduce

Reproduced process: ContextMenuStrip.Items - order //breaks: [0] available = true [1] available = false [2] available = true //works: [0] available = false [1] available = false [2] available = true

Test C# Code

using System;
using System.Windows.Forms;

namespace ContextMenuStripItemAvailableTest
{
	static class Program
	{
		[STAThread]
		static void Main()
		{
			Application.SetHighDpiMode(HighDpiMode.SystemAware);
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			var contextMenuStrip = new ContextMenuStrip
			{
				MaximumSize = new Size(400, 200),//just for easier testing
			};

			var itemManual = new ToolStripMenuItem("item_manual");
			contextMenuStrip.Items.Add(itemManual);

			var itemHidden = new ToolStripMenuItem("item_hidden");

			//breaks the rendering
			itemHidden.Available = false;

			contextMenuStrip.Items.Add(itemHidden);

			for (int i = 0; i < 20; i++)
			{
				var tsi = new ToolStripMenuItem("item_" + i);
				contextMenuStrip.Items.Add(tsi);
			};

			///works fine 
			//contextMenuStrip.Items[1].Available = false; //1=itemHidden

			var frm = new Form()
			{
				StartPosition = FormStartPosition.CenterScreen,
				ContextMenuStrip = contextMenuStrip,
				Width = 600,
				Height = 400,
			};
			Application.Run(frm);
		}
	}
}


PS: There are other rendering bugs that happens if available is used. I can not describe the exact way to reproduce the error and for that reason i can only confirm this behavior for Net Framework 4.8. Example 1: Scrolling is working but if the bottom of the item list is reached the contextmenu expands to the bottom of the screen and the addtionals space is filled with empty items. Example 2: Scrolling works and when you reach the bottom and scrool up again you can not reach the top of the list ever again. From 100 Items you can only go up to 40 and then it jumps down to 100 again. It is just a loop from no on.

贡献者指南