Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access resources from a different project using the designer in WinForms?

Tags:

I've got a rather large solution with many projects in it. Ideally I'd like to create a Resources project that contains all the embedded icons I need throughout my application.

I've read articles where people are referencing them through code, but is it possible to get access to this project's resources in the designer? In other words, I'd like to select the resources when clicking on the BackgroundImage property of a form in the designer.

If not, what sort of architecture should I consider when trying to decouple my resources from my main project? It's probably not a smart idea to release a 100mb executable everytime I modify a line of code or two.

like image 970
Daniel Minnaar Avatar asked Feb 15 '13 22:02

Daniel Minnaar


People also ask

Is WinForms front end?

Hence, WinForms is clearly a front-end thing. Show activity on this post. Front-end technology can essentially apply to any technology that is primarily used to build applications' user experiences. This can include WinForms, Win32's windowing API's, MFC, Silverlight, Javascript, HTML, CSS.


1 Answers

I've found a working way to share a resources file between many projects and keep VS Designer compatibility (works with VS 2012), it is no complicated :

  1. Create a new resources file in your source project (named in my example SharedResources.resx)

  2. Open SharedResources.resx in Designer and change Access Modifier to Public

  3. Open the .csproj of your new project file with notepad and add following code into the <ItemGroup> which contains the <compile Include="MyForm.cs"> : (will include *.resx and *.Designer.cs as link without compilation and with special namespace)

    <EmbeddedResource Include="MY_SOURCE_PROJECT_FOLDER\Properties\SharedResources.resx">
      <Generator>PublicResXFileCodeGenerator</Generator>
      <Link>SharedResources.resx</Link>
      <LastGenOutput>SharedResources.Designer.cs</LastGenOutput>
      <SubType>Designer</SubType>
      <CustomToolNamespace>MY_SOURCE_PROJECT_NAMESPACE.Properties</CustomToolNamespace>
    </EmbeddedResource>
    <None Include="MY_SOURCE_PROJECT_FOLDER\Properties\SharedResources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>SharedResources.resx</DependentUpon>
      <DesignTime>True</DesignTime>
    </None>
    
  4. Replace MY_SOURCE_PROJECT_FOLDER with the relative path of your project containing the resources and replace MY_SOURCE_PROJECT_NAMESPACE with namespace specified in the generated file SharedResources.Designer.cs (should be your global project's namespace)

  5. Enjoy !

WARNING :

Sometimes VS makes an other automatic change in the **.Designer.cs* shared file which will make a failure, please ensure property ResourceManager still points to the root shared file. enter image description here

IMPORTANT :

  • When you add new resources in your resx file, open it from the source project or file will be added in wrong folder !
  • When you just added a new resource, if it does not appear in the new project, simply restart VS, it should work
like image 137
6 revs, 2 users 99% Avatar answered Sep 24 '22 11:09

6 revs, 2 users 99%