WinForm a custom control in a class textbox in a program, adding a feature that loses focus

Source: Internet
Author: User
Tags border color

Principle:

One, in the background code of the control, add the property of the Boolean type CanFocus

Second, in the control's constructor, register the handling method of the Enter event. And in the processing method, depending on the value of the CanFocus property, determines whether the focus can be lost, and if so, calls the sending class of the Windows message.

Third, in the processing method, call the User32.dll class library, send the window message.

Example code:

How Windows messages are sent

WMMessageHepler.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Runtime.InteropServices;usingSystem.Windows.Forms;namespaceyxt.common{ Public classWmmessagehepler {Private Const intWm_killfocus =0x0008; //Send a window message with lost focus         Public Static voidsendblurmsg (IntPtr hwnd) {PostMessage (hwnd, Wm_killfocus,0,0); } [DllImport ("user32.dll")]        Private Static extern voidPostMessage (IntPtr hWnd,intMsgintWParam,intLParam); [DllImport ("User32.dll", EntryPoint ="SendMessage")]        Private Static extern intSendMessage (IntPtr hWnd,intMSG, IntPtr WParam,stringLParam); }}
View Code

Key code for custom controls

WatermarkTextBox.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingYxt.common;namespaceYxt. client.controls{[ToolboxBitmap (typeof(TextBox))]  Public classWatermarktextbox:textbox { PublicWatermarktextbox () { This. BorderStyle =BorderStyle.FixedSingle;  This. Enter + =NewEventHandler (Watermarktextbox_enter); }        voidWatermarktextbox_enter (Objectsender, EventArgs e) {            if( This. Setblur) { This.            OnBlur (); }        }        PrivateColor _bordercolor =color.red; Private string_watermarktitle; PrivateColor _watermarkcolor =Color.darkgray; Private BOOL_setblur =false; /// <summary>        ///whether the control loses focus/// </summary>         Public BOOLSetblur {Get{return  This. _setblur;} Set            {                 This. _setblur =value;            Invalidate (); }        }        /// <summary>        ///Watermark Font Hint/// </summary>         Public stringWatermarktitle {Get{return_watermarktitle;} Set{_watermarktitle=value;            Invalidate (); }        }        /// <summary>        ///Border Color/// </summary>         PublicColor bordercolor {Get{return_bordercolor;} Set{_bordercolor=value;            Invalidate (); }        }        /// <summary>        ///Watermark Color/// </summary>         PublicColor Watermarkcolor {Get{return_watermarkcolor;} Set{_watermarkcolor=value;            Invalidate (); }        }        Private voidOnBlur () {wmmessagehepler.sendblurmsg ( This.        Handle); }        protected Override voidWndProc (refMessage m) {            Base. WndProc (refm); if(M.msg = =0xf|| M.msg = =0x14|| M.msg = =0x85)            {                if( This. BorderStyle = =borderstyle.fixedsingle) {using(Graphics g = Graphics.fromhwnd ( This. Handle)) {using(varPen =NewPen (_bordercolor)) {G.drawrectangle (pen,0,0, This. Width-1, This. Height-1); Wmpaint (refm); }                    }                }            }        }        Private voidWmpaint (refMessage m) {            using(Graphics graphics = Graphics.fromhwnd (Base. Handle)) {if(Text.length = =0&&!string. IsNullOrEmpty (_watermarktitle)&&!Focused) {textformatflags format=textformatflags.endellipsis|Textformatflags.verticalcenter; if(RightToLeft = =righttoleft.yes) {format|= Textformatflags.righttoleft |Textformatflags.right;                        } textrenderer.drawtext (Graphics, _watermarktitle, Font,Base.                ClientRectangle, _watermarkcolor, format); }            }        }    }}
View Code

Using the example

Method Use Example

Controlsedintor (stringcontrolsname) {            //modifying styles when editing a control            foreach(varValueinch  This. Controls) {varTextBox = value asWatermarktextbox; if(Controlsname = ="Edit")                {                    if(TextBox! =NULL) {Textbox.borderstyle=BorderStyle.FixedSingle; Textbox.readonly=false; Textbox.setblur=false; }                }                Else                {                    if(TextBox! =NULL) {Textbox.borderstyle=BorderStyle.None; Textbox.readonly=true; Textbox.setblur=true; }                }            }        }
View Code

WinForm a custom control in a class textbox in a program, adding a feature that loses focus

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.