Refactor C# Application - c#

I found this post and I would like to do the second option in the first answer
2) If the app is only useful within the context of the parent apps,
say by being given objects or other arguments from the other apps,
consider refactoring the small app to allow the form's project to be
referenced by the parent apps. Then, your parent apps can simply
create a new instance of the form and display it:
var childForm = new SmallAppForm();
childForm.Owner = this; //the child window will close when its owner does.
childForm.Show();
I have some questions about what steps I would have to do in order to accomplish it.
The app that I trying to call can be found here.
To change it so that I have a main (parent) form that creates and calls this application like a class, would I add a new project that would be the "parent", add the Main to my new parent form so that it is the application. Then have a menu choice to start the wizard in the parent form in order to create and call my wizard like this?
CreateUserContext context = new CreateUserContext();
CreateUserWizard wizard = new CreateUserWizard(context);
Thanks for any guidance,
Leslie

Add a class library and put the classes of your SmallFormApp in it. This class library will be under the same solution. Then go ahead and reference this in your ParentForm and use the functions in it.

What I would do is to tranform the small aplication into a Class Library instead of a Winforms application. (It can also be a project within the solution, referenced by the main project)
Then, the parent application just references the small application and calls the new SmallAppForm()
Of course the SmallAppForm must be public.
Class Library is the project type. When you create a project, it can be console application, windows form application, class library and many others. So you should create a new project as class library type.
Copy all your forms of the small applications to this project, make the classes public.
(There's no program class in a class library, just the forms and other classes you created)

Related

Insert a user interface in a DLL

In my project I have implemented this plugin manager:
https://code.msdn.microsoft.com/windowsdesktop/Creating-a-simple-plugin-b6174b62
In this way I can add .DLL file and make my project more modular.
I wanna know if in one of these .DLL plugin I can add a .xaml with user interface, and use it inside my main project to visualize the content of that xaml in my main GUI.
In this way I can make my app more modular not only by code library but also with user interface.
Thanks
If you create a project that contains WPF UserControl items, then as long as you expose those items through the DLL interface then you can utilise them in another project.
You should be able to verify this very easily by doing something like the following:
1) Within your 'DLL' project make a public class SquareControl, which is simply a UserControl under the hood, and specifically a canvas containing a red square of a fixed size.
2) Within your utilising project, reference the DLL.
3) Within your utilising project, in C# code somewhere create an instance of SquareControl, and check in the debugger that its properties are as you expect.
4) Then create a UserControl within your utilising project, and open VS Designer for that control. Within the empty Grid that has been created for you drop an instance of SquareControl, and you should be able to see this within Designer. Getting your xaml namespace definitions can be awkward the first time around but there's plenty of help available for that. Then fire up the application and see it there.

How to open a new form from another project

In my soloution I have created some projects and one of them is the main project so it gets fired up when application starts.
I want to use only this main project's Properties file to store all Properties (string, objects and etc..) so for this case I have to reference this main form project in other form projects (Hope I am making sense!)
Now the problem is when I want to call one of those forms inside my main form projects, because I can not reference to them (I get this circular dependency warning), I am unable to do so! What can be the workearound in this case?
You can only link in one direction. You must think of a project as a upside down tree. You can reference a parent class, but the parent class can't reference the children class. So if you need a feature that is needed in both, move it to the parent of the two classes, or move it to a 3rd class, and both reference it.
you pass main form instance as constructor argument when you call one of those forms in the main. In that way you access properties of main project. main form has to be precedent to others. the other way wouldnt work unless you put all properties as independent class or construct

C++ Parenting a WPF window from another assembly and handling events

I've been unable to make this work because of what I believe is a glitch in Visual Studio, so I'd really appreciate if someone could attempt this situation and share what happens.
I have setup in a solution 2 projects:
- a C++ application which has been CLI enabled (.exe)
- a C#/WPF class library which has a .xaml form inside with a matching .cs window class (.dll)
I want to spawn the WPF window inside my C++ application, so I import its reference and create a new instance of the window and run under a new application context. Thats works fine.
I now want to make classes out of this window and handle different events inherited from protected functions in the C# window, so In the C++ assembly make a public ref class whom child is the .cs class of the .xaml powered window. This compiles fine.
ie:
public ref class myCPPWindow : myWPFWindow { ... };
I then change the window I spawn to the parent class which is located in the C++ assembly rather than the base class located in the C# assembly. Now I get an error on the InitializeComponent() part of the base C# class while loading the .xaml window that I require saying that it fails to load the .xaml window source from the C# assembly even though the base class works. Can anyone give an explanation/fix for this?
It looks like a common [library;user control]-[application;derived control] issue in WPF - I reproduced that even without C++. Without digging into explanation, general workaround is either aggregating "base" class or re-degisning base class to be templated control instead of user control (e.g. without .xaml file). If I understand correctly, your question is the same as The component does not have a resource identified by the uri question.

C#,WPF integrated user control

Got a winform in project A (Main project) and which hosts a wpf user control and is maintained under different project called B
On click of a button on user control ,I wanted to query server which is in project "C" and retrieve data and show it on hosted wpf user control. I know its a bad coding practice to put a reference to s project "C" in project B which includes only custom controls .Any alternative thots would be appreciated...
To minimize project dependencies, create an interface in your control project (B) that reflects the operations your control requires.
Implement a class in your client (A) project that implements this interface (which will obtain the information you need from your data project (C) and pass it back through the interface method).
Pass the service class (implementing the interface) constructed in A to your control B when you initialize the control.
You make another project that is just a class library that can be shared across all three projects. You'll put the interfaces and/or the class implementations that need to be shared between the projects in the class library and then just use those in the server and the user-control projects.

Creating Multiple Forms By Combining Multiple Projects

Assume that I have a project that contains two buttons on it. Each buttons generate a custom form. I wrote both forms manually. But I wonder, is there a way that I can make these forms by using toolbox in different projects and combine them in one solution. I mean, when I press a button I want to call another project.
And finally if such a solution exists, is it proper way to make programs like this , or is it better to create forms manually ?
You can call a form from another project (if we are talking about VS project) if it is public. You need to add reference to the project where this form is "placed" to the project where the call is made.
Of course, you can create forms manually, but you can as well create new windows form and add controls to it via designer.
And about the proper way. If you need the same form to be called from different projects, then, yes, keeping in in some third project is alright.
Yes you can invoke the forms even if they are in different projects. But you need to have references to that projects and to have defined a proper using statements.

Categories

Resources