c# - 使用网格中的文本框和带有属性的标签设计用户控件

标签 c# winforms properties user-controls custom-controls

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/

相关文章:

c# - 为什么ToArray性能有这样的行为,.Net Core 3.1?

c# - 从字符串转换为字节

java - 如何使用属性作为变量?

c# - 以编程方式单击按钮 C#

c# - 将硬编码数据传输到数组

properties - 如何将属性 getter 作为函数类型传递给另一个函数

使用百分号的Java属性文件 "%"

c# - 为 SpecBind 设置 Selenium Web 驱动程序

c# - 如何更改文本框特定的文本颜色?

.net - Windows 窗体的 Awesomebar-like 行为