0

In my solution, I have multiple Forms/Views and wanted to use global properties to set the basic form control properties (font name, size, color, etc).

The Designer.cs file is a partial class, which is obviously apart of the main partial class when you do "View Code." This is the class which inherits from "Form." So what I've done was created another class file which I call ViewMaster.cs, and inherit "Form" from that and set my ViewMaster class as the inherited class for my form.

MembershipView.cs

public partial class MembershipView : ViewMaster {
    ...
}

ViewMaster.cs

public class ViewMaster : Form {
    public Font fontButton = new Font("Microsoft Sans Serif", 11F, FontStyle.Regular, GraphicsUnit.Point, 0);
    public Font fontLabel = new Font("Microsoft Sans Serif", 11F, FontStyle.Regular, GraphicsUnit.Point, 0); // new Font("Arial", 18F, FontStyle.Regular, GraphicsUnit.Point, 0);
    public Font fontGrid = new Font("Microsoft Sans Serif", 10F, FontStyle.Regular, GraphicsUnit.Point, 0);
    public Size sizeButton = new Size(125, 28);
}

MembershipView.Designer.cs

partial class MembershipView {
    ...
    this.label5.Font = fontLabel;
    ...
    this.btnSearch.Font = fontButton;
    ...
}

Now this works when I click "Start" or "Start Debugging", although when I double click on the Form Viewer (MembershipView.cs in my case), I get errors like:

  • The variable 'fontLabel' is either undeclared or was never assigned.

  • The variable 'fontButton' is either undeclared or was never assigned.

  • The variable 'sizeButton' is either undeclared or was never assigned.

  • The variable 'fontGrid' is either undeclared or was never assigned.

I am able to hit "Ignore and Continue." at this point, although the size / font within the designer / form view 8.25, so it obviously doesn't read from my inherited properties. Again - it all works when I hit "Start."

Anyone have any suggestions to resolve this?

Thanks! Mike

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Mike J
  • 425
  • 4
  • 15
  • I posted an answer to the question. For more information take a look at this post: [Can't view designer when coding a form in C#](http://stackoverflow.com/a/32299687/3110834) and also see the interesting *Example* part of that post. It would help you to get a better understanding about how the designer works. – Reza Aghaei Jul 15 '16 at 12:45

2 Answers2

1

I have had the same problem and I fixed it. Actually Visual Studio only works with X86 controls and you can't create a user control in X64 mode and use it.

You should add a new class library in Any CPU mode and build the class library. then you can add its DLL in your project. Done.

If it doesn't you must go to the Configuration manager and set the Active solution platform to X64 also do that for all subprojects. Remember that build option has to be checked. and go to the properties of the class library and click on the build tab. then set the platform target to Any CPU.

Ahadi
  • 17
  • 6
0

The designer can't load because when the designer deserializes the codes in the MembershipView.Designer.cs file, it sees this.label5.Font = fontLabel;, so it looks for declaration of fontLabel field in the file and also looks to find the initialization of field in InitializeComponent of the file. But the declaration and the initialization doesn't exist in the file so the designer can't load correctly.

For more information about how the designer work, take a look at this post. The post also contains a very interesting example:

What should I do to have some base settings like Font, Size?

Instead of putting such settings in a base from, you can use property binding to application settings. This way you can create some base settings like ButtonFont of Font type or ButtonSize of Size type and then using property grid at design-time, bind the Font and Size property of Button controls to those settings properties. You can also use these settings for other properties of your controls.

To learn more about property binding to application settings, take a look at this post:

Community
  • 1
  • 1
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398