Manual Editing of *.Designer.Cs File

Manual editing of *.designer.cs file

You should never modify .designer.cs. Period. Your changes will be overwritten without mercy.

Update: To be a bit more helpful, C# since v3 (VS 2008) has included partial methods, which many designers will now use to let you implement custom behavior.

Visual Studio how bad is it to add code in the designer files?

The comment in the designer file should say enough:

This code is generated. Modifications will be lost when this code is regenerated.

Therefore, there is a risk: you can lose your modifications and / or additions.

Do note, some generators use #region markup to mark what they will generate. For example LLBLgen. However usually these files are not named with .designer.cs.

As for the discussion, a few arguments:

  • usually designer generated files are partial. So the class can be edited in the other file. If something in the designer file needs changes, best is to use the generator tool instead.
  • Code quality tools like Stylecop and code metrics usually are configured to ignore designer generated files. You might miss important feedback.

Updating web code causes Designer.cs file to replace WebControls.ListItem with HtmlControls.HtmlGenericControl

I had the same problem. I changed some part of my ASP.net project and after that I reached this problem :

The base class includes the field 'Option1', 
but its type (System.Web.UI.HtmlControls.HtmlGenericControl) is not compatible
with the type of control (System.Web.UI.WebControls.ListItem).

For fixing this problem, I modified this file <MyFileName>.aspx.designer.cs and I searched Option1 and I have replaced

protected global::System.Web.UI.HtmlControls.HtmlGenericControl.ListItem Option1;

with

protected global::System.Web.UI.WebControls.ListItem Option1;

Finally, I built my solution and my problem have gone.

.designer file not being associated with .cs file in visual studio?

First step: right-click the files and exclude them, and try including them back in. It may fix itself. If not, you could edit the project directly; you just to add the dependency that the CSPROJ or VBPROJ expects. Here is an example that you need to make sure is in the project XML:

<Compile Include="Views\Main.aspx.designer.vb">
<DependentUpon>Main.aspx</DependentUpon>
</Compile>
<Compile Include="Views\Main.aspx.vb">
<DependentUpon>Main.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
</Compile>

how to use resources manually in C#(no IDE act)?

well finally i found the answer of my question .
i really didn't get the correct and the best answer from searching the internet and asking questions .

its all about resources , everything from outside used in a projection must go through these steps :

  1. imported/downloaded to project folder.

