Click here to Skip to main content
16,019,843 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a button and save the button in the dll file like mybtn.dll file.Now I would like create a button in run time but i have to access the button properties of mybtn.dll to new button.
Posted
Comments
ridoy 29-Dec-14 7:26am    
You can have a look at: http://stackoverflow.com/questions/6392372/access-a-method-from-a-dll-from-c-sharp-program

Assuming you want to create a re-usable Windows Forms Component, like a custom Button:

1. start a new C# Desktop Project of Type 'Class Library.

2. add references to the Project for 'System.Windows.Forms and System.Drawing

3. modify the Class Library code to look like this:

C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Dec29_FancyButton.cs
{
    public class FancyButton: Button
    {
        private static Size defaultSize = new Size(100,30);
        private static Color defaultBackColor = Color.White;
        private static Color defaultForeColor = Color.Black;
        private const string defaultText = "FancyButton";

        // use default values if the "vanilla" ctor is called ...
        public FancyButton()
        {
            Size = defaultSize;
            BackColor = defaultBackColor;
            ForeColor = defaultForeColor;
            Text = defaultText;
        }

        // alternative 'ctor for specifying initial visual state
        // note that since 'Size.Empty and 'Color.Empty are not compile-time constants
        // they cannot be used as optional parameter values in a 'ctor
        public FancyButton(string text, Size bSize, Color backColor, Color foreColor)
        {
            this.Text = (text == String.Empty) ? defaultText : text;

            this.Size = (bSize == Size.Empty) ? defaultSize : bSize;

            this.BackColor = (backColor == Color.Empty) ? defaultBackColor:  backColor;
            this.ForeColor = (foreColor == Color.Empty) ? defaultForeColor:  foreColor;
        }
    }
}
4. compile this to create the 'dll.

5. use the compiled dll like this:

a. start a new Project

b. add a reference to the compiled dll for FancyButton

c. put a 'using statement on the Form where you want to use the Component, then create and use instances of the component:
C#
// required
using Dec29_FancyButton.cs;

private void Form1_Load(object sender, EventArgs e)
{
    FancyButton fb1 = new FancyButton();
    this.Controls.Add(fb1);
    fb1.Text = "number one";
    fb1.Location = new Point(100, 100);

    FancyButton fb2 = new FancyButton("number two", 
        Size.Empty, Color.Empty, Color.Empty);   
    this.Controls.Add(fb2);
    fb2.Location = new Point(200,200);
}
6. You can mount the compiled 'dll in your ToolBox by:

a. create new Tab in the ToolBox ... or re-use one you've already created

b. select 'Choose Items'

c. add a reference to the 'dll
 
Share this answer
 
Comments
Divakard3 29-Dec-14 10:04am    
Thank U
Guys thanks for your valuable answer but i have solved myself in the form of Customs Control.I have a created a button in the customs control and used it many times like drag and drop.
 
Share this answer
 
Comments
BillWoodruff 30-Dec-14 10:21am    
Glad to hear you have found a solution. Please post comments like this as comments on the responses of people you are writing to, not as a "solution." cheers, Bill

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900