C # Inherits basic controls implementing custom Controls (reprint)

Source: Internet
Author: User

There are three categories of custom controls: 1. Composite Control: A combination of basic controls. Inherit from UserControl 2. extended Control: Inherits basic controls, extending some properties and events. For example, inherit Button 3. custom controls: directly inherit from Control The first situation is easier to get started, but also more commonly used, which also has a lot of skills, slowly summed up. For example, to build a separate class library project, will not cause conflict. How to defer the event code to the user. Get started with extension controls today.

------------------------------------------------------------------

Step one: Here you first build a Windows Control Library project.

Step Two: Create a new user control, modify the code ( Note the comment out .) code in the Designer.cs file )

Extended Button

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 Winformcontrollibrary

{

public partial class Ucbutton:button

{

Public Ucbutton ()

{

InitializeComponent ();

}

Creates the private variable that would store the value of your

Property.

private int varvalue;

Declares the property.

public int ButtonValue

{

Sets the method for retrieving the value of your property.

Get

{

return varvalue;

}

Sets the method for setting the value of your property.

Set

{

Varvalue = value;

}

}

}

}

Namespace Winformcontrollibrary

{

Partial class Ucbutton

{

<summary>

Required designer variable.

</summary>

Private System.ComponentModel.IContainer components = null;

<summary>

Clean up any resources being used.

</summary>

<param name= "disposing" >true if managed resources should be disposed; Otherwise, false.</param>

protected override void Dispose (bool disposing)

{

if (disposing && (components = null))

{

Components. Dispose ();

}

Base. Dispose (disposing);

}

#region Component Designer generated code

<summary>

Required method for Designer support-do not modify

The contents of this method with the Code editor.

</summary>

private void InitializeComponent ()

{

components = new System.ComponentModel.Container ();

to comment out this sentence

This. AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

}

#endregion

}

}

Extended Label

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 Winformcontrollibrary

{

public partial class Uclabel:label

{

Public Uclabel ()

{

InitializeComponent ();

}

protected override void Onmouseenter (EventArgs e)

{

Base. Onmouseenter (e);

This. Font = new Font (" Arial ", 10F, fontstyle.underline);

}

protected override void OnMouseLeave (System.EventArgs e)

{

Base. OnMouseLeave (e);

This. Font = new Font (" Arial ", 10F, fontstyle.regular);

}

}

}

Step three: Add a project reference in another Windows forms project. After compiling, you see the generated custom control in the Toolbox. url:http://greatverve.cnblogs.com/archive/2012/02/16/user-control-inherit.html Reference MSDN:

http://msdn.microsoft.com/zh-cn/library/5h0k2e6x (v=vs.80). aspx

http://blog.csdn.net/yysyangyangyangshan/article/details/7078471

C # Inherits basic controls implementing custom Controls (reprint)

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.