Check the image of form and see the below code to get that functionality 这是我完成的一些代码。 我正在开发用户控件库项目,并在其上拖动标签和文本框。
Please check the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CustomControls
{
public partial class CustomTextbox: UserControl
{
public enum Directions
{
Left, Right, Top, Bottom
}
[Description("Define the Text Property of label")]
public string Description {
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
[Description("Define the location of label")]
public Point LabelLocation
{
get
{
return label1.Location;
}
set
{
label1.Location = value;
}
}
[Description("Define the location of Textbox")]
public Point TextboxLocation
{
get
{
return textBox1.Location;
}
set
{
textBox1.Location = value;
}
}
[Description("Set Password Character Input in Textbox")]
public char PasswordChar
{
get
{
return textBox1.PasswordChar;
}
set
{
textBox1.PasswordChar = value;
}
}
[Description("Set the Multiline feature of Textbox")]
public bool MultiLine
{
get
{
return textBox1.Multiline;
}
set
{
textBox1.Multiline = value;
}
}
public CustomTextbox()
{
InitializeComponent();
}
}
}
我声明了一个枚举名称方向,以便我可以根据属性网格中选择的值(左、右、下、上)更改标签控件的位置,并且根据所选值标签应在我使用的项目中对齐控制DLL。 同样,我也想为文本框创建事件,例如文本验证和控件的其他重要事件。
我该怎么做。请建议?
最佳答案
据我了解,您需要来自用户控件的自己的自定义事件。
首先在用户控件中定义委托(delegate)和事件,如下所示。
public delegate void TextChangeDelegate(object obj, string str);
public event TextChangeDelegate TextChanged;
现在,在您的用户控件中,您需要从自定义条件中引发此事件。
if(this.TextChanged != null)
{
this.TextChanged.Invoke(this, textBox1.Text);
}
在您的表单中使用它,如下所示。
userControl.TextChanged += UserControl_TextChanged;
关于c# - 使用网格中的文本框和带有属性的标签设计用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430060/