I'm new to Windows Forms and have a problem with inheriting forms. So I created one base form with multiple controls and events and one form which inherits from the base form. After building the solution and I try to open the designer of the inherited form, I get this error message: "Duplicate component name (the components name). Component names must be unique and case-insensitive."
I don't know what to do :(
CodePudding user response:
All form controls must have a unique name. The designer doesn't allow adding or naming controls after controls that already exists but if you make manual changes to it such as making it inherit another form by editing the Designer.cs file, you can temporarily bypass these safety measures, but when you try to use the visual designer, you will get errors like the one you are getting.
You must fix the name collision manually by finding the control of that name in your form and renaming it (ctrl-r in visual studio to rename the reference) as well as changing the name in the string on the line someControl.Name = "someControl";
Once fixed, you should be able to use the visual designer again. As already mentioned, the inheriting form can't edit the base form by default, however you can change this by going in the base form and changing the Modifiers property from Private to Protected for the controls you want to be edited by the inheriting form. If you then look at the Designer.cs file, you will notice the member for the control in question now has the protected keyword, which allows derived classes to access it. However, this only applies to modifying other properties from the new form, not for fixing the naming collision as the name is also used to name the fields which must be unique at compile-time for any kind of class.