UserControl custom controls Series 1: Label + TextBox composite controls

Source: Internet
Author: User

From today on, I will share my research on the custom controls of UserControl, which are mainly used to combine multiple controls into a single control to display the distinctive display style of controls, I have not really become a programmer, but I have the same characteristics as a programmer-I am very lazy and write some small programs for my own convenience, however, in order to implement a function, multiple controls are dragged to the form, and the controls must be formatted in an orderly manner (a program with a very messy layout does not work well ), so I thought if I could write a control, drag all the widgets I want into a group and combine them into a control. When I want them, I just need to drag the custom controls and use them directly, you do not have to arrange the layout for half a day every time. Now, go to the topic.

This time, I used the Label and TextBox controls to combine them.

I. Demonstration and principles

1. control effect demonstration

During the design of the form interface, drag and drop the image to the long form. Let's take a look at the effect after running.

The three controls present three different display effects. The first control does not have a field name. I cleared the field name, so it is displayed as an underline, if it is like the second control, it is a text box full of ten characters. The second control adds a text box with a field name. At this time, it is being edited and some content is entered in it, in the third control, I entered the content in it, so that it would display the content with an underscore. These are the functions to be implemented in this UserControl combination control.

2. Implementation Principle

The field Label control is used to display the field name. The value Label control is used to display the value entered by the user. When the Label control is displayed, the entire control is in the browsing status, the TextBox Control is used to enter a value. When the control is displayed, the entire control is in the editing status. As you can see, the control contains an underline when it is in the browsing status, I used the underline to draw it and provided the control with an attribute to change the color of the underline. With these ideas, this control is easy to implement.

The demonstration and principle of the control is here. Next we will start to implement these functions.

Ii. function implementation

1. Control Interface Design

:

Here, I used four controls, two label controls and one textbox Control. However, we can see that there are only two controls, one label control and one textbox Control, the third control is hidden behind the textbox Control. The fourth control is added to the timer control to solve the problem of focus location.

Use label2 for the Label control on the left, label1 for the Label control on the right, textbox1 for the textbox, and the Control name for the Label control on the right. I'm too lazy to give these controls a good name, you can name it like this: the Label control on the left is lblFiled, the Label control on the right is lblValue, And the TextBox Control is txtValue. Such a name is more meaningful, well, let's write it with meaningful names. It is very important to develop a good habit of writing code. Next, we will set the AutoSize attribute of the lblValue control to False to prevent him from automatically adjusting the size, and set the Anchor attribute of some lblValue and txtValue so that they can automatically adapt with the size of UserControl. The settings of the control are now complete, and the following tasks are handed over to the code.

2. Code Implementation

Here, I am hesitant to directly post code, because I am an amateur, so the code is poorly written. Do not look at it. If you can help me to point it out for improvement, thank you very much.

Paste the Code directly. The Code is as follows:

Using System; using System. collections. generic; using System. componentModel; using System. drawing; using System. data; using System. linq; using System. text; using System. windows. forms; namespace detail {public partial class TxtPlusLblLine: UserControl {private Graphics g; public TxtPlusLblLine () {InitializeComponent ();} private void TxtPlusLblLine_Load (object sender, EventArgs e ){ TxtValue. visible = false; lblValue. visible = true; lblValue. focus (); timer1.Enabled = true;} private void lblValue_MouseDown (object sender, MouseEventArgs e) {lblValue. visible = false; txtValue. visible = true; if (UserSystemPasswordChar) {} else {txtValue. text = lblValue. text ;}} private void txtValue_Leave (object sender, EventArgs e) {txtValue. visible = false; lblValue. visible = true; lblVa Lue. Focus (); if (UserSystemPasswordChar & txtValue. Text! = "") {LblValue. text = "Secret";} else {lblValue. text = txtValue. text ;}} public string UText {get {return txtValue. text;} set {lblValue. text = value; txtValue. text = value ;}} public override string Text {get {return txtValue. text;} set {txtValue. text = value ;}} public Boolean ReadOnly {get {return txtValue. readOnly;} set {txtValue. readOnly = value ;}} public Boolean UserSystemPasswordChar {get {return txtValue. useSystemPasswordChar;} set {txtValue. useSystemPasswordChar = value ;}}////// Override the control to obtain the focus attribute ///Public override bool Focused {get {lblValue. Visible = false; txtValue. Visible = true; return txtValue. Focused ;}}////// The SelectedIndexChanged event of the ComboBox control loaded by the custom control /////////Public delegate void TextChangedHandler (object sender, System. eventArgs e); public event TextChangedHandler UTextChanged; private void txtValue_TextChanged (object sender, EventArgs e) {try {UTextChanged (sender, e );} catch {}} public delegate void DoubleClickHandler (object sender, System. eventArgs e); public event DoubleClickHandler UDoubleClick; private void txtValue_DoubleClick (object sender, EventArgs e) {try {UDoubleClick (sender, e) ;}catch {}} private Color color = Color. black; private void TxtPlusLblLine_Paint (object sender, PaintEventArgs e) {g = this. createGraphics (); Pen = new Pen (LineColor); if (lblFiled. text. trim ()! = "") {LblFiled. visible = true; lblValue. location = new Point (lblFiled. location. X + lblFiled. width + 1, 3); lblValue. width = this. width-lblFiled. location. x-lblFiled. width; txtValue. location = new Point (lblFiled. location. X + lblFiled. width, 0); txtValue. width = this. width-lblFiled. location. x-lblFiled. width; g. drawLine (Pen, lblFiled. location. X + lblFiled. width, 20, this. width, 20);} else {lblFiled. visible = false; lblValue. location = new Point (1, 3); lblValue. width = this. width; txtValue. location = new Point (0, 0); txtValue. width = this. width; g. drawLine (Pen, 0, 20, this. width, 20) ;}} public Color LineColor {get {return color;} set {color = value ;}} public string LBLText {get {return lblFiled. text;} set {lblFiled. text = value ;}} public ContentAlignment LBLTextAlign {get {return lblFiled. textAlign;} set {lblFiled. textAlign = value ;}} public bool UEnabled {get {return txtValue. enabled;} set {txtValue. enabled = value ;}} private void timereffectick (object sender, EventArgs e) {if (txtValue. visible) {txtValue. focus ();}}}}

I have not defined all the properties of controls in the Code. If you want to add them, the events are the same. If you want to add them, you can add them as needed.

There are few comments in the Code. Hey, this is still relatively simple, so I will study it if I don't understand it, I can understand it and help me think about it. "If this is the case, it may be better." Let's discuss it with this topic. If you don't want to read it, you can leave something to come in.


The last original blog reprinted please indicate the source Thank you http://blog.csdn.net/jsjyyjs07/article/details/18564099



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.