VS2019中.NET 3.1 LTS WinForm项目工具箱MainMenu控件缺失的解决方法求助
Hey there, let's get that missing MainMenu control sorted out for your .NET 3.1 WinForms app. I've run into this exact issue before, so here are a few reliable ways to fix it:
Method 1: Correctly Add MainMenu to Toolbox
The MainMenu control isn't listed by default in the .NET Core 3.1 WinForms toolbox, but you can manually add it by following these precise steps:
- Right-click any empty area in the Toolbox, then select Choose Items...
- In the pop-up window, click the Browse button
- Navigate to the
System.Windows.Forms.dllfile for .NET Core 3.1. You'll typically find it in this path:C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App\3.1.xx\(replacexxwith your installed 3.1 version number) - Select the DLL and click Open. Now you'll see MainMenu in the component list—check its box and hit OK
- After this, the MainMenu control should appear in your Toolbox, ready to drag onto your form.
Method 2: Create MainMenu via Code (Bypassing Toolbox)
If the Toolbox method still doesn't work, you can directly create and attach the MainMenu using code—this is actually a quick workaround that avoids Toolbox glitches:
Add this code to your form's constructor (right after InitializeComponent();):
// Initialize MainMenu and menu items var mainMenu = new MainMenu(); var fileMenu = new MenuItem("&File"); var openItem = new MenuItem("&Open"); var exitItem = new MenuItem("E&xit"); // Build the menu hierarchy fileMenu.MenuItems.Add(openItem); fileMenu.MenuItems.Add(exitItem); mainMenu.MenuItems.Add(fileMenu); // Attach the menu to your form this.Menu = mainMenu;
Run your app, and you'll see the MainMenu appear at the top of your form. You can also add click event handlers to the menu items just like any other control.
Method 3: Reset the Toolbox
Sometimes the Toolbox gets stuck with cached data. Try resetting it first before re-adding the control:
- Right-click an empty spot in the Toolbox
- Select Reset Toolbox
- Once the Toolbox resets, repeat Method 1 to add MainMenu again.
Quick Note: Consider MenuStrip Instead
Just a heads-up—Microsoft recommends using the MenuStrip control (which is in the default Toolbox) for .NET Core WinForms projects. It's the modern replacement for MainMenu, with better support for features like icons, keyboard shortcuts, and dynamic menu items. If you don't have a specific requirement for MainMenu, switching to MenuStrip might save you some hassle!
内容的提问来源于stack exchange,提问作者raspiduino

