-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 ?

27
  • 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
    Commented Aug 22, 2015 at 15:59
  • 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 ? Commented Aug 22, 2015 at 16:09
  • 1
    @Mehdi check this link and the first answer for it. Please research Commented Aug 22, 2015 at 16:10
  • 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 Commented Aug 22, 2015 at 16:15
  • 1
    Why can't you just use the designer in Visual Studio and move on? Commented Aug 22, 2015 at 17:13

1 Answer 1

-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.