-2

I'm learning C# with visual studio and i want to go a little deeper.

I want to know about how to use project resources manually? Not by IDE. I searched the site but I just saw this dudes post How to create and use resources in .NET but it's not what I want.

I defined a projection for myself to set a form icon manually.

I tried this :

  1. first I added a folder to project and renamed it to Resources
  2. then I added an .ico file and renamed it to icon
  3. then I defined it in Resources.Designer.cs like this

        internal static System.Drawing.Icon icon {
        get {
            object obj = ResourceManager.GetObject("icon", resourceCulture);
            return ((System.Drawing.Icon)(obj));
        }
    }
    
  4. I added the icon in the form designer like this :

    this.Icon = Properties.Resources.icon;
    

Everything seems fine and the icon phrase is available in the VS intellisense when I typed Properties.Resources., but at first execution attempt the icon didn't set

And in other attempts I received this error :

The type 'setting_icon_from_resources.Properties.Resources' has no property named 'icon'. C:\Users\mehdi\Documents\Visual Studio 2012\Projects\setting icon from resources\setting icon from resources\Form1.Designer.cs

What am I doing wrong ?

Community
  • 1
  • 1
Mehdi
  • 329
  • 1
  • 3
  • 14
  • You should not edit the .Designer.cs files until you know exactly what you're doing. Try adding a resource file to your project by going into the properties of it, going to the resources tab, and then adding the project resource file there. Then edit that file and add the icon to it (by editing the resource file, not the designer.cs file), this should take care of everything for you. – Lasse V. Karlsen Aug 22 '15 at 15:56
  • @PhilipStuyck I don't see anything wrong in what he is trying to do. Maybe he meant "I'm learning the internals of C#". Please don't discourage anyone when they are trying to learn more about something. Personally, I'd learn a language better when I know how it works. I don't see anything wrong in learning this to understand the internals of C# – Rakshith Ravi Aug 22 '15 at 15:56
  • @PhilipStuyck i asked for the answer not for learning procedure , i already know how to add an icon by IDE and many other elemantries – Mehdi Aug 22 '15 at 15:57
  • 1
    @RakshithRavi you shouldn't edit designer files. That is one of the main reason `partial` classes were invented, so we never have to touch designer files. Discouraging someone from doing exactly what the comments of the file recommend you never do seems like good advice to me. – dmeglio Aug 22 '15 at 15:59
  • @LasseV.Karlsen i already know how to use IDE for this and other things – Mehdi Aug 22 '15 at 16:00
  • @dman2306 Agreed. You shouldn't edit designer files as it might result in other unexpected results. However, I don't see anything wrong in editing it just for the sake of learning and knowing how things work. Personally, I didn't know how to place controls manually unless I saw and edited the designer file for a `Form`. Its just a way to learn – Rakshith Ravi Aug 22 '15 at 16:03
  • @RakshithRavi you are completely wrong , theres no problem in editing Designers as you do it easily with forms designers . seems your an IDE addicted . step out of the IDE . if you add for example a background image to youre resources you will saw codes in Designers edited predictably – Mehdi Aug 22 '15 at 16:06
  • 1
    @Mehdi no you are wrong. The chance is pretty high you are going to mess up and find yourself in a situation where you are no longer able to make it work. Are you using a version control system to save your back when things go wrong ? – Philip Stuyck Aug 22 '15 at 16:09
  • 1
    @Mehdi check [this](http://stackoverflow.com/questions/16161994/manual-editing-of-designer-cs-file) link and the first answer for it. Please research – Rakshith Ravi Aug 22 '15 at 16:10
  • Have you verified that the property you added is still there? – Lasse V. Karlsen Aug 22 '15 at 16:10
  • @LasseV.Karlsen yep its there , i even checked the Resources folder – Mehdi Aug 22 '15 at 16:14
  • Also note that the .Designer.cs file output from a .resx file is overwritten in its entirety, none of that file is supposed to be edited. Portions of a .designer.cs file form a *forms* designer is reasonably safe to edit but not the output from the resource code generator. – Lasse V. Karlsen Aug 22 '15 at 16:14
  • 3
    @Mehdi also, you seem to think using IDE makes you bad at programming and using the command line makes you a better programmer. Please understand that an IDE is just a tool and does not, in anyway, affect your programming skills. Using a tool always makes your life easier. Its much easier to cut a tree with an axe than using your bare hands. You can call your self strong by using your hands but at the end of the day, the one who used an axe will cut more trees than you and eventually, earn more than you. Please don't get influenced by sayings like this. See what is right for yourself – Rakshith Ravi Aug 22 '15 at 16:15
  • Check the code in the forms designer, does it still look like you wrote it? Can you access the icon from the constructor of the form? Is the form in the same project as the resource file? – Lasse V. Karlsen Aug 22 '15 at 16:16
  • @RakshithRavi lol , its really clear philosophy , if designer files must not be edited so why its available for editing in IDE !? – Mehdi Aug 22 '15 at 16:16
  • Can we stop this? We're just flooding this comment section. Let's focus on the question at hand. I just wanted to point out that you should not edit designer files on an ideal situation but there is nothing wrong in editing them for learning purposes. Let's just leave it at that – Rakshith Ravi Aug 22 '15 at 16:20
  • @RakshithRavi i have no problem in using IDEs as i use them already , the problem is when you mastered any IDE learning how IDEs works is ordinary . in this way of thinking no one can design an IDE . i didnt mean usuing IDE is bad and users of IDE are newby . dude i just wanted to go deeper just this – Mehdi Aug 22 '15 at 16:20
  • @LasseV.Karlsen nope theres no change in codes in designers and yes i saw the icon phrase in VS intellisense and yes both form and resources and the designer is in one project folder – Mehdi Aug 22 '15 at 16:23
  • And can you access the icon from the constructor of the form instead? ie. just moving the line of code from the .designer.cs file to the constructor in the .cs file? – Lasse V. Karlsen Aug 22 '15 at 16:24
  • @LasseV.Karlsen yes i typed this in both form constructors class and formload method : Properties.Resources. and the intellisense shows me icon phrase , seems intellisense shows everything defined in the designer file – Mehdi Aug 22 '15 at 16:26
  • Clearly the compiler disagrees with you but the code and everything you'e said so far indicates that it should work, so I believe the problem is in some other part of the code. Please check that you've put the icon property in the right class, not inside a #ifdef type of compiler directive, etc. – Lasse V. Karlsen Aug 22 '15 at 16:28
  • @LasseV.Karlsen everything doubled checked . the error disappear when i rebuild the project ! but still no icon seted – Mehdi Aug 22 '15 at 16:38
  • Place a breakpoint in the method then, ensure it actually returns the icon file. – Lasse V. Karlsen Aug 22 '15 at 16:39
  • @LasseV.Karlsen no icon type got from code . i think its a good approach to solve this to know how visual studio set an icon to a form , when i set the icon with IDE it adds this code to the form designer : this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); no file name , and nothing added to Resources , so how the designer understand where to get the icon ? i think there must be a XML that resources like this defined in it – Mehdi Aug 22 '15 at 16:58
  • 1
    Why can't you just use the designer in Visual Studio and move on? – Lasse V. Karlsen Aug 22 '15 at 17:13
  • @LasseV.Karlsen just a step deeper in C# . i think if i understand whats going on there there might be other benefits too – Mehdi Aug 22 '15 at 17:26
  • @LasseV.Karlsen i found it . i posted it as an answer . maybe its interesting a usable for you take a look – Mehdi Aug 22 '15 at 23:46

1 Answers1

-2

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

Mehdi
  • 329
  • 1
  • 3
  • 14