I didn't realize at the time I create this particular application that I'd need to reuse some of the components - some Windows forms and a class or two.
Now that I've already created the fairly complex forms inside one project, what's the easiest way to transform those forms into inheritable forms that I can reuse in other projects? Once that's done I'd like to modify the existing project to use the newly created inheritable forms.
How can I accomplish this with as little pain as possible? I'm using C# in Visual Studio 2008.
You don't really have to do anything special to achieve this. Your form is already inheritable. On any new form, just make sure the first line looks like this:
public partial class frmMyChild : frmMyInheritableForm
instead of:
public partial class frmMyChild : Form
and make any methods that you need to access from the child either "public" or "protected".
Update: one additional trick is to set the Modifiers property of each control on your original form to Protected (instead of the default Private). In the designer for your child form that inherits from this form, you will then see all of the controls on the parent form, and you can move them and resize them as you see fit (this will not affect the original form's layout).
To access any parent method from the child form, you just call:
base.MyMethod();
Just declare an empty class that inherits from System.Windows.Forms.Form and then make your "huge" class inherit from that. Once that works, start moving, a small reusable piece at a time from your "huge" class to the parent class.
In your first project, add a new "Windows Forms Control Library" to your solution
Drag the windows/classes from the original project to the new one.
fix the errors.
At this point, you now have a Class Library which you can include in your second windows project.
Bear in mind that you don't need to design your forms to be inheritable in order to use them in other projects. Inheritable forms are a PITA, and in all but the simplest circumstances are more trouble than they're worth.
If you're simply looking to design your forms to be more portable, then the biggest thing that would be required is ensuring that you do NOT expose internal fields (Controls are included in that) outside of the form. If outside code (be it in the same or another project) needs to interact with the form in some visual or behavioral way, then you need to expose functions and properties that represent that functionality, rather than the control itself.
Apart from the design of the particular form, it would likely be helpful (if a somewhat time-consuming exercise) to move these common forms into a separate control library. While you can definitely add your .exe as a reference to another project, that's not ideal (and not entirely intuitive).
Related
I'm just curious if there is an easy way to set a WinForm property like Autosize (for example) to true for every form in a project without having to manually change every single one of them. Thanks!
Create a Form called BaseForm and let all other forms inherit from it. On the BaseForm you can set the desired font, scaling etc. and all other forms will inherit these properties. This is the first thing I do in every Windows Forms project I fire up.
You may also consider creating a BaseDialog, inheriting from BaseForm. This BaseDialog can have hidden minimize/maximize buttons etc. that you would like to be common for all modal dialogs in your application.
I'm unsure from your question whether this will meet your needs but the Application.OpenForms property allows you to enumerate all open Form instances: https://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms(v=vs.90).aspx
I'm using a third party Windows Forms library that has some Form theming options such as making the form windows look like Office Forms (class Office2007Form) or like Metro Forms (class MetroForm). The library is made by SyncFusion.
https://www.syncfusion.com/products/windows-forms/office2007form
https://www.syncfusion.com/products/windows-forms/metroform
Unfortunately there isn't a single form class where I can set the theme via a property (something like: SyncFusionForm.Theme = Metro). Instead the classes are separate and as such if I want to support both themes, I have to create duplicate forms for each theme with the same controls and nearly identical code.
Is there some way I can dynamically set something like this up perhaps by simulating multiple inheritance with interfaces? I don't have the source for the form classes so my options are not too flexible. To create a SyncFusion themed form, I currently just create a Windows Form and change the base class from 'Form' to either 'Office2007Form' or 'MetroForm' and then there are a few properties I set for color etc...
Please let me know if anyone has some idea of how I can do this without replicating my controls and code. Thanks in Advance!
My first instinct would be to have everything on a UserControl. You then add the user control to either the Office2007Form or MetroForm instance as required.
I follow the Design pattern principle of 'favour composition over inheritence'
I've created a win form application which consist of a single form. We have 8 tabs to access the modules of application.
The problem is we are a team of 4 who works on this project. But since it is a single form application, only one person can use the file at a time. Is there anyother way to build application with more than one file?
Please provide some solution.
Firstly, you should probably have a separate UserControl per tab. That will give you 8 files (at least) since you have 8 tabs.
Secondly, you should be using a Model-View-Controller style architecture for Windows Forms applications. That will give you at least one controller, but likely you will have one controller per UserControl (i.e. per tab). You might even have an overall controller that manages the per-tab controllers.
You might only have one data model for the entire app, or you might have one data model per UserControl (tab).
If you did all that, you'd have a few more source files.
However, it's actually difficult to say without knowing anything about your app.
Try using user controls to make each tab modular.
Figure out what are the parameters that each tab accepts and that it exposes and then create user controls that have that behavior.
Here are couple resources to get you started
http://msdn.microsoft.com/en-us/library/aa302342.aspx
User Control vs. Windows Form
User Controls in Windows Forms - Anything similar to ASP.NET User Controls?
Even if this is a giant ball of wax, your source control tools are shoddy and breaking it up into separate classes is hard to do, you can still take advantage of a Form class being a partial class. Which means that you can spread the code over any number of source code files, not just the two files that the designer creates. So a logical organization is to move code that belongs to a particular tab in its own partial class with the same form class name and its own source code file. Some cut+paste required however when you add event handlers with the designer.
Have you considered using MDI?
MSDN Working with MDI...
Examples are in VB.Net but I'm sure it will be easy to use C# if you really want to - I'm not sure why, but... :)
I new to c# and windows form programming. Here is the problem i am trying to solve.
I have to write an application that uses an multiple instance of an ActiveX control. Therefore, I dragged as many control as necessary to my Form. Now my problem is that i'd like to add some personalised methods to this activeX. The logical solution I thought was to create my own class derived from that aciveX and add some more members and methods to that class so it would work as desired. Now my problem is that the newly generated class doesn't exists as a control that can be inserted into the form.
So How can a class become a control and then inserted into a form ?
If you look at the Designer.CS file that corresponds to your form, you should see, in the #region Windows Form Designer generated code, the code that was generated when you dragged the ActiveX control onto the form.
This code is similar to what your code needs to look like.
My guess would be that you should wrap this activex into your own control, and add funcionality that lacks into that wrapper.
Pros: you'l have .net control and will be able to extend it and use it anyway you like.
Cons: if you want to access original methods, you'll have to generate pass-through method wrappers.
How:
- create a control class
- drop activex you have onto it
- set activex dock to 'fill'
- either set control to public, create get wrapper for it, or create method wrappers you desire
- compile that and use it on the form.
Let's say you have two classes that extend UserControl. Each of the controls provides a custom event (this could be done by using an interface).
You want to display one of the controls in the odd days and the other in the even days.
You also want to be able to drag&drop (Visual Studio) the UserControl on your form without knowing what the Control type will finally be.
How do you do that ? Is the factory pattern useful here ?
I would make a container control that is added on the form (and that is present in the designer toolbox), that internally uses some factory to create an instance of the actual control to use and then adds it to the container with Dock set to Fill.
You could make a third usercontrol that creates & hosts the usercontrol depending on the day.
But, this has a bad feeling to it, could you explain more in detail what you actually are trying to do?