为窗口属性和表单dll文件创建按钮属性 [英] Create a button properties for windows properties and in the form dll file
问题描述
我创建了一个按钮并将按钮保存在dll文件中,如 mybtn.dll 文件。现在我想在运行时创建一个按钮,但我必须访问mybtn的按钮属性。 dll到新按钮。
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.
推荐答案
假设您要创建一个可重复使用的Windows窗体组件,如自定义按钮:
1.启动类型'类库的新C#桌面项目。
2.为'System.Windows.Forms添加对项目的引用和System.Drawing
3.修改类库代码如下:
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:
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。编译这个来创建'dll。
5.使用这样编译的dll:
a 。开始一个新项目
b。为FancyButton添加对已编译dll的引用
c。在表单上放置一个'using语句,然后创建和使用组件的实例:
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:
// 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。您可以通过以下方式将已编译的'dll挂载到ToolBox中:
a。在ToolBox中创建新标签...或者重新使用您已经创建的标签
b。选择选择项目
c。添加对'dll
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
的提及
伙计感谢您的宝贵答案,但我已经以海关控制的形式解决了自己。我在 中创建了一个按钮海关控制并多次使用它如拖放。
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.
这篇关于为窗口属性和表单dll文件创建按钮属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!