简体   繁体   English

使用文本框设计UserControl并使用网格中的属性设计标签

[英]Design UserControl with Textbox and Label with Properties in grid

Check the image of form and see the below code to get that functionality Here is some code which i had done. 检查表单的图像并查看以下代码以获取该功能。这是我已经完成的一些代码。 I am working on user control library project and i drag a label and textbox on it. 我正在研究用户控件库项目,并在其上拖动标签和文本框。

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();
        }
    }
}

I had declare an enum name direction so that i can change the position of label control as per value selected in Property Grid (left, right, down, up) and as per selected value label should align in project where i used the control dll. 我已经声明了一个枚举名称方向,以便可以根据在属性网格(左,右,向下,上)中选择的值更改标签控件的位置,并且根据选择的值标签应与我使用控件dll的项目对齐。 Similarly i also want to create events for textbox like text validating and other important events of the controls. 同样,我也想为文本框创建事件,例如文本验证和控件的其他重要事件。

How can i do this. 我怎样才能做到这一点。 Please suggest? 请提出建议?

As par my understanding you need your own custom event from user control. 根据我的理解,您需要来自用户控件的自己的自定义事件。

First define delegates and events in your user control as follow. 首先,在用户控件中定义委托和事件,如下所示。

public delegate void TextChangeDelegate(object obj, string str);
public event TextChangeDelegate TextChanged;

Now in your user control you need to rise this event from your custom condition. 现在,在用户控件中,您需要从自定义条件引发此事件。

if(this.TextChanged != null)
{
    this.TextChanged.Invoke(this, textBox1.Text);
}

Use this in you form where you use this as follow. 在表单中使用它,如下所述。

userControl.TextChanged += UserControl_TextChanged;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM