Solved

Adding Controls Dynamically

Posted on 2014-12-12
11
67 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
11 Comments
 
LVL 34

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 34

Expert Comment

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

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

Author Comment

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

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

624 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