Custom Controls for winform

Source: Internet
Author: User

Winform controls are divided into user controls and custom controls. User Controls are relatively simple. This article discusses custom controls.

Ms controls, both winform controls and webform controls, have the same design philosophy.

1) For custom controls:

The UI display of the winform control depends on the onprint method, while that of the webform control depends on the renderxxxx method.

2) For custom controls and user controls:

The properties of the winfrom control and webform control play a crucial role in the uidesign. You can set attributes in the design view to set the UI effect. Remember to call the invalidate (); Method to redraw the winfrom attribute set.

3) for the UI design:

Winform is drawn based on GDI +, while webform depends on familiarity with HTML.CodeReturn to the client.

The following is the code for the winfrom custom control:

Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;

NamespaceWinformcontrol
{
Public Partial ClassLandpycontrol: Control
{
PublicLandpycontrol ()
{
Initializecomponent ();
}

PrivateColor _ textcolor;

Public Color textcolor
{
Get { Return _ Textcolor ;}
Set
{
_ Textcolor = Value;
Invalidate ();
}
}

Protected   Override   Void Onpaint (painteventargs PE)
{
Base . Onpaint (PE );
Font enfont =   New Font ( " Times New Roman " , 12 );
Rectangle rect = PE. cliprectangle;
PE. Graphics. drawstring (text, enfont, New Solidbrush (_ textcolor ), 0 , 0 );
}

protected override void onclick (eventargs E)
{< br> base . onclick (E);
_ textcolor = color. red;
invalidate ();
}

Protected Override VoidOnmouseleave (eventargs E)
{
Base. Onmouseleave (E );
_ Textcolor=Color. Black;
Invalidate ();
}
}
}

Textcolor allows you to set the color of the displayed text and define the events of the control.

The simple winfrom custom control is complete.

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.