问题
I have a question related to the error on the title. Im working with c# and Visual Studio 2010.
I have a form declared as "public class FormularioGeneral : Form", which is the base for the rest of the forms in my application. When i try to access the Designer View i get this error several times, as you can see in the image:

[...]
this.PanelMargenIzquierdoCapaBase.BackColor = m_ColorCapaBase;
[...]
But all the variables are declared in the same class as read-only properties and all of them are assigned inside a method which is called in the constructor.
Declaration of properties:
protected Color m_VariableName;
public Color VariableName
{
get { return m_VariableName; }
set { }
}
Constructor code:
public FormularioGeneral()
{
ConfigurarUI();
AccionesConstructor();
InitializeComponent();
PostInicializacionComponentes();
EstablecerIcono();
InicializarLocalizacionFormulario();
}
ConfigurarUI method:
public virtual void ConfigurarUI()
{
[...]
m_AltoBordeSuperiorCapaBase = 30;
m_AltoBordeInferiorCapaBase = 7;
m_AnchoBordesLateralesCapaBase = 7;
m_ColorCapaBase = Color.FromArgb(50, 100, 150);
m_ColorTextoCapaBase = Color.White;
m_ColorTextoBotonAplicacion = Color.Black;
m_FuenteTextoIzquierdoCapaBase = new System.Drawing.Font("Verdana", 11.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
m_FuenteTextoCentroCapaBase = new System.Drawing.Font("Verdana", 14.0F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
[...]
}
So, as far as i know, all the variable which are giving the errors are correctly declared and have a value assigned before the InitilizeComponent function is called.
Im stuck at this point and dont know what to do to solve the problem. Hope some of you can help me with this issue.
回答1:
So, I have had the same problem in the past, for fix I did the following:
- Solution → Clean Solution;
- Build → Rebuild Solution;
- Close Visual Studio, re-open.
Thanks a lot to Marshall Belew!
回答2:
I ran into this error because my project is x64 only. Apparently Visual Studio, being a 32bit application, cannot load any forms or controls compiled to 64bit in the designer. It makes total sense, but the error gives you no indication that is the problem.
See the answer to Visual studio designer in x64 doesn't work.
The workaround is to change your project to Any CPU when designing, then back when building.
回答3:
Maybe the error occurs due to your constructor code. Place InitializeComponent();
at the beginning of the constructor like this:
public FormularioGeneral()
{
InitializeComponent();
ConfigurarUI();
AccionesConstructor();
PostInicializacionComponentes();
EstablecerIcono();
InicializarLocalizacionFormulario();
}
Explanation:
The variables are initialized in that method.
回答4:
In my case, I had an older Windows Forms project where InitializeComponents()
started like this:
private void InitializeComponent()
{
var componentResourceManager = new ComponentResourceManager(typeof(MyForm));
...
This resulted in an error message later on when accessing the componentResourceManager
inside InitializeComponent()
:
The variable 'componentResourceManager' is either undeclared or was never assigned.
When comparing with a newly created form, I saw that it was similar to my non-working form, except for one thing:
The variable was not named componentResourceManager
but simply resources
.
Solution
After doing a rename on my variable to also have the name resources
, everything works successfully:
private void InitializeComponent()
{
var resources = new ComponentResourceManager(typeof(MyForm));
...
The Windows Forms Designer in Visual Studio 2017 did open the form correctly.
回答5:
I had the same problem and cleaning and rebuilding did not work for me.
In my case the problem was caused by the Visual Studio designer loading referenced DLLs from the GAC instead of loading them from the <HintPath> directory specified in the .csproj file. The DLLs in the GAC did not have the same version as the locally stored DLLs.
When I updated the DLLs in the GAC to have the same version everything worked OK again.
回答6:
I had this issue when my user control had some code in the constructor which was related to runtime resource. I added null check and it fixed.
InitializeComponent();
if (MyConfig!= null)
{
this.label2.Text = MyConfig.text1;
this.label3.Text = MyConfig.text2;
this.label1.Text = MyConfig.text3;
}
回答7:
Don't put anything other than InitializeComponent();
in the constructor. You can put the code from there in events like Load()
.
回答8:
User Controls were caused problem and after trying all suggestions, (Focus solution then Alt+Enter) changing solution's Platform Target from x64 to Any CPU solved the problem.
回答9:
About the variables, can you simply initialize them in the declaration? I think that would suffice, even if you change the value later. From what I'm seeing, the compiler is unable to check whether you have initialized them or not because it's not directly on the constructor code, it's being done on a virtual method which will evaluate only at runtime.
So, instead of:
protected Color m_VariableName;
public Color VariableName
{
get { return m_VariableName; }
set { }
}
Do:
protected Color m_VariableName = Color.White; // Or null
public Color VariableName
{
get { return m_VariableName; }
set { }
}
And a comment: you should avoid virtual calls in the constructor, which can lead to obscure errors in your application. Check it here.
回答10:
This error occurs for me while creating a third party control in InitializeComponent() which is called from form constructor. When I created it after InitializeComponent() it works fine for me.
public MyForm() //Form constructor
{
InitializeComponent();
//Create/initialize third party control here with new operator
}
回答11:
I am working with WPF inside of Windows Forms.
I hosted my WPF User Control in a Windows Forms Element Host. Due to that when InitializeComponent() got called I executed Code before reaching the InitializeComponent() of my WPF control. Tricky.
So I moved it out of my constructor, Clean Build, Rebuild, Restart VS and everything works as expected. Finally.
回答12:
In my case, I added a third party control in my Toolbar(via a .dll file), and draw one of it in my form. And for some reason, my Toolbar clean this third party control out of the general group(I added it in the general group), so VS cannot find this control. Here is what I done to slove this problem:
- Add this control into the Toolbar.
- Clean the solution
- Rebuild the solution
Redraw the control if neccessary.
回答13:
First I had code that referenced something a type that the designer couldn't load (for whatever reason). Then I had code in the constructor that couldn't be executed from my local laptop. I decided the best option was to move the logic to the Load event and check if the component was in DesignMode and exit if it was.
Even this wasn't enough for me as the designer still tried to JIT the type that was later down in the method, so I had to move it out to a separate method to stop that from happening. Here's basically what I ended up with:
private void userControl_Load(object sender, EventArgs e)
{
if (DesignMode) return;
Initialize();
}
private void Initialize()
{
// do your work
}
Special thanks to this SO answer which pointed me to a comment in a blog post about not accessing the DesignMode property until you're in the Load event...
回答14:
Renaming the variable componentResourceManager to resources solved error.
Unfortunately i had to change a ton of other items to get the designer working for Telerik reports designer
回答15:
In my Solution i had wrong Reference paths which i fixed in the .csproj files. After fixing that i could finally load the Form again.
回答16:
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.
来源:https://stackoverflow.com/questions/8521600/the-variable-variable-name-is-either-undeclared-or-was-never-assigned