Solved

Adding Controls Dynamically

Posted on 2014-12-12
11
63 Views
Last Modified: 2015-01-07
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.
0
Comment
Question by:Jatin Nahar
  • 4
  • 3
  • 2
11 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40495863
Take a look at a designer file of a WinForm. Just to the same.
0
 

Author Comment

by:Jatin Nahar
ID: 40495869
@ste5an : form is in dll and i want to inject my own custom button in it. please provide the solution in this direction.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40495900
Take a look at a designer file of a WinForm. Just to the same.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Jatin Nahar
ID: 40495909
how can open the form which is another dll to my new project?
0
 
LVL 33

Accepted Solution

by:
ste5an earned 500 total points
ID: 40495917
Not that form, you should do only the same what VS does. E.g. from a freshly create form with a button:

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(63, 64);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(66, 52);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

Open in new window


So you can do the same:

        private System.Windows.Forms.Button button1;

        private void InitializeComponentOnDllForm(System.Windows.Forms.Form dllForm)
        {
            this.button1 = new System.Windows.Forms.Button();
            dllForm.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(63, 64);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(66, 52);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            dllForm.Controls.Add(this.button1);
            dllForm.ResumeLayout(false);
        }

Open in new window

0
 
LVL 40
ID: 40498306
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.
0
 

Author Comment

by:Jatin Nahar
ID: 40518359
I've requested that this question be deleted for the following reason:

we will post this question as fresh
0
 
LVL 40
ID: 40518360
The question has been answered.

Why repost it?
0
 

Author Closing Comment

by:Jatin Nahar
ID: 40537233
excellent
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question