Solved

Adding Controls Dynamically

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

Author Comment

by:Jatin Nahar
ID: 40495909
how can open the form which is another dll to my new project?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

21 Experts available now in Live!

Get 1:1 Help Now