User control and application in one project

I want to create a custom control in C # that I will use several times in my application, but I’m unlikely to use it in other applications. I have seen several websites that explain how to create a custom control and add it to the Visual Studio toolbar. They instruct you to create a project with a custom control that compiles into a DLL, then create a new project for the application and add a link to that DLL in the toolbar. However, I would prefer my application to be the only executable that does not depend on the DLL. Is there a way to place the user control and application in the same project and still have a custom control in the Visual Studio toolbar? Or,if it is not possible to add the control to the toolbar, is there another way to view the control in the application designer?

+3
source share
1 answer

The method is in:

  • To develop your project, you can use your component as usual (this means adding it as a DLL to the toolbar and referring to it in your project).

  • To use your DLL internally in a single exe file, you must add it (source or deflated) to your project resources.

  • In the main method, subscribe to the AppDomain.AssemblyResolve event:

    public static void Main( string[] args )
    {            
        AppDomain.CurrentDomain.AssemblyResolve += AppDomain_AssemblyResolve;    
    }
    
  • In the event handler, you must enable the assembly with your component:

    private static Assembly AppDomain_AssemblyResolve( object sender, ResolveEventArgs args )
        {
            if( args.Name.Contains( "YourComponent" ) )
            {           
                using( var resource = new MemoryStream( Resources.YourComponent ) )
                    return Assembly.Load( resource.GetBuffer() );
            }
    
            return null;
        }
    

Then your application will use this assembly extracted from your exe, but you can still use the component from the toolbar during development.

, , .

+2

Source: https://habr.com/ru/post/1757034/


All Articles