我使用此网站作为在此textBox中创建自定义控件的示例:
在类库项目中,我构建了项目,我有了dll文件。 然后在我的Windows窗体项目中,当我选择dll文件时,我确实选择了项目,我看到它被添加到dll文件列表中,但我没有看到它附近的紫色标志:
这是添加dll文件时Windows窗体项目的屏幕截图。
在加载它之后的dll左边调用:ExtdTextBox但是在它附近就没有这个紫色标志。
然后在form1设计器中,拖动它后会看到textBox。
在底部解决方案资源管理器的右侧,您会看到我得到的错误:
errorProviler1上的错误
这是我添加到我的类库项目的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace CustomControl
{
public class ExtdTextBox : TextBox
{
#region Member Variables
Color waterMarkColor = Color.Gray;
Color forecolor;
Font font;
Font waterMarkFont;
string waterMarkText = "Your Text Here";
#endregion
#region Constructor
public ExtdTextBox()
{
base.Text = this.waterMarkText;
this.forecolor = this.ForeColor;
this.ForeColor = this.waterMarkColor;
this.font = this.Font;
//event handlers
this.TextChanged += new EventHandler(ExtdTextBox_TextChanged);
this.KeyPress += new KeyPressEventHandler(ExtdTextBox_KeyPress);
this.LostFocus += new EventHandler(ExtdTextBox_TextChanged);
}
#endregion
#region Event Handler Methods
void ExtdTextBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.Text))
{
this.ForeColor = this.forecolor;
this.Font = this.font;
}
else
{
this.TextChanged -= new EventHandler(ExtdTextBox_TextChanged);
base.Text = this.waterMarkText;
this.TextChanged += new EventHandler(ExtdTextBox_TextChanged);
this.ForeColor = this.waterMarkColor;
this.Font = this.waterMarkFont;
}
}
void ExtdTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
string str = base.Text.Replace(this.waterMarkText, "");
this.TextChanged -= new EventHandler(ExtdTextBox_TextChanged);
this.Text = str;
this.TextChanged += new EventHandler(ExtdTextBox_TextChanged);
}
#endregion
#region User Defined Properties
/// <summary>
/// Property to set/get Watermark color at design/runtime
/// </summary>
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets Watermark color")]
[DisplayName("WaterMark Color")]
public Color WaterMarkColor
{
get
{
return this.waterMarkColor;
}
set
{
this.waterMarkColor = value;
base.OnTextChanged(new EventArgs());
}
}
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets TextBox text")]
[DisplayName("Text")]
/// <summary>
/// Property to get Text at runtime(hides base Text property)
/// </summary>
public new string Text
{
get
{
//required for validation for Text property
return base.Text.Replace(this.waterMarkText, string.Empty);
}
set
{
base.Text = value;
}
}
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets WaterMark font")]
[DisplayName("WaterMark Font")]
/// <summary>
/// Property to get Text at runtime(hides base Text property)
/// </summary>
public Font WaterMarkFont
{
get
{
//required for validation for Text property
return this.waterMarkFont;
}
set
{
this.waterMarkFont = value;
this.OnTextChanged(new EventArgs());
}
}
/// <summary>
/// Property to set/get Watermark text at design/runtime
/// </summary>
[Browsable(true)]
[Category("Extended Properties")]
[Description("sets Watermark Text")]
[DisplayName("WaterMark Text")]
public string WaterMarkText
{
get
{
return this.waterMarkText;
}
set
{
this.waterMarkText = value;
base.OnTextChanged(new EventArgs());
}
}
#endregion
}
}
类库项目就像我在c#&gt;下创建新项目的网站示例一样窗户&gt; windows窗体控件库
.net 4.5,因为我的Windows窗体项目我想要添加dll也是.net 4.5