C # getting started with custom controls

Source: Internet
Author: User
In the past few days, the evaluation team has been inexplicably nervous about what it meant to be "evaluated". Now, the evaluation team has gone away, but it seems a bit boring.
In order to improve the windows-like canvas, we made a very primitive color lift control. Now we will introduce the general process. You can also download the project file directly.
Platform: VS 2005 winform
Language: C #
Finally:

Looking at the image doesn't seem to be a problem. Let's take a look at it!

To edit a custom control in VS 2005 winform, follow these steps:
1. click File> Create Project> select windows control library
2. edit controls
3. Click Generate> Generate project name. After this step is completed, the "Project name. dll" file is displayed in the bin or DEBUG directory. This is your control library.
4. When you need to use this control Program Click Tools> select tool items> browse> select the. dll file you just selected, so that you can find your control in your toolbox.

The second step is to be written today, and others will master it on their own.
After a project is created, the page shown in the right figure is displayed:
First, we will give the custom control a name: colorhatch;
Next, we will modify the layout of this interface to a similar interface.
Then the code is edited.
Analysis 1: We need a Panel1 to display the selected color. At the same time, we want to directly set its initial color in the program that calls the control;
Now let's implement the requirements in Analysis 1:
The main problem here is how to define an external attribute for this control, so that we can set it in the attribute design view.
The method for defining attributes is to first declare a private variable private color hatchcolor;
Then we compile the set and get methods, that is, the corresponding attribute assignment and value methods.

[Description ( " Set current color " )] // Description displayed in attribute design view
[Defaultvalue ( Typeof (Color ), " Black " )] // Give the Initial Value
Public Color hatchcolor
{
Get   {ReturnHatchcolor ;}
Set
{
Hatchcolor=Value;
Panel1.backcolor=Value;
}
}

In this way, our external attribute is ready. This effect can only be viewed in the form that calls this control.

Analysis 2: when calling the form operation of the control, we hope to have a method to pass the selected color to the called form; we can think of how other controls pass information with the main form (either through properties or events). Here we must pass the color to the main form after selecting a color, therefore, we need to use events for processing.
Because the control itself does not provide this event, we naturally need to define an event.
For details about the "event", you can write a specific piece. here is a brief introduction to how to define events by yourself. You can go to the Internet to check the materials. Remember the reference video of C # language provided by Chen Guang.
The main steps for writing events are as follows:
1. Create a delegate (which is not described here) 2. Declare an event 3. Define a method to trigger an event

Now let's write down this event:

Public   Delegate   Void Colorchangedeventhandler ( Object Sender, colorchangedeventargs E ); // Delegate required for the event

// Event triggered when color changes
Public   Event Colorchangedeventhandler colorchanged; // Define a colorchanged event

Protected   Virtual   Void Oncolorchanged (colorchangedeventargs E)
{ // Event trigger Method
If (Colorchanged ! =   Null )
{//Determines whether the event is empty.
Colorchanged (This, E );//Trigger event
}
}

Colorchanged (this, e) is actually an event processing method called by Delegate. This event processing method is automatically generated when we double-click an event. as if we double-click, a private panelease click (Object sender, eventargs E) is automatically generated ).

However, we cannot find this event on the caller after writing this, because we have completed an event and have not joined our controls. we must add the event trigger method where the control needs to call this event. we need to know that the prerequisite for using this event is that the color in Panel1 is changed, that is, this method is triggered when we select a new color, as follows: Private   Void Panel_click ( Object Sender, eventargs E)
{
Panel P = Sender As Panel;
If (P ! =   Null )
{< br> hatchcolor = P. backcolor;
panel1.backcolor = hatchcolor;
oncolorchanged ( New colorchangedeventargs (hatchcolor); /// events triggered due to color changes
}
}

After this step is added, we can find the Custom Event in the event of the form that calls this control. in this way, we can double-click the event to write the event processing method just like a general event.

Some may ask what colorchangedeventargs is? Are you familiar with this? Is it similar to eventargs? In the click event, we often encounter that. eventargs is a user-passed parameter. Our colorchangedeventargs is a class inherited from this eventargs to pass the selected color to the caller.

Next let's take a look at this colorchangedeventargs class. Public   Class Colorchangedeventargs: eventargs
{
Private Color color;

/**/ /// <Summary>
///Color change event data
/// </Summary>
/// <Param name = "C">Changed color</Param>
Public Colorchangedeventargs (color C)
{
Color=C;
}

/**/ /// <Summary>
///Obtain color
/// </Summary>
Public Color getcolor
{
Get {ReturnColor ;}
}

}

The main problem of such a custom control has been solved. Some friends may be confused, so we recommend that you first understand the events in. net.

Finally, let's take a look at the effect in the form that calls this control:

Entire project file

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.