.NET Custom Control Resources.resx - c#

I have a C# custom control that loads images from Resources.resx. I was loading this resources into the Project's Resources and then accessing them like:
ProjectNamespace.Properties.Resources.resourcename;
This works for one project but now I want to use my control in multiple projects.
What's the best way to handle this? Load the resources into the controls .resx? How can I access them from there? Or should I approach this completely differently?

It should work as is, even if your control is used from other projects.
The code generated by VS is a wrapper around the ResourceManager class, and it gives the assembly of your control as a constructor parameter. So, the ResourceManager always knows where to look for resources.

Related

How can I dynamically instantiate an ActiveX control from an .ocx file?

I would like to create an instance of an ActiveX control. Given the .ocx file and interface definitions in C#, how can I do this? I don't mean the usual way of adding a reference to the project because I want to avoid the need for registering the control globally on the target system.
This looks like you should check into Registration-free COM. Beware that this does not work on older systems IIRC.
For some details/samples etc. see:
http://msdn.microsoft.com/en-us/library/fh1h056h.aspx
http://blogs.msdn.com/b/junfeng/archive/2006/04/20/579748.aspx
http://apocryph.org/2010/05/23/getting-reg-free-com-activation-working-between-managed-and-unmanaged-dlls/
http://dotnetforum.net/topic/21006-manifest-resource-refference-in-c-net-application-com-reg-free-instantiation/
http://www.codeproject.com/Articles/13391/Using-IFilter-in-C

How to re-use ASP.NET .aspx or .ascx files?

I know if someone wants to re-use some classes (not UI), he must gather all of them and put in a Visual Studio Class Library, build it to some dells and distribute these dlls. In this approach there just one code, you just update code in one place.
But what about ASP.NET's markups? For example you have an .ascx file or a collection of .aspx files regarding user management. If I want to use them in another project I am forced to copy them in new project again. By this I have two same code that is very hard to maintain.
So is it any way to re-use .ascx and .aspx files just like simple .dlls? For example building them?
Many Thanks,
Afshar Mohebbi
With the default configuration, .ascx and .aspx files will need to exist on disk, because they need to have a path associated with them for everything to work. All the code (everything but the first line which specifies which class to inherit) in them, however, can be compiled away into a DLL file. It would probably be possible to get around this by writing custom handlers and build providers that load things from DLLs, but it's not worth the effort.
If you want to put your user controls into a DLL file, create them as custom controls instead of user controls (.ascx files). That's how all the custom control libraries for sale around the 'net are done.

How to add a control to a resource file in C# for localizing a winform application

I want to add culture to window application so that it can be used globally.
For this I want to use Only one Resource File.
Right now what I do is adds control manually to that file and reads them at page load.
I want this thing to become automated.
How should I proceed
The .Net model is to use a different dll per culture, so if I understand correctly, you're not going to be able to use .NET i18n.

How do I serialize a .NET control to CODE?

I want to create a .NET Form at runtime, add buttons and other controls to that (also at runtime), and then be able to generate a something.designer.cs file from that form (which can then be added to a C# solution and compiled).
What I want to do is very similar to what the WinForm designer does. But instead of having a drag/drop interface for the user, I want to dynamically build the Form/Controls myself at runtime.
I was thinking I could just reuse what the WinForm designer is doing.
Is that possible?
This MSDN magazine article should have everything you need.
It's really not as simple as it was pre-.NET as the visual version of the form you see in Visual Studio is actually the result of multiple files.
But in the simplest form you could simply just mirror what .NET does at the start of creating a new form:
Create three files Form.cs, Form.Designer.cs and Form.resx (which is an XML file).
Place the same default content in them that VS does
Mimic the code generated when adding controls, code-behind and resources
It will be a tedious task, but it can be done. Adding resources however will be burdensome.
Yes, you can do achieve this using Compiler Services (compiling c# code) or Emit class if you know building correct MSIL.

Dynamically loading code-behind along with XAML?

Related to this question I asked earlier, I wonder if it's possible to also dynamically load a code-behind file that is paired with a XAML file. Can it work this way or would it just be better to compile both into a DLL?.
Thanks!
Actually, it's the code-behind that loads the XAML file. The designer generates a hidden file that binds all of your named elements and events to the class. (Notice the 'partial' keyword on your class in the code-behind.) It functions similar to the file that the WinForms designer generates, only it's a bit harder to find. You can find them in the "obj/debug/" folder along with the compiled BAML.
As for actually answering your quetion, it would be better to compile them to a DLL. It may not be impossible to set up a library that can connect a XAML to a special class that has methods to dynamically access elements, but there's nothing like that now as far as I know.

Categories