C # method in custom control 3

Source: Internet
Author: User

Most of the time, we need to use custom controls. In C #, we mainly create custom controls in the way of 3. Some precautions such as click events are often ignored, the user control click is not like the system button click 1. inherited from. net class library, such as textbox menustrip pannel2. inherited from control class 3. inherited from the usercontrol class. Below are three small examples 1. inherit from the existing controls in the class library to customize an extendtextbox. This control extends an attribute. You can enter only numbers, letters, or all characters in the text box: 1 public class extendtextbox
: System. windows. forms. textbox 2 {3 private allowtype _ allowtype = allowtype. all; 4 Public allowtype 5 {6 get {return _ allowtype;} 7 set {_ allowtype = value;} 8} 9 10 protected override void onkeypress (system. windows. forms. keypresseventargs
E) 11 {12 // set this attribute to true the base method will not process this keypress event 13 E. handled = true; 14 15 if (_ allowtype = allowtype. number) 16 {17 // If the input is 0-9, it is processed by the base method and 18 if (E. keychar> = '0' & E. keychar <= '9') 19 E. handled = false; 20} 21 else if (_ allowtype = allowtype. letter) 22 {23 // If the input is a A-Z or a-z
Input 24 if (E. keychar> = 'A' & E. keychar <= 'Z') 25 E. handled = false; 26} 27 else if (_ allowtype = allowtype. all) 28 {29 E. handled = false; 30} 31 base. onkeypress (E); 32} 33} 34 public Enum allowtype35 {36 Number, 37 Letter, 38 all39} 2. inherited from usercontrol class
This method is mainly used to compile the usercontrol container of the composite control to provide all functions of the control. You can write code to specify which attributes of the new user control can expose its sub-control. usercontrol supports setting the background color to transparent directly, while control does not support public class usercontrol1: usercontrol {} 3. inherited from the control class, the control class provides basic control functions, but does not provide graphical interfaces or any specific functions. You must write code to specify the property method events of the control, you must also override the onpaint and onpaintbackground methods to create an interface.
Class to display information to users. It processes user input through the keyboard and pointer device. It also processes message routing and security. Although it does not implement painting, it defines the boundary of the control (its location and size ). It provides a window handle (hwnd ). The following is a simple example of an elliptical button, but there are several points worth attention. For click events inherited from the base class in a custom control, the events include the usercontrol and control click events. The left-click and right-click events trigger this event. If the double-click event is not defined, the double-click Control triggers the click event twice. The click event here is different from the click event in the. NET class library.
If the traditional button is clicked with the left mouse button, the system responds to the focus and then press Enter. The click event in the custom control does not respond to the keyboard's enter, and the left-click or right-click operation triggers the event. Therefore, we need to perform some processing on it. In this example, we have customized an elliptical button and it will not respond to 1 public class ellipesbutton: System by right-clicking. windows. forms. control Field and override onpaint and onpaintbackground 1 private bool _ mouseentered; 2 protected
Override void onpaint (painteventargs e) 3 {4 // draw the text to the Center 5 sizef stringsize = E. graphics. measurestring (text, font); 6 float startx = (width-stringsize. width)/2; 7 float starty = (height-stringsize. height)/2; 8 E. graphics. drawstring (text, Font, new
Solidbrush (forecolor), startx, starty); 9 base. onpaint (E); 10} 11 protected override void onpaintbackground (painteventargs pevent) 12 {13 // The rendering process is the overall background color of the control-> the background color of the control's valid area-> The control status indicates the area-> the control's outer frame 14 15 //********* * 16 // control overall background color 17 // *********** 18 if (this. parent! = NULL) 19 pevent. Graphics. fillrectangle (New
Solidbrush (parent. backcolor), 0, 0, this. width, this. height); 20 // use the high quality smooth mode to eliminate the 21 pevent. graphics. smoothingmode = smoothingmode. highquality; 22 23 // ************ 24 // control the background color of the effective area of the control 25 // *************** Control. mousebuttons static member 26 if (_ mouseentered & (mousebuttons
& Mousebuttons. left) = mousebuttons. left) 27 {28 color mousedowncolor = color. fromargb (128, backcolor); 29 pevent. graphics. fillellipse (New solidbrush (mousedowncolor), 0, 0, width-1, height-1); 30} 31 else32 pevent. graphics. fillellipse (New solidbrush (backcolor ),
0, 0, width-1, height-1 ); 33 34 // *********** 35 // control status indicates Area 36 // ************** 37 // when the left button is not pressed, the rendering status indicates Area 38 If (mousebuttons & mousebuttons. left )! = Mousebuttons. Left) 39 {40 // draw an orange border with the mouse 41 if (_ mouseentered) 42 {43 pen mouseenterpen = new pen (color. Orange, 2); 44
Pevent. graphics. drawellipse (mouseenterpen, 1, 1, width-3, height-3); 45 mouseenterpen. dispose (); 46} 47 // control gets focus but the mouse does not enter draw blue border 48 else if (focused) 49 {50 pen focusedpen = new pen (color. powderblue, 2); 51 pevent. graphics. drawellipse (focusedpen, 1, 1, width
-3, height-3); 52 focusedpen. dispose (); 53} 54} 55 // if there is a focus, draw the focus box 56 If (focused) 57 {58 pen focuseddotpen = new pen (color. black); 59 focuseddotpen. dashstyle = dashstyle. dot; 60 pevent. graphics. drawellipse (focuseddotpen, 3, 3, width-7, height-7); 61 focuseddotpen. dispose (); 62
} 63 64 // ********* 65 // Control Box 66 // ************ 67 pevent. graphics. drawellipse (pens. black, 0, 0, width-1, height-1); 68 69 // base. onpaintbackground (pevent); 70} non-critical code 1 protected override void onmouseenter (eventargs E) 2 {3 _ mouseentered = true; 4 This. refresh ();
5 base. onmouseenter (E); 6} 7 protected override void onmouseleave (eventargs e) 8 {9 _ mouseentered = false; 10 this. refresh (); 11 base. onmouseleave (E); 12} 13 protected override void ongotfocus (eventargs e) 14 {15 this. refresh (); 16 base. ongotfocus (E); 17} 18 protected
Override void onlostfocus (eventargs e) 19 {20 this. refresh (); 21 base. onlostfocus (E); 22} Note: 1 // here you need to determine whether it is left or right-click to press 2 protected override void onmousedown (mouseeventargs e) 3 {4 If (E. button = mousebuttons. left) 5 {6 this. focus (); 7 This. refresh (); 8} 9
Base. onmousedown (E); 10} 11 protected override void onmouseup (mouseeventargs e) 12 {13 if (E. button = mousebuttons. left) 14 This. refresh (); 15 base. onmouseup (E); 16} 17 // here, the message object is forcibly converted to a mouse message to determine which key is clicked. Only when the left button is clicked is 18 // executed, the click event Delegate is called by onclick. Therefore, the message determines whether to trigger click19.
Protected override void onclick (eventargs e) 20 {21 mouseeventargs M = (mouseeventargs) E; 22 if (M. button = mousebuttons. left) 23 base. onclick (E); 24}

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.