Use Visual C # For winform Components

Source: Internet
Author: User
.. NET development platform provides a namespace system. windows. forms provides many classes and objects for developing Windows form in this namespace. These classes and objects are not only rich in content, but also very powerful in scalability, you can use these classes and objects to easily and quickly develop components you need. This article describes how to use the original class and object of the. NET development platform to write a winform component, compile its own component, and Program .

I. Basic Environment for program design and operation:

(1). Windows 2000 Server Edition

(2). Net Framework SDK beta 2

Ii. component functions developed in this article:

(1 ). the component developed in this article is a custom component, which is composed of two components, one is the label component and the other is the textbox component ).

(2 ). two new attributes are defined in the Custom component. One attribute is text, which is obtained by deriving the text attribute from the original text box. The other attribute is labeltext, it is obtained by inheriting the text attribute of the original tag.

(3). Purpose of the component.

In programming, a tag is often defined to display the text to be entered. Define a text box and enter the information. After this component is used, you only need to define a component and set different values for the component attributes. This simplifies the program design process. This will be reflected in subsequent component applications.
Iii. Difficulties and key points in component development:

(1). How to set the content of a custom component:

This component consists of the label component and the text box component. First, you must define the component structure. The specific program design is as follows:

// The labledtextbox component inherits the usercontrol component
Public class labeledtextbox: usercontrol
{
// Define the component structure
Private Label mylabel;
Private textbox mytextbox;
......
}

(2). How to derive the text attribute in the text box and generate its own new attribute:

Because it is the text attribute of the derived text box, the keyword "Override" is used in the program ". You can use the keyword "set" to set attributes and "get" to read attribute values of a component. The specific program design is as follows:

// The text attribute in the component is derived from the text attribute of the text box.
Public override string text
{
Get
{
Return mytextbox. text;
}
Set
{
Mytextbox. Text = value;
}
}

(3) how to create a new attribute labeltext, and the attribute value is obtained by inheriting the "text" attribute of the existing tag. The specific program design is as follows:

// Create a new property labeltext. The value of this property inherits the text attribute value of the label in this component.
Public String labeltext
{
Get
{
Return mylabel. text;
}
Set
{
Mylabel. Text = value;
}
}

4. source program of custom componentsCode(Control. CS ):

Control. CS Source code As follows:
Using system. Windows. forms;
// Define the namespace that encapsulates this component
Namespace mycontrols
{
// The labledtextbox component inherits the usercontrol component
Public class labeledtextbox: usercontrol
{
// Define the component structure
Private Label mylabel;
Private textbox mytextbox;

Public labeledtextbox ()
{
Initializecomponent ();
}

Public void initializecomponent ()
{< br> // define a label
mylabel = new label ();
mylabel. location = new system. drawing. point (0, 0);
mylabel. size = new system. drawing. size (100, 20);
// define a text box
mytextbox = new Textbox ();
mytextbox. location = new system. drawing. point (105, 0);
mytextbox. size = new system. drawing. size (100, 20);
// you must set the size of the expected component.
This. size = new system. drawing. size (205, 20);
// Add component
This. controls. add (mylabel);
This. controls. add (mytextbox);
}< br> // The text attribute in the component, is derived from the text attribute of the text box
Public override string text
{< br> Get
{< br> return mytextbox. text;
}< br> set
{< br> mytextbox. TEXT = value;
}< BR >}< br> // create a new property labeltext, the value of this attribute inherits the text attribute value of the label in this component.
Public String labeltext
{< br> Get
{< br> return mylabel. text;
}< br> set
{< br> mylabel. TEXT = value;
}< BR >}

So far, we have completed the building process of a new component. Next we will compile the source program file and produce components.
5. Compile components:

So far, what we have done is no different from writing a class in a normal application. what is different is the following compilation process. The result of compilation is to create a library, instead of an application. The specific compilation command is as follows:

CSC/R: system. Windows. Forms. dll/T: library control. CS

After compilation, the control. dll component is obtained.

6. Create a simple customer application:

There is no difference between using custom components and using the components provided in the. NET Framework SDK. follow these steps:

(1). Import the component namespace. In the application, it is to import mycontrols. The details are as follows:

Using mycontrols;

(2) define components encapsulated by this namespace in the program: three new components are used in the program. The details are as follows:

Protected labeledtextbox name, address, zip;

(3). Set the attributes of these components. In the program, you can see how to set the two custom attributes of the component. The following statement describes how to define the new attributes of a component in a program.

Name = new labeledtextbox ();
Name. Location = new system. Drawing. Point (5, 5 );
Name. labeltext = "name :";

Visibility is no different from defining other attributes.

(4) Add the component to the form.

The following figure shows the running interface of the source code (sample. CS) and the execution file generated by the Code:

The sample. Cs source code is as follows:
Using system. Windows. forms;
Using mycontrols; // The namespace of the imported component
Using system;

Public class form1: Form
{
// Define the new component
Protected labeledtextbox name, address, zip;
Protected button show;

Public form1 ()
{
Initializecomponent ();
}
Public static void main ()
{
Application. Run (New form1 ());
}

Public void initializecomponent ()
{
// Create a new component, which encapsulates labels and text boxes.
Name = new labeledtextbox ();
Address = new labeledtextbox ();
Zip = new labeledtextbox ();
Show = new button ();
// Set the attribute values of the new component. You can see how to set the text and labeltext attributes.
Name. Location = new system. Drawing. Point (5, 5 );
Name. labeltext = "name :";

Address. Location = new system. Drawing. Point (5, 35 );
Address. labeltext = "Address :";

Zip. Location = new system. Drawing. Point (5, 70 );
Zip. labeltext = "zip code :";

Show. Location = new system. Drawing. Point (5,100 );
Show. Text = "display Component Property Values ";
Show. size = new system. Drawing. Size (100, 25 );
Show. Click + = new system. eventhandler (show_click );

This. Text = "displays the labeltext attribute and text attribute value of the self-built component! ";
This. Controls. Add (name );
This. Controls. Add (Address );
This. Controls. Add (ZIP );
This. Controls. Add (show );
}
Protected void show_click (Object sender, eventargs E)
{
String message = Name. labeltext + "" + name. text;
Message + = "\ n" + address. labeltext + "" + address. text;
Message + = "\ n" + Zip. labeltext + "" + Zip. text;
MessageBox. Show (message, "labeltext attributes and text attributes of the component are as follows :");
}
}

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.