Transforming a c# class into a visual control - c#

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.

Related

Winform Composite Control Project?

Is it possible to have a single project which allow to include a custom custom control within another custom control ?
Update: of course I'm not asking about how to put a custom control on a winform!!! But if I can create put a CUSTOM user CONTROL inside ANOTHER CUSTOM user CONTROL within the SAME PROJECT.
Sure, you're able to do this. In fact you're able to put a custom control inside another custom control the same way you'd do it on a form (it's essentially the same while editing). You might as well base your custom control off another class (or custom control), but not all will support the built in gui editor (so you might just see an error message but the code will still work).

C# UserControl factory

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?

Make an Existing Windows Form Inheritable

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).

Using C# WinForms Designer on Panel instead of Form?

Warning: I'm new to GUI building in C# (come from a long history in Java with Swing and SWT.)
Using VS2008, When I create a new class, and extend System.Windows.Forms.Form, the WinForms Designer works really nicely. However; when I create a new class and extend System.Windows.Forms.Panel, the designer does not work nearly as elegantly - it simply shows some kind of abstract, list-type view of the components contained in the Panel (as opposed to showing the actual layout of the Controls in the Panel.)
Is there a way to get the Designer to work more nicely with a Panel?
Or is there some workaround so that I can build a Form with the designer, and then use it as if it were only a Panel?
What I'm really looking for is some UI 'element' I can build with the designer, and then dynamically (read: programmatically) add/remove that 'element' to a larger UI.
I think what you're looking for is a UserControl. You can inherit directly from that, and you should be able to use the designer to drag and drop stuff on it.
To make it even easier, you can just right click on your project and click Add -> User Control. That will create the .cs file for you as well as a partial .cs file for the designer generated code.

Dynamically add an ActiveX control without the VS designer

I'm trying to dynamically add an ActiveX control to my application, without having to place it on a form.
However, when I do place the ActiveX control using the Visual Studio designer, initialization code is generation which contains a statement for setting for OcxState of the ActiveX control.
When an ActiveX control is added to a form using the designer, it seems like that the OcxState is serialized into a resources file, which is later retrieved at the initialization of the form.
Now I want to set that OcxState dynamically in code, so I can create some kind of wrapper class for the ActiveX for in my class library.
Anyone knows how to dynamically set the OcxState of an ActiveX control?
Thanks in advance!
Mathieu
i working in the same problem, the solution i got by now is working with a Worker form, that is not show, but where i can load the ocx and wrapper all the events and methods of the ocx to the outside.
In my library class i use the form like any other class instead of the ocx calls
I had the same problem while I was working on a .NET project that requires an ActiveX control for data manipulation. I had to put the ActiveX control on a form, load it with the application and use it with a public property. Otherwise if I just create an instance of the control I loose some of the functionality.
The form does not require to be visible all the time but it provides a container where the ActiveX control can live.
The another way is UserControl,
You can create a user control with your ActiveX Control. Now you can load the user control at wherever you want dynamically at runtime.

Categories