Link to home
Start Free TrialLog in
Avatar of Jatin Nahar
Jatin Nahar

asked on

Adding Controls Dynamically

Hi Experts,

I am working on the project where i am using the external dll of one application. This dll contains the windows forms. I want to add custom buttons in this form. How can i achieve this?

Please help me to solve this issue.

Thanks in advance.
Avatar of ste5an
ste5an
Flag of Germany image

Take a look at a designer file of a WinForm. Just to the same.
Avatar of Jatin Nahar
Jatin Nahar

ASKER

@ste5an : form is in dll and i want to inject my own custom button in it. please provide the solution in this direction.
Take a look at a designer file of a WinForm. Just to the same.
how can open the form which is another dll to my new project?
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If what you need does not have to be done at runtime, you can to it in the Visual Studio Form Designer by creating an Inherited Form.

First, reference the dll.

Then, create the inherited form with Project...Add New Item...Windows Forms...Inherited Forms and select the form that you want to modify.

You can add controls as you want on the new form, and program them as you usually do.

For the controls that where on the original form, what you can do with them is limited. You will have the same result if you use the technique described by ste5an. Both are dependent on rules defined for inheritance, just as for any other class. This is determined by the Modifier property of each form, that you cannot change on your side, it was fixed by the creator of the dll.

You will be able to do anything you want with those that are marked Public. You will be able work with them only at runtime through your code, to make some invisible as an example, if they are Protected. Won't be able to do anything with them if they are anything else.

By default, C# sets that property to private when you put a control on a form, so most of the time, you can add stuff to a form, but are prevented from working with anything else. In such a case, inheriting from a Form works almost only when the creator designed the form to be used through inheritance, as some kind of model.

Forms created in VB however have their controls Modifier set to Protected by default, so you can usually do a lot with them. Although I am a fan of VB, this is not very good however, because this can lead to dangerous security holes.

Be aware also that if for some reason, in a future versions of the dll, things are moved a bit to add or remove a feature, you might end up with overlapping controls, yours now being in the same spot as controls that were moved or added in the new version of the dll.

That is why this thing usually works well only with forms that were designed so that you can inherit from them.
I've requested that this question be deleted for the following reason:

we will post this question as fresh
The question has been answered.

Why repost it?
excellent