Visual Studio 2022 gives error "'Site' must not be null in order to access a required service." when trying to add menu item

huangapple go评论55阅读模式
英文:

Visual Studio 2022 gives error "'Site' must not be null in order to access a required service." when trying to add menu item

问题

我有一个 contextMenuStrip,经常(不总是,但我还没有注意到可重复的模式)在尝试在“Type Here”字段中键入时出现错误对话框:

> 必须不为 null 以访问所需的服务。

当我点击光标进入字段时,它会立即发生。

有人知道是什么原因吗?

英文:

I've got a contextMenuStrip and often (not always, but I haven't noticed a repeatable pattern yet) I get the error dialog:

> 'Site' must not be null in order to access a required service.

when trying to type in the "Type Here" field. It happens the second I click the cursor into the field.

Visual Studio 2022 gives error "'Site' must not be null in order to access a required service." when trying to add menu item

Anyone have any idea what's causing this?

答案1

得分: 1

以下是翻译好的部分:

这让问题对我来说不再存在。我不知道是哪个步骤解决了问题,但现在似乎正常工作了。

首先,设计师所做的只是为您编写[FormName].Designer.cs页面。您可以自己添加子项的条目,VS似乎对此没有问题。我开始做这个,看了一个以前的项目,该项目拥有菜单子项(在VS22中完美工作),作为一个模型。

  1. 在您的设计文件底部的对象定义中,添加菜单项的声明。
  2. 在隐藏部分,在实例化中,添加一行或多行以创建您的菜单项。
  3. 此时我注意到较旧的、正常工作的项目中,所有的实例化都以this为前缀(即this.LogFileToolStripMenuItem=new ToolStripMenuItem),对于需要子菜单项的第二个菜单项,我添加了this,然后重新进入设计师,一切都正常工作了(而且继续如此,没有错误消息)。我不知道这其中哪一部分解决了问题,但当我回到设计文件查看如果删除了this会发生什么时,设计师编辑器已经自动去掉了this,但一切仍然正常。
  4. 您需要为工具栏菜单项设置三个属性。您可能可以复制其中一个现有的菜单项,然后根据您的新toolstripmenuitem进行更改。
  5. 您需要做的最后一件事是将子菜单添加到父菜单中。在父菜单的属性定义部分,添加一个AddRange行:

cmsLogging.DropDownItems.AddRange(new ToolStripItem[] { cmsLogging_LogFile, cmsLogging_ShowLogFile, cmsLogging_ShowLogFileDirectory });

在此之后,重新启动表单设计器,菜单项就会出现,一切都看起来正常。作为奖励,错误消息也消失了。这对我有效。

英文:

This made the problem go away for me. I do not know which step did it, but it seems to be working normally now.

First, all the designer does is write the [FormName].Designer.cs page for you. You can always put the entries in for the subitems in yourself and VS seems to be fine with that. I started this, looking at an older project that had menu subitems (and worked perfectly in VS22) as a model.

  1. In the object definitions at the bottom of your Designer file, add the declaration for the menu item(s).
  2. In the hidden section, in the instantiations, add a line(s) to create your menu item(s).
  3. It was at this point I noticed the older, working project all the instantiations were prefixed with this (i.e. this.LogFileToolStripMenuItem=new ToolStripMenuItem) and on a second menu item that needed submenu items I added the this and went back into the designer and everything worked normally (and continued to do so, no error message) from there on out. I don't know which part of this fixed it but when I went back into the designer file to see what happened if I removed the this the designer editor had already stripped the this out, but everything still worked.
  4. You need to set the three properties for a tool strip Menu Item. You can probably copy one of the existing ones and just change things to fit your new toolstripmenuitem.
  5. The last thing you need to do is add the submenus to the parent. In the property definition section for the parent, add an AddRange line:

cmsLogging.DropDownItems.AddRange(new ToolStripItem[] { cmsLogging_LogFile, cmsLogging_ShowLogFile, cmsLogging_ShowLogFileDirectory });

After this and restarting the form designer the menu items were present and everything looked normal. As a bonus the error message is gone away. It worked for me.

huangapple
  • 本文由 发表于 2023年5月18日 10:59:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定