(it's really common to put the files to a Resource folder as a projection sub-folder)


  1. defined/submitted/converted to a local or global projection's .resx file.

(.resx file is a XML)


  1. get a handle from C#.net to be accessible .

(from .resx designer file)

(this is done by defining - usually as a property - in the global or local C#.net .resx designer)

if we assume local .resx as a personal backpack for classes and projection(global) .resx file as a public drawer , every classes provide their resources from their own backpack and the public drawer . by conventional direct procedure of providing resources , Visual Studio IDE don't let the classes use other classes resources . but if you go a little further into .resx files and the designer files this would be possible .

first let me explain how a class can use other classes resources .

when you import files into a local .resx a hard encoded copy of the files imported into the XML of the .resx. you can access XML of the local .resx file if you open the .resx file with a XML-editor .

for example if you imported an icon file into a form
you will see these codes in the XML related to the icon :

(to access the XML-editor in VS just easily right-click the .resx file and click open with then choose XML-editor)

<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing"
mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAkAMDAAAAEACACoDgAAlgAAACAgAAABAAgAqAgAAEAPAAAYGAAAAQAIAMgGAAD
oFwAAEBAAAAEACABoBQAAsB4AAAAAAAABACAA510BABgkAAAwMAAAAQAgAKglAAAAggEAICAA
AAEAIACoEAAAqKcBABgYAAABACAAiAkAAFC4AQAQEAAAAQAgAGgEAADYwQEAKAAAADAAAABgAAA
AAQAIAAAAAAAACQAAAAAAAAAAAAAAAQAAAAEAAAAAAAAZHBQAHR4ZABgkFAAaKRYAHyAaAB0rGQ
AeMBkAIiQcACQpHgAiNBwAJDkeACUn
.
.
.
+Pn5D/j5+Q/4+fkP+Pn5D/j5+Q/4+fkP+Pn5D/j5+Q/4+fk
P+Pn5D/j5+Q/4+fkPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AAD//wAA4AMAAAABA
AAAAAAAAAEAAAADAAAAAwAAAAMAAAADAAAAAwAAAAMAAAADAAAAAwAA
</value>
</data>

the file encoded , you can see VS gives it a name ($this.Icon) and specify its type and mimetype there . so these property's now is changeable

for accessing resources in designer or constructor files an object of System.Resources.ResourceManager must be created .
the constructor of ResourceManager has two overload , one of them just use resourceSource type as the input parameter . it has to be the resource type of the host of resources . one can get it like this :

typeof(Form1) ;

so creating a ResourceManager object :

System.Resources.ResourceManager resource = new System.Resources.ResourceManager(typeof(Form1))

the object resource now has a GetObject() method to access .resx imported files by their name , for example for the icon :

((System.Drawing.Icon)(resources.GetObject("$this.Icon")))

if you open the designer file of the form you can see the codes for the icon of the form :

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

resource is a ResourceManager object created in the base form class by the IDE so you can use it here in the designer file easily .

if you want to use for example the icon of form1 in form2 you can refer to it like this :

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
**this.Icon = (System.Drawing.Icon)((new System.Resources.ResourceManager(typeof(Form1))).GetObject("$this.Icon"));***
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);

(look carefully the line started with **)

maybe its better to defined it as a public static property in the form constructor to avoid too much typing of nestings :

public static System.Drawing.Icon icon
{
get
{
return (System.Drawing.Icon)((new System.Resources.ResourceManager(typeof(Form1))).GetObject("$this.Icon"));
}
}

so in the form2 designer :

this.Icon = Form1.icon ;

so far using resources of other classes revealed now lets talk about defining and using global projections resources manually !

there's no hard-copy of the imported files in a global .resx file . there's just a reference to it .

for example you imported icon with the IDE , it creates a Resource sub-folder and put the file in it .
now if you open properties in the solution manager and open the Resource.resx with a XML editor (Right-Click > Open With > XML editor) you can see this code :

<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="PerfCenterCpl" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>

you can see above in the value it specified its reference address and its type and even its type version ! and its culture type and really interesting it has a key !

from its address , it seems files just added to projection for integrity . global resources can be every where in the projection folder , in the computer , in the network or in the internet !

it has a unique key ! seems we can access resources by their key in the project too !

now its time for resource to gets its handle
if you open Resource.resx in the solution manager and open the Resource Designer you can see it gets its handle by a property :

       internal static System.Drawing.Icon icon {
get {
object obj = ResourceManager.GetObject("icon", resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}

so the resource is accessible with Properties.Resources.icon
and doesnt need any type casting :

this.Icon = Properties.Resources.icon ;

now if further processes on the resources needed , global resources can be a method accepting input parameters !

in my problem i give the resource a handle but there isn't any resource reference in the XML

Can a designer-generated method be moved from usercontrol.designer.vb to usercontrol.vb?

In commenting with user @jmcilhinney on my OP, the answer is this:

The Dispose() method can be moved from usercontrol.designer.vb to usercontrol.vb without issue. The reason for this is as follows:

The designer code file ... gets regenerated often and ... that
includes generating a Dispose method if and only if there isn't one in
the user code file.

And so after moving, Dispose can be modified at will, and it will not be regenerated and/or overwritten by the designer by any subsequent changes to the UserControl.

Where to dispose resources in a System.Windows.Form-derived class?

You can move the Dispose() method from the Designer.cs file to your source code file. And alter it to add dispose calls for any disposable members in your form class. This is one of the few cases where it is okay to edit the designer file, you only need to stay away from the code that inside the #region marked "Windows Form Designer generated code".

Form1.Dispose(bool): no suitable method found to override

Your form1.cs file makes Form1 a nested class, to be accessed as WindowsFormsApplication3.Person.Form1. This is because you put the closing brace for Person after Form1's definition. Using any decent editor's code formatting option should make this apparent.

WindowsFormsApplication3.Person.Form1 does not match WindowsFormsApplication3.Form1, so the base class is never seen for the latter.

Windows Form Event Disappearing

This is why you don't edit the designer code. It's specifically designed to not maintain these types of changes.

You should be subscribing to the event in the object's constructor, load event, or some similar location.



Related Topics



Leave a reply



Submit