Solved

Adding Controls Dynamically

Posted on 2014-12-12
11
60 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 32

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 32

Expert Comment

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

Author Comment

by:Jatin Nahar
ID: 40495909
how can open the form which is another dll to my new project?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 32

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now