简体   繁体   中英

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. 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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