So I am working on a common framework for my agency, and one of the things we are trying to do is store all resources in a Resources project, for example the icons and button images. That way if we make a change, for example, to the 'Copy' button image, the changes will be applied across all projects next time they are built. I made the Resources.resx file use public constructors, andI've referenced this project in my WinForms project which contains the base class for our main form. This allowed me to access the resources via code from other projects. However, when I went to the designer to change the icon or try to change the images of buttons to those contained in the Resource project, they were not available. The only way I was able to do it was by modifying the designer.cs manually to add the following line:
this.Icon = Resources.Properties.Resources.AGO;Same for the menu buttons:
this.copyToolStripMenuItem.Image = Resources.Properties.Resources.CopySmall;This worked fantastically. I saved it, closed it down, and opened the form in designer. I saw the icon and button images from the Resource project just as I wanted. Now here's the annoying bit. As soon as I make any change to the form in designer, it generates a .resx file for the main form and pulls copies of the files from the Resource project into that .resx in base64. Consequently, if someone goes and makes a change to the image in the Resource project, the changes will not propogate as desired. Anyone know a better way to use resources from an external project in the designer without making local copies of the resources? | | AlexH_AGO Thursday, February 19, 2009 5:48 PM |
Hi AlexH_AGO,
From your last post, I quite understand your situation. Yes, you are right, put the code in the constructor cannot be shown in the designer. The solution I have provided is not suitable for this situation. To be honest, I have never met "cross project resource problem" before. Due to limitation of our product. I have no better solution for it.
Just a thought in my mind, you can write two copy of these code, one in the designer.cs and another in the constructor. So you don't need to debug and see the result. Once the code is covered by the designer, you can also run your application properly with the code in the constructor. That seem not professional enough, but it may be better than run your application to see the test result.
If you have further problem, please feel free to come to MSDN forum.
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byKira QianMSFT, ModeratorThursday, February 26, 2009 10:02 AM
-
| | Kira Qian Wednesday, February 25, 2009 2:30 AM |
Hi AlexH_AGO,
Firstly, it is not recommend to modify the Designer.cs file directly, because the file is controled by the designer. If you want to modify the property of your form and controls on it, you should write the code in the constructor.
I don't know why you need to create a separate resource project instead of add resource to your current project. If you want to use your "Copy" button in many form, you'd better create an inherited Button, so when you change the icon of that button, all "copy" button can be changed to that icon.
If you need any further help, please feel free to tell me.
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. | | Kira Qian Monday, February 23, 2009 7:57 AM | Thanks for the reply Kira! I understand Microsoft does not recommend modifying the Designer.cs directly but it was the only way I could figure out to get it to use a resource from an external project. I could just as easily assign it in the constructor, but then it doesn't show up in the designer and that bothers me. What's the point in a designer view if it doesn't accurately represent the UI that will be shown at runtime? If I have an image assigned to a button, I want to see it in the designer!
I understand your recommendation of making a set of buttons which I inherit from, but this is not always feasible. For example, we'd like to put Cut/Copy/Paste buttons on the 'Edit' menu of each application, and so I included that in the base class for our main form. However, we may develop an application where it makes sense to place the Copy button on the toolbar or somewhere else too, and I don't want to duplicate the resource in multiple places. I'd have to create a Copy button of every type (menu button, toolstrip button, form button, etc.) in the same project as our resources are kept in order to truly avoid duplication of resources. This seems like an illogical constraint, as anything that can be included in an assembly should be re-usable without duplication,and so that's why I came here to find an answer. It seems this angle may have eluded the development team.
(And yes, I've since come to realize the impossibility of having a menu on our base form which is another topic entirely... turns outyou can't modify the menu in a child class if you do that,so I've since removed the menu from our base class altogether, but I believe the point is still valid.)
P.S. Re-reading the post, it's mostly rhetorical. I suppose I have my answer: Either do it at runtime or live with duplicated resources. If anyone has a third option, please let me know! | | AlexH_AGO Tuesday, February 24, 2009 11:10 PM |
Hi AlexH_AGO,
From your last post, I quite understand your situation. Yes, you are right, put the code in the constructor cannot be shown in the designer. The solution I have provided is not suitable for this situation. To be honest, I have never met "cross project resource problem" before. Due to limitation of our product. I have no better solution for it.
Just a thought in my mind, you can write two copy of these code, one in the designer.cs and another in the constructor. So you don't need to debug and see the result. Once the code is covered by the designer, you can also run your application properly with the code in the constructor. That seem not professional enough, but it may be better than run your application to see the test result.
If you have further problem, please feel free to come to MSDN forum.
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byKira QianMSFT, ModeratorThursday, February 26, 2009 10:02 AM
-
| | Kira Qian Wednesday, February 25, 2009 2:30 AM |
